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

相关推荐
☀Mark_LY6 小时前
MyBatis-Flex入门以及多数据源配置
java·mybatis
人道领域8 小时前
javaWeb从入门到进阶(SpringBoot基础案例2)
java·开发语言·mybatis
九皇叔叔8 小时前
【06】SpringBoot3 MybatisPlus 修改(Mapper)
java·spring boot·mybatis·mybatisplus
mc_故事与你9 小时前
前后端分离项目(springboot+vue+mybatis)-教学文档(SpringBoot3+Vue2)-4 (正在编写)
vue.js·spring boot·mybatis
秃头续命码农人9 小时前
谈谈对Spring、Spring MVC、SpringBoot、SpringCloud,Mybatis框架的理解
java·spring boot·spring·mvc·maven·mybatis
bekote19 小时前
mybatis-plus-generator模版
mybatis
cyforkk1 天前
MyBatis Plus 字段自动填充:生产级实现方案与原理分析
mybatis
九皇叔叔1 天前
【03】SpringBoot3 MybatisPlus BaseMapper 源码分析
java·开发语言·mybatis·mybatis plus
马猴烧酒.1 天前
【Mybatis出现bug】应为 <statement> 或 DELIMITER,得到 ‘id‘
java·bug·mybatis
一只程序熊1 天前
SpringBoot class java.lang.String cannot be cast to class java.lang.Long
spring·mybatis