摘要
JDK:1.8.0_202
MyBatis Version:3.5.9
Spring Version:
SpringBoot Version:
样例表:下载
# 一:普通项目
# 1.1 引入依赖
在 pom.xml 中引入 MyBatis 依赖,具体版本可以查看 Maven 仓库 (opens new window),最后完整的 pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.qform</groupId>
<artifactId>mybatis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mybatis-demo</name>
<url>https://qform.top</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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.5.9</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 1.2 配置
在 src/main/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>
<settings>
<!-- 指定日志 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 是否开启自动驼峰命名规则 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="top.qform.model"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ccj_test"/>
<property name="username" value="root"/>
<property name="password" value="1qazwsx23edc"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/CustomersMapper.xml"/>
</mappers>
</configuration>
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
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
配置说明:
- <settings>:logImpl属性配置指定使用LOG4J输出日志;mapUnderscoreToCamelCase属性,开启自动驼峰命名
- <typeAliases>:元素下面配置了一个包的别名,通常确定一个类的时候需要使用类的全限定名称,例如 top.qform.model.Customers。在MyBatis中需要频繁用到类的全限定名称,为了方便使用,配置了 top.qform.model 包,这样配置后,在使用类的时候不需要写包名的部分,只使用 Customers 即可;
- <environments>:主要配置了数据库连接;
- <mappers>:配置了一个包含完整路径的 Customers.xml,这是一个MyBatis的SQL语句和映射配置文件。
在 src/main/resources 下创建 log4j.properties
日志配置
# 全局配置
log4j.rootLogger=ERROR,stdout
# MyBatis 日志配置
log4j.logger.mapper=TRACE
# 控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上面 log4j.logger.mapper
配置的 mapper 对应 mapper 包,此处为资源目录下有 mapper 目录
# 1.3 测试
1) 导入样例表
2)实体类Customers
public class Customers {
private Integer custId;
private String custName;
private String custAddress;
private String custCity;
private String custState;
private String custZip;
private String custCountry;
private String custContact;
private String custEmail;
// 省略 getter 和 setter
@Override
public String toString() {
return "Customers{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custAddress='" + custAddress + '\'' +
", custCity='" + custCity + '\'' +
", custState='" + custState + '\'' +
", custZip='" + custZip + '\'' +
", custCountry='" + custCountry + '\'' +
", custContact='" + custContact + '\'' +
", custEmail='" + custEmail + '\'' +
'}';
}
}
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
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
3)CustomersMapper.xml
Mapper命名方式:
在MyBatis中,根据MyBatis官方的习惯,一般用Mapper作为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="mapper.CustomersMapper">
<select id="selectAll" resultType="Customers">
select * from customers
</select>
</mapper>
1
2
3
4
5
6
7
2
3
4
5
6
7
配置说明:
- <mapper>:XML的根元素,属性namespace定义了当前XML的命名空间
- <select>:定义的一个SELECT查询
- id属性:定义了当前SELECT查询的唯一标识
- resultType:定义了当前查询的返回值类型,此处就是指实体类 Customers,前面配置中提到的别名主要用于这里,如果没有设置别名,此处就需要写成resultType="top.qform.model.Customers"
4)单元测试
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 org.junit.BeforeClass;
import org.junit.Test;
import java.io.Reader;
public class CustomersTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void init() {
try (Reader reader = Resources.getResourceAsReader("mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Test
public void testSelectAll() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
List<Customers> customers = sqlSession.selectList("selectAll");
customers.forEach(System.out::println);
}
}
}
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
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
运行结果:
# 二:SSM项目
# 三:SpringBoot项目
# 四:参考文献
- 《MyBatis从入门到精通 - 刘增辉》
- MyBatis官网 (opens new window)