版本信息

  • Spring Boot 3.3.0
  • Mybatis 3.5.16
  • JDK 17

Demo搭建

搭建一个简单的Spring Boot项目,方便调试Mybatis源码。

首先创建一个Spring Boot的项目,我这里将项目命名为Mybatis_Reader,并引入相关依赖。

设置依赖

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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

yml配置

创建application.yml,并添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
# 配置数据源
spring:
application:
name: Mybatis_Reader
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog_db?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
mybatis:
config-location: classpath:mybatis-config.xml

Mybatis配置类

在resources文件夹中创建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
<?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="mapUnderscoreToCamelCase" value="TRUE"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/blog_db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>

<mappers>
<!-- Register mapper files -->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>

</configuration>

日志文件配置

创建logback.xml,以自定义输出日志信息,内容简单点就行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>

<logger name="org.apache.ibatis" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
</configuration>

表结构

数据库中创建一张用户表用来做测试

1
2
3
4
5
6
7
CREATE TABLE `T_USER` (
`id` bigint NOT NULL AUTO_INCREMENT,
`USER_NAME` varchar(45) DEFAULT NULL COMMENT '姓名',
`AGE` int DEFAULT NULL COMMENT '年龄',
`GENDER` int DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户主表'

创建相关的类

项目中创建一个mybatis的包,然后创建相关的类

JDBCConstant.java

1
2
3
4
5
public class JDBCConstant {
public static final String URL = "jdbc:mysql://localhost:3306/blog_db?useUnicode=true&characterEncoding=utf-8";
public static final String USERNAME = "root";
public static final String PASSWORD = "123456";
}

UserPO.java

1
2
3
4
5
6
7
8
9
10
@Data
public class UserPO implements Serializable {

private Long id;

private String userName;

private Integer gender;

}

UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
@Mapper
@CacheNamespace
public interface UserMapper {

@Select("SELECT * FROM T_USER WHERE id = #{id}")
@Options
UserPO selectUserById(Long id);

@Update("UPDATE T_USER SET USER_NAME = #{arg0} WHERE id = #{arg1}")
int updateUserNameById(Long id, String name);

List<UserPO> queryUserList();
}

自定义SQL

resources中创建一个mapper文件夹,用以放置编写自定义SQL的XML文件。

UserMapper.xml

1
2
3
4
5
6
7
8
9
<?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="org.example.mybatis_reader.mybatis.UserMapper">
<select id="queryUserList" resultType="org.example.mybatis_reader.mybatis.UserPO">
SELECT * FROM T_USER
</select>
</mapper>

启动类配置以及项目接口

最后在启动类上加上@MapperScan(“XX”)就可以开始下一步的调试动作了,整个项目大致如下

image-20240605105404387