Mybatis 快速入门
Mybatis 简介
MyBatis是一个优秀的基于 ORM 的半自动轻量级持久层框架,它对db的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建 connection、创建 statement 手动设置参数、结果集检索等 jdbc 繁杂的过程代码。
Mybatis 快速入门
1.创建数据库及 user 表
CREATE TABLE `user` (
`id` int DEFAULT NULL,
`name` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,
`age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2.创建 maven工程,导入依赖( MySQL驱动、 mybatis、 Junit)
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
3.编写 User 实体类
package com.orginly;
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Demo{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4.编写 UserMapper.xm映射配置文件(ORM思想)
<?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">
<!-- resultType: 返回的结果类型(自动映射封装) -->
<select id="findAll" resultType="com.orginly.User">
select * from user
</select>
</mapper>
5.编写 SqIMapConfig.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>
<!-- environments:运行环境 default:默认环境 -->
<environments default="development">
<environment id="development">
<!-- 当前的事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 数据源信息 POOLED:使用 mybatis 的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/demo?characterEnCoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="000"/>
</dataSource>
</environment>
</environments>
<!-- 引入配置文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
- 映射关系配置的引入(引入映射配置文件的路径)
SqIMapConfig.xml 中添加
<!-- 引入配置文件 -->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
- 编写测试代码
@Test
public void quickStart() throws IOException {
// 1. 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2. 获取 sqlSessionFactory 工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3. 获取 sqlSession 会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4. 执行 sql 参数:namespace + . + id
List<User> users = sqlSession.selectList("user.findAll");
for (User item : users) {
System.out.println(item);
}
// 5. 关闭资源
sqlSession.close();
}