mybaits-plus分页查询的步骤
版本:3.5.1
1. 引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
2.配置分页插件
这里将@MapperScan写在了配置类上,也可写在启动类
@Configuration
@EnableTransactionManagement
@MapperScan("com.zhku.pigsale.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
3. 编写分页查询方法测试
这里只提供Controller的方法
//分页测试
@GetMapping("test")
public Map getPageList(){
Page<Customer> page = customerService.pagetest();
HashMap<String, Object> map = new HashMap<>();
long pages = page.getPages();
List<Customer> records = page.getRecords();
long current1 = page.getCurrent();
long total = page.getTotal();
long size = page.getSize();
map.put("records",records);
map.put("total",total);
map.put("size",size);
System.out.println("=================================");
System.out.println("page:"+ page);
System.out.println("records:"+records);
return map;
}
4. 编写mapper、entity等业务类,以及application配置信息
5. 去调用该分页查询的url,就能知道是否配置成功(按以上方式配置一般是没问题的)
分页查询失效原因分析
1.对上述流程进行检查,确保没问题;
2.若是分页插件config配置在了别的模块(common)中,然后在service中调用的要注意:
common的外层路径最好更service的外层路径相同
service:
common:
其次,要在Service启动类加上包扫描路径!!!(很大原因是springboot没扫描到common中配置)
(并且若是其他的配置也失效了,很大原因也是如此)