Spring Boot整合MyBatis
本文最后更新于:2 年前
引言
随着微服务和容器化技术的普及,Spring Boot成为Java开发者首选的微服务框架之一,而MyBatis作为一个灵活且功能强大的持久层框架,其与Spring Boot的集成可以极大地提高数据库操作的效率和灵活性。本文将详细介绍如何在Spring Boot项目中集成MyBatis,从依赖管理到配置细节,每一步都旨在帮助开发者构建一个高效、可维护的应用。
引入依赖
在Spring Boot项目中引入MySQL连接、MyBatis、pagehelper的Maven依赖:
1 |
|
数据源配置
这里我们采用“配置文件+Java配置类”的形式来配置数据源
在项目配置文件中指定数据库连接信息
1 |
|
这里我们采用连接池方案,配置连接池的相关参数
1 |
|
对应的Java Properties类为
1 |
|
对应的数据源实现配置为
1 |
|
MyBatis自身配置
1 |
|
分页插件配置
1 |
|
日志配置
在系统日志配置文件中指定相关类的日志级别
1 |
|
测试
以Student表为例,编写数据库Mapper文件
1
2
3
4
5
6
7
8
9
10
11
12<?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="space.yangtao.mapper.StudentMapper">
<select id="list" resultType="space.yangtao.domain.po.StudentPO">
SELECT * FROM tt_student
</select>
</mapper>对应的Mapper接口为
1
2
3
4
5
6@Mapper
public interface StudentMapper {
List<StudentPO> list();
}定义服务层接口及对应的实现类
1
2
3
4
5public interface StudentService {
PageInfo<StudentPO> page();
}1
2
3
4
5
6
7
8
9
10
11
12
13
14@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Override
@Transactional(rollbackFor = Exception.class, readOnly = true)
public PageInfo<StudentPO> page() {
PageHelper.startPage(2, 2);
return new PageInfo<>(studentMapper.list());
}
}编写视图层接口,用于测试
1
2
3
4
5
6
7
8
9
10
11
12
13@RestController
@RequestMapping("/student")
public class StudentController {
@Resource
private StudentService studentService;
@GetMapping("/page")
public PageInfo<StudentPO> page() {
return studentService.page();
}
}启动项目,通过HTTP请求进行测试
1
curl -XGET http://localhost:8080/student/page
请求结果如下
1
{"total":3,"list":[{"id":3,"classId":2,"name":"zhangsan2","height":180.0,"gender":"男","birthday":"1989-12-31T16:00:00.000+00:00","createTime":"2022-07-01T14:10:22.000+00:00"}],"pageNum":2,"pageSize":2,"size":1,"startRow":3,"endRow":3,"pages":2,"prePage":1,"nextPage":0,"isFirstPage":false,"isLastPage":true,"hasPreviousPage":true,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[1,2],"navigateFirstPage":1,"navigateLastPage":2}
控制台输出如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2518:00:56.756 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/student/page", parameters={}
18:00:56.756 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to space.yangtao.domain.controller.StudentController#page()
18:00:56.757 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Creating new transaction with name [space.yangtao.service.StudentServiceImpl.page]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-java.lang.Exception
18:00:56.757 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Acquired Connection [HikariProxyConnection@493797028 wrapping com.mysql.cj.jdbc.ConnectionImpl@2ac4f26f] for JDBC transaction
18:00:56.757 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Switching JDBC Connection [HikariProxyConnection@493797028 wrapping com.mysql.cj.jdbc.ConnectionImpl@2ac4f26f] to manual commit
18:00:56.757 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
18:00:56.758 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@195129d1]
18:00:56.758 [http-nio-8080-exec-3] DEBUG SQL_CACHE - Cache Hit Ratio [SQL_CACHE]: 0.6666666666666666
18:00:56.758 [http-nio-8080-exec-3] DEBUG o.m.spring.transaction.SpringManagedTransaction - JDBC Connection [HikariProxyConnection@493797028 wrapping com.mysql.cj.jdbc.ConnectionImpl@2ac4f26f] will be managed by Spring
18:00:56.758 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list_COUNT - ==> Preparing: SELECT count(0) FROM tt_student
18:00:56.758 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list_COUNT - ==> Parameters:
18:00:56.759 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list_COUNT - <== Total: 1
18:00:56.760 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list - ==> Preparing: SELECT * FROM tt_student LIMIT ?, ?
18:00:56.760 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list - ==> Parameters: 2(Long), 2(Integer)
18:00:56.760 [http-nio-8080-exec-3] DEBUG space.yangtao.mapper.StudentMapper.list - <== Total: 1
18:00:56.761 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@195129d1]
18:00:56.761 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@195129d1]
18:00:56.761 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@195129d1]
18:00:56.761 [http-nio-8080-exec-3] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@195129d1]
18:00:56.761 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Initiating transaction commit
18:00:56.761 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Committing JDBC transaction on Connection [HikariProxyConnection@493797028 wrapping com.mysql.cj.jdbc.ConnectionImpl@2ac4f26f]
18:00:56.761 [http-nio-8080-exec-3] DEBUG o.s.jdbc.support.JdbcTransactionManager - Releasing JDBC Connection [HikariProxyConnection@493797028 wrapping com.mysql.cj.jdbc.ConnectionImpl@2ac4f26f] after transaction
18:00:56.761 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
18:00:56.761 [http-nio-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Writing [PageInfo{pageNum=2, pageSize=2, size=1, startRow=3, endRow=3, total=3, pages=2, list=Page{count=true (truncated)...]
18:00:56.762 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK由请求结果和控制台日志可知,Web环境下MyBatis的各项基本配置已经配置成功。
总结
通过本文的介绍,开发者可以详细了解如何在Spring Boot环境下配置和使用MyBatis,包括如何设置数据源、如何配置MyBatis以优化执行和管理SQL会话,以及如何利用MyBatis的高级功能如分页插件来增强应用的性能。此外,文章还探讨了如何通过适当的日志配置来监控和调试数据库交互,确保应用的稳定运行。整合MyBatis后,开发者将能够更有效地管理数据库操作,同时保持代码的清晰和组织性。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!