MyBatis的基本使用情况的一次记录

添加依赖

Maven 中添加如下依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>

<!-- PostgreSQL数据库驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24</version>
</dependency>
</dependencies>

创建数据库和表

1
2
3
4
5
6
7
8
9
10
-- 创建数据库
CREATE DATABASE emp_database;

-- 创建数据表
CREATE TABLE employee
(
employee_id int, -- 雇员id
name text, -- 姓名
salary numeric -- 薪水
);

创建 employee 表对应的 POJO 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cn.zzq.model;

public class Employee {
private int employeeId;
private String name;
private float salary;


public Employee(int employeeId, String name, float salary) {
this.employeeId = employeeId;
this.name = name;
this.salary = salary;
}

public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}
}

XML 配置

添加 MyBatis 的 XML 配置文件如下:

image-20211006210849785

mybatis-config.xml 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 数据库驱动,这里使用的是pg,故导入pg的驱动类名-->
<property name="driver" value="org.postgresql.Driver"/>

<!-- 数据库连接的url-->
<property name="url" value="jdbc:postgresql://localhost:5432/emp_database"/>

<!-- 数据库用户名与密码-->
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
</dataSource>
</environment>
</environments>

<!-- mapper映射文件-->
<mappers>
<mapper resource="mapper/employee.xml"/>
</mappers>
</configuration>

mapper 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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:命名空间,随便写,一般保证命名空间唯一 -->
<mapper namespace="Employee">

<!-- 创建一个查询语句,用于查找所有的雇员 -->
<select id="selectAllEmployee" resultType="cn.zzq.model.Employee">
SELECT *
FROM employee
</select>

<!-- 创建插入语句,#{这里填写POJO中的属性名}-->
<insert id="insertEmployee" parameterType="cn.zzq.model.Employee">
INSERT INTO employee(employee_id, name, salary)
VALUES (#{employeeId}, #{name}, #{salary})
</insert>
<!--查询工资大于5000的人员信息-->
<select id="selectPersonSalaryBiggerThan5000" resultType="cn.zzq.model.Employee">
SELECT *
FROM employee
WHERE salary > 5000
</select>
</mapper>

编写主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package cn.zzq;

import cn.zzq.model.Employee;
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 Main {
public static void main(String[] args) throws IOException {
String mybatisConfigPath = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(mybatisConfigPath);
// 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();

try {
// 定义一个employee的entity
Employee employee = new Employee(1, "EmployTest", 23);
// 将其插入数据表中
sqlSession.selectOne("Employee.insertEmployee", employee);
// 查询数据表
List<Employee> employeeList = sqlSession.selectList("Employee.selectAllEmployee");
System.out.println(employeeList);

sqlSession.selectOne("Employee.insertEmployee", new Employee(5, "Test", 6000));
sqlSession.selectOne("Employee.insertEmployee", new Employee(7, "test1", 5100));

List<Employee> persons1 = sqlSession.selectList("Employee.selectPersonSalaryBiggerThan5000");
System.out.println(persons1);


} finally {
sqlSession.close();
}
}
}

输出结果

image-20211006213006468

[Employee{employeeId=1, name=‘EmployTest’, salary=23.0}]
[Employee{employeeId=5, name=‘Test’, salary=6000.0}, Employee{employeeId=7, name=‘test1’, salary=5100.0}]

参考文献

MyBatis 官网中文入门指南