Spring Boot 中使用 TKMybatis 和 PageHelper

Posted by 彭超 on 2019-06-09
Estimated Reading Time 3 Minutes
Words 608 In Total
Viewed Times

整合 Druid 数据源

引入依赖

pom.xml 中引入 druid-spring-boot-startermysql-connector-java 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 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>

相关配置

application.yml 中添加以下配置

1
2
3
4
5
6
7
8
9
10
11
spring:
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/{db_name}?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: {username}
password: {password}
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

整合 tk.mybatis

tk.mybatis 整合了 MyBatis 框架,在其基础上提供了很多工具,封装了常用的增删改查 SQL 语句,可以让我们的开发效率更高。

引入依赖

pom.xml 中引入 mapper-spring-boot-starter 依赖

1
2
3
4
5
6
<!-- mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>

相关配置

application.yml 中添加 mybatis 相关配置,并且设置日志监听路径

1
2
3
4
5
6
7
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 # 配置监听日志

整合 PageHelper 分页插件

引入依赖

pom.xml 中引入 pagehelper-spring-boot-starter 依赖

1
2
3
4
5
6
<!-- pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>

相关配置

Application 入口类中使用 tk.mybatis.spring.annotation 包下的 @MapperScan 注解指定 Mapper 接口的扫描路径

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

}

分页查询示例

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
@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();
}
}

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !