Spring Boot 整合 TKMybatis 二次简化持久层代码的实现
作者:暮夏有五 时间:2021-06-17 06:21:26
经常用 MyBatis 的的都知道,使用这个框架存在一个非常不友善的问题就是,就是每操作一个单表就需要自己手写一个 xml 文件,虽然说可以用工具生成 xml 和实体类可以解决这个问题,但是二次开发的时候对某个表字段进行修改的时候,生成 xml 文件就不现实啦。最近发现 tk.mybatis 就非常好的解决了这个问题。tk.mybatis 整合了 MyBatis 框架,在其基础上提供了很多工具,封装了常用的增删改查 SQL 语句,可以让我们的开发效率更高。在这里和大家分享一下。
引入依赖
在 pom.xml
中引入 mapper-spring-boot-starter
依赖
<!-- druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 数据库连接依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
<scope>runtime</scope>
</dependency>
<!-- mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
相关配置
在 application.yml
中添加相关配置
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.jdbc.Driver # MySQL 8.x: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: # 实体类的存放路径,如:com.antoniopeng.hello.spring.boot.entity
mapper-locations: classpath:mapper/*.xml # mapper.xml 文件存放路径,这里存放在配置文件目录 resources 下
logging:
level:
com.antoniopeng.hello.springboot.mybatis: debug # 配置监听日志
在 Application
入口类中使用 tk.mybatis.spring.annotation
包下的 @MapperScan
注解指定 Mapper 接口的扫描路径
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan(value = "com.antoniopeng.springboot.mybatis.mapper")
@SpringBootApplication
public class HelloSpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootMybatisApplication.class, args);
}
}
整合 PageHelper 分页插件
引入依赖
在 pom.xml
中引入 pagehelper-spring-boot-starter
依赖
<!-- pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
分页查询示例
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Transactional
@Rollback
public class MyBatisTests {
@Autowired
UserService userService;
/**
* 测试分页插件
*/
@Test
public void testPageHelper() {
Example example = new Example(User.class);
// 查询条件
example.createCriteria().andEqualTo("userId", "1")
// 分页参数
PageHelper.startPage(1, 10, "create_time desc");
// 获取分页列表数据
List<User> userList = userService.selectByExample(example);
PageInfo pageInfo = new PageInfo(userList);
// 获取列表总数
int userCount = (int) pageInfo.getTotal();
}
}
来源:https://blog.csdn.net/monianqing/article/details/113212238
标签:Spring,Boot,整合,TKMybatis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JDBC自定义连接池过程详解
2023-11-17 13:27:55
C++/java 继承类的多态详解及实例代码
2022-11-28 10:22:06
Java将byte[]转图片存储到本地的案例
2021-08-30 08:19:12
C#仿Windows XP自带的扫雷游戏
2023-07-30 07:40:48
![](https://img.aspxhome.com/file/2023/5/69785_0s.jpg)
flutter Bloc 实现原理示例解析
2023-07-18 08:00:39
SpringBoot 嵌入式web容器的启动原理详解
2021-12-29 23:23:14
![](https://img.aspxhome.com/file/2023/8/64908_0s.png)
JAVA 字符串加密、密码加密实现方法
2023-11-28 04:08:09
java String类常量池分析及"equals"和"==”区别详细介绍
2021-11-28 01:54:48
基于java构造方法Vector删除元素源码分析
2023-11-25 14:54:45
![](https://img.aspxhome.com/file/2023/8/60238_0s.png)
Android实现简洁的APP更新dialog数字进度条
2023-01-29 18:53:39
![](https://img.aspxhome.com/file/2023/8/93318_0s.gif)
Spring Boot Reactor 整合 Resilience4j详析
2021-08-08 10:30:02
![](https://img.aspxhome.com/file/2023/2/62362_0s.png)
Java实现的简单网页截屏功能示例
2021-05-25 13:50:35
![](https://img.aspxhome.com/file/2023/9/88219_0s.png)
Dynamic和Var的区别及dynamic使用详解
2022-09-22 13:18:23
SpringBoot @PropertySource与@ImportResource有什么区别
2023-08-22 02:02:47
C#检测是否有u盘插入的方法
2023-11-19 13:11:00
java中List删除时需要的注意事项
2023-11-11 01:28:00
Java网络编程实现的简单端口扫描器示例
2022-04-17 09:59:18
超详细讲解Java异常
2023-10-02 07:30:16
![](https://img.aspxhome.com/file/2023/3/80853_0s.png)
零基础写Java知乎爬虫之获取知乎编辑推荐内容
2023-11-29 04:33:39
![](https://img.aspxhome.com/file/2023/3/60783_0s.png)
Java中的泛型详解
2023-11-02 17:51:56