mybatisplus分页总是查到全部数据?
这是因为版本的问题,在3.4.1及以上需要配置过滤器以实现分页
分页代码–>
@Test void contextLoads6() { PagepageB= new Page (2,2);//可以通过构造方法直接指定分页情况 System.out.println(pageB.getSize());//2 System.out.println(pageB.getCurrent());//2 System.out.println(pageB.getTotal());//0 // page.setSize(2); //可通过方法设置属性 Page page = deptService.page(pageB); System.out.println(page.getSize());//2 System.out.println(page.getCurrent());//2 System.out.println(page.getTotal());//0 List list = page.getRecords(); // list.forEach(System.out::println); for (Dept dept : list) { System.out.println(dept); } System.out.println("---------------"); Map map= new HashMap<>(); map.put("deptno",60); Page< Map > page1=new Page<>(2,2); Page
未配置过滤器,会查询到全部数据,并不做limit处理
配置过滤器—-
mybatisplus 3.4.1
@Configuration @MapperScan("com.qy.mapper") public class MyInterceptorConfig { @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false // paginationInterceptor.setOverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 // paginationInterceptor.setLimit(500); // 开启 count 的 join 优化,只针对部分 left join paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } }
mybatisplus 3.5.1
package com.qy.Config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Gavin */ @Configuration @MapperScan("com.gavin.mapper") public class MyInterceptorConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }