在 pom.xml 中引入 druid-spring-boot-starter 和 mysql-connector-java 依赖
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
5.1.40
runtime
相关配置
在 application.yml 中添加以下配置
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 依赖
tk.mybatis
mapper-spring-boot-starter
2.0.2
相关配置
在 application.yml 中添加 mybatis 相关配置,并且设置日志监听路径
mybatis:
type-aliases-package: # 实体类的存放路径,如:com.antoniopeng.hello.spring.boot.entity
mapper-locations: classpath:mapper
@Test
public void testPageHelper() {
Example example = new Example(User.class);
// 查询条件
example.createCriteria().andEqualTo("userId", "1")
// 分页参数
PageHelper.startPage(1, 10, "create_time desc");
// 获取分页列表数据
List userList = userService.selectByExample(example);
PageInfo pageInfo = new PageInfo(userList);
// 获取列表总数
int userCount = (int) pageInfo.getTotal();
}
}
- 文章作者:彭超
- 本文首发于个人博客:https://antoniopeng.com/2019/06/09/springboot/SpringBoot%E4%B8%AD%E4%BD%BF%E7%94%A8TKMybatis%E5%92%8CPageHelper/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭超 | Blog!



