-
创建mybatis-Dao
项目
-
导入核心的pox.xml
依赖坐标
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
-
在main
文件下的创建com.zcl.domain
包,并创建User
实体类对于数据库的字段
package com.zcl.domain;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
-
在main
文件下的创建com.zcl.dao
包,并创建UserMapper
接口,对接映射文件的命名空间namespace
package com.zcl.dao;
import com.zcl.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll();
User findById(int id);
}
同包下创建impl
并创建接口实现类UserMapperImpl
,完成对对数据的查询和返回操作
package com.zcl.dao.impl;
import com.zcl.dao.UserMapper;
import com.zcl.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserMapperImpl implements UserMapper {
@Override
public List<User> findAll() {
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
return all;
}
@Override
public User findById(int id) {
InputStream resourceAsStream = null;
try {
resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User all = mapper.findById(id);
return all;
}
}
代理开发方式与传统方式不同的地方在于:
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
代替了原先直接指定namespace
命名空间下的sql方法id
直接调用mapper 里面的接口方法,就可以了
-
在main
文件下的创建com.zcl.service
包,并创建ServiceDemo
模拟业务层获取dao查询返回的对象
package com.zcl.service;
import com.zcl.dao.impl.UserMapperImpl;
import com.zcl.domain.User;
import java.io.IOException;
import java.util.List;
public class ServiceDemo {
public static void main(String[] args) throws IOException {
UserMapperImpl userMapper = new UserMapperImpl();
List<User> all = userMapper.findAll();
System.out.println(all);
User byId = userMapper.findById(1);
System.out.println(byId);
}
}
执行方法可以输出查询的内容
-
在resources
文件下创建com/zcl/mapper
包目录,并在里面创建UserMapper.xml
配置文件编写sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zcl.dao.UserMapper">
<select id="findAll" resultType="user">
select * from user
</select>
<select id="findById" resultType="user" parameterType="int">
select * from user where id = #{id}
</select>
</mapper>
namespace
命名空间改为了com.zcl.dao.UserMapper
对接dao层的UserMapper
接口,mybatis会自动代理完成
findAll:需要与接口的方法一致
resultType:输出参数类型要与接口中的方法返回值类型一致
-
在resources
文件下创建jdbc.properties
文件,存放数据库的连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=1234
-
在resources
文件下创建mybatis-config.xml
核心配置文件信息
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<typeAliases>
<typeAlias type="com.zcl.domain.User" alias="user"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zcl/mapper/UserMapper.xml"/>
</mappers>
</configuration>