SpringBoot使用MybatisPlus分页插件

MybatisPlus3.4.1版本

1、配置分页插件

 1 @Configuration
 2 public class MybatisPlusConfig {
 3 
 4     /*    旧版本配置
 5     @Bean
 6     public PaginationInterceptor paginationInterceptor(){
 7         return new PaginationInterceptor();
 8     }*/
 9 
10     /**
11      * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置
12      * MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
13      */
14     @Bean
15     public MybatisPlusInterceptor mybatisPlusInterceptor() {
16         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
17         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
18         return interceptor;
19     }
20 
21     @Bean
22     public ConfigurationCustomizer configurationCustomizer() {
23         return configuration -> configuration.setUseDeprecatedExecutor(false);
24     }
25 }

 

2、添加@MapperScan注解,扫描mapper接口

1 @SpringBootApplication
2 @MapperScan("com.donleo.mp.mapper")
3 public class MybatisPlusApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(MybatisPlusApplication.class, args);
7     }
8 
9 }

 

3、测试

 1  /**
 2      * MybatisPlus分页插件使用
 3      */
 4     @Test
 5     void TestPagination(){
 6         Page<User> userPage = new Page<User>(1,2);
 7         Page<User> pageList = userMapper.selectPage(userPage, new QueryWrapper<User>()
 8                 .eq("nick_name", "xx")
 9         );
10         long current = pageList.getCurrent();
11         long size = pageList.getSize();
12         long total = pageList.getTotal();
13 
14         System.out.println("当前页:"+current);
15         System.out.println("一页显示几条:"+size);
16         System.out.println("总条数:"+total);
17         System.out.println("查询数据:"+pageList.getRecords());
18     }
查询结果:

 

控制台打印结果:

 

 

posted @ 2020-12-09 18:35  donleo123  阅读(3298)  评论(0编辑  收藏  举报