网站宣传的手段有哪些seo中国
问题概述
SpringBoot中使用mybatis-plus实现分页查询时,提供一个page分页对象和一个QueryWrapper条件类对象,在使用Service.page(page,queryWrapper)方法进行分页查询时,发现并未查询到分页的结果,反而是查询到全部符合条件的结果。
public List<User> getOrdinaryUser() {//创建page分页对象Page page=new Page(1,3);//查询身份代码为1的普通用户QueryWrapper queryWrapper=new QueryWrapper<>().eq("identity","1");IPage page1 = this.page(page, queryWrapper);System.out.println("查询的结果:"+page1.getRecords());return page1.getRecords();}
发现其sql语句也是未添加limit
解决方法
在Springboot中,若是要使用mybatis-plus实现查询分页,首先需要配置一个分页配置类即可,配置之后即可实现分页查询。
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbTypereturn interceptor;}
}
若还未分页成功,则可以原因之一是数据库中没有数据,也会导致sql语句中不出现limit,为此在实现分页查询的功能时,切要添加测试数据到数据库中。
这就是springboot使用mybatis-plus进行分页查询失败的原因之一。