Mybatis-plus的两种分页方案

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

https://gitee.com/pianjiao006/boot-spring.git

相关推荐
毕业设计70313 小时前
(免费领源码)SSM董家湾线上点餐系统04682- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·redis·mysql·tomcat·maven·mybatis·ssm
云絮.2 天前
mybatis(二)
java·数据库·mybatis
夕除3 天前
redis--主从还是集群,内存
java·spring·mybatis
驭渊的小故事3 天前
MyBatis-Plus 实战指南:从基础 CRUD 到条件构造器与自定义 SQL(二)
数据库·sql·mybatis
毕业设计7033 天前
(免费领源码)基于node的学生公寓管理系统06412- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
vue.js·mysql·node.js·maven·mybatis·express·b/s
驭渊的小故事4 天前
MyBatis XML 开发实战:从配置到 CRUD 全解析
xml·java·mybatis
ab1237684 天前
MyBatis-Plus 知识点整理
mybatis-plus
云絮.4 天前
Mybatis(一)
java·数据库·mybatis
她的男孩7 天前
权限通了、列表有数,一点分页就炸Forge Admin 用 COUNT(1) 扛住复杂待办
java·spring boot·mybatis
paopaokaka_luck7 天前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis