Mybatis-plus的两种分页方案
底层的逻辑是写出最终执行的sql或者selectPage方法等,中间需要配置相应的page拦截器。
注意:如没有配置拦截器,直接执行就会存在total的值为0,此方法无效。
1. 基于MP的IPage接口实现
使用步骤:
- 引入pom配置
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
- 配置分页拦截器
java
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
- 执行测试
java
@Test
public void testSelectPage(){
Page<User> page = new Page<>(0, 5);
page.addOrder(OrderItem.asc("age"));
page.setOptimizeCountSql(false);
//关闭自动优化
Page<User> userIPage = userMapper.selectPage(page, Wrappers.<User>lambdaQuery().eq(User::getAge, 19).like(User::getName, "bob"));
assertThat(page).isSameAs(userIPage);
log.error("总条数 -------------> {}", userIPage.getTotal());
log.error("当前页数 -------------> {}", userIPage.getCurrent());
log.error("当前每页显示数 -------------> {}", userIPage.getSize());
List<User> records = userIPage.getRecords();
assertThat(records).isNotEmpty();
}
2. 基于PageHelper的第三方组件实现
- 引入配置
xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<!-- pagehelper 包含该依赖存在版本冲突,因此不建议和 mp 一起混用 -->
<exclusions>
<exclusion>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
-
配置拦截器
@Configuration
public class MyBatisPlusConfig {// 基于PageHelper @Bean PageInterceptor pageInterceptor() { PageInterceptor pageInterceptor = new PageInterceptor(); Properties properties = new Properties(); properties.setProperty("helperDialect", "mysql"); pageInterceptor.setProperties(properties); // 由此可进入源码, return pageInterceptor; }
}
-
执行测试
java
@Test
void test() {
// pagehelper
Map<String, Object> params = new HashMap<>();
params.put("name", "%");
PageHelper.startPage(3, 2);
List<User> list = userMapper.pageUser(params);
System.out.println("list.size=" + info.getList().size());
log.info(JSONUtil.toJsonStr(info.getList()));
System.out.println("page.total=" + info.getTotal());
}
########分隔符#########
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pimee.bootmybatisplus.mapper.UserMapper">
<select id="pageUser" resultType="com.pimee.bootmybatisplus.model.User">
select id, name, age, email from user
<where>
<if test="name!=null and name!=''">
name like #{name}
</if>
</where>
</select>
</mapper>
########分隔符#########
@Mapper
public interface UserMapper extends BaseMapper<User> {
/**
* 分页列表
* @return
*/
List<User> pageUser(Map<String, Object> params);
}
测试代码在这:https://gitee.com/pianjiao006/boot-spring/tree/master/boot-mybatis-plus