MyBatis的基本使用
快速入门
Mybatis 的官网:https://mybatis.org/mybatis-3/
MyBatis 的开发步骤
添加 MyBatis 的坐标
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--mybatis坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--mysql驱动坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!--单元测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--⽇志坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
创建 user 数据表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
INSERT INTO `user` VALUES ('1', '小唐', '123');
INSERT INTO `user` VALUES ('2', '小张', '456');
INSERT INTO `user` VALUES ('3', '小雨', '789');
编写 User 实体类
/**
* 用户信息
*
* @name: User
* @author: terwer
* @date: 2022-03-15 11:32
**/
public class User {
private Integer id;
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
编写映射⽂件 UserMapper.xml
<?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="user">
<select id="findAll" resultType="com.test.pojo.User">
select * from user
</select>
</mapper>
编写核⼼⽂件 SqlMapConfig.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<!-- 当前事务交给JDBC -->
<transactionManager type="JDBC"></transactionManager>
<!-- 当前只用MyBatis提供的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/zdy_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
编写测试类
@Test
public void test1() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("user.findAll");
for (User user : userList) {
System.out.println(user);
}
// 关闭资源
sqlSession.close();
}
运行结果
!乱码解决
修改 url 为
jdbc:mysql://localhost:3306/zdy_mybatis?characterEncoding=utf8
效果
完善,添加新增方法
<!-- 添加用户
parameterType:参数类型
-->
<insert id="saveUser" parameterType="com.terwergreen.pojo.User">
insert into user(id, username) values(#{id}, #{username})
</insert>
没有添加成功原因
未提交事务
解决
sqlSession.insert("user.saveUser", user);
sqlSession.commit();
更新
<!-- 修改 -->
<update id="updateUser" parameterType="com.terwergreen.pojo.User">
update user set username=#{username} where id=#{id}
</update>
删除
<!-- 删除 -->
<update id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{id}
</update>
备注:只有一个参数的时候,参数名的占位符可以是任意字符,例如下面的语句也是正常的
<!-- 删除 -->
<update id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{abc}
</update>
MyBatis 的 Dao 层实现
传统开发方式
-
定义 Dao 层接口
/** * 用户Dao层接口 * * @name: IUserDao * @author: terwer * @date: 2022-03-16 20:53 **/ public interface IUserDao { /** * 查询所有用户 */ public List<User> findAll() throws IOException; }
-
DaoImpl 实现
/** * 用户Dao层实现类 * * @name: UserDaoImpl * @author: terwer * @date: 2022-03-16 20:54 **/ public class UserDaoImpl implements IUserDao { @Override public List<User> findAll() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); List<User> userList = sqlSession.selectList("user.findAll"); // 关闭资源 sqlSession.close(); return userList; } }
-
测试
@Test public void test5() throws Exception{ IUserDao userDao = new UserDaoImpl(); List<User> all = userDao.findAll(); for (User user : all) { System.out.println(user); } }
代理开发方式
Mapper 接口开发方法只需要程序员编写 Mapper 接口(相当于 Dao 接口),由 Mybatis 框架根据接口定义创建接
口的动态代理对象,代理对象的方法体同上边 Dao 接口实现类方法。
Mapper 接口开发需要遵循以下规范:
1) Mapper.xml 文件中的 namespace 与 mapper 接口的全限定名相同
2) Mapper 接口方法名和 Mapper.xml 中定义的每个 statement 的 id 相同
3) Mapper 接口方法的输入参数类型和 mapper.xml 中定义的每个 sql 的 parameterType 的类型相同
4) Mapper 接口方法的输出参数类型和 mapper.xml 中定义的每个 sql 的 resultType 的类型相同
编写 UserMapper 接口
测试代理方式
// ===============
// Dao层代理开发方式
@Test
public void test5() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
IUserDao userDao = sqlSession.getMapper(IUserDao.class);
List<User> userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
}
文章更新历史
2022/05/08 feat: 初稿