Spring Boot 整合 TKMybatis Spring Boot 整合 TKMybatis 二次简化持久层代码的实现

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring Boot 整合 TKMybatis Spring Boot 整合 TKMybatis 二次简化持久层代码的实现

暮夏有五   2021-01-27 我要评论

经常用 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();
 }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们