SpringBoot & TK & PageHelper 整合
步骤
1.导入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.mapper.version>2.0.4</mybatis.mapper.version>
<mybatis.pagehelper.version>1.2.9</mybatis.pagehelper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mybatis.mapper.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${mybatis.pagehelper.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>demo-orm-mybatis-mapper-page</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.编写yml配置类
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
initialization-mode: always
continue-on-error: true
hikari:
minimum-idle: 5
connection-test-query: SELECT 1 FROM DUAL
maximum-pool-size: 20
auto-commit: true
idle-timeout: 30000
pool-name: SpringBootDemoHikariCP
max-lifetime: 60000
connection-timeout: 30000
logging:
level:
com.xkcoding: debug
com.xkcoding.orm.mybatis.MapperAndPage.mapper: trace
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.demo.orm.mybatis.MapperAndPage.entity
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
not-empty: true
style: camelhump
wrap-keyword: "`{0}`"
safe-delete: true
safe-update: true
identity: MYSQL
pagehelper:
auto-dialect: true
helper-dialect: mysql
reasonable: true
params: count=countSql
3.编写注解
@SpringBootApplication
@MapperScan(basePackages = {"com.demo.orm.mybatis.MapperAndPage.mapper"})
public class SpringBootDemoOrmMybatisMapperPageApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoOrmMybatisMapperPageApplication.class, args);
}
}
测试
@Slf4j
public class UserMapperTest extends SpringBootPageApplicationTest {
@Autowired
private UserMapper userMapper;
@Test
public void testQueryByPageAndSort() {
initData();
int currentPage = 1;
int pageSize = 5;
String orderBy = "id desc";
int count = userMapper.selectCount(null);
PageHelper.startPage(currentPage, pageSize, orderBy);
List<User> users = userMapper.selectAll();
PageInfo<User> userPageInfo = new PageInfo<>(users);
Assert.assertEquals(5, userPageInfo.getSize());
Assert.assertEquals(count, userPageInfo.getTotal());
log.debug("【userPageInfo】= {}", userPageInfo);
}
@Test
public void testQueryByCondition() {
initData();
Example example = new Example(User.class);
example.createCriteria().andLike("name", "%Save1%").orEqualTo("phoneNumber", "17300000001");
example.setOrderByClause("id desc");
int count = userMapper.selectCountByExample(example);
PageHelper.startPage(1, 3);
List<User> userList = userMapper.selectByExample(example);
PageInfo<User> userPageInfo = new PageInfo<>(userList);
Assert.assertEquals(3, userPageInfo.getSize());
Assert.assertEquals(count, userPageInfo.getTotal());
log.debug("【userPageInfo】= {}", userPageInfo);
}
private void initData() {
testInsertList();
}
}