软件编程
位置:首页>> 软件编程>> java编程>> Mybatis-Plus分页的使用与注意事项

Mybatis-Plus分页的使用与注意事项

作者:为了我的架构师  发布时间:2022-08-14 22:00:56 

标签:mybatis-plus,分页

1.写个Mybatis-plus配置类:

是通过 * 实现分页

@Configuration
public class MybatisConfig {
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor() {
       MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
       interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
       return interceptor;
   }
}

官网复制即可,只是你需要把数据库改为你使用的,这里我是使用mysql

Mybatis-Plus分页的使用与注意事项

2.写接口测试

很简单

@GetMapping("/test")
   public Response test(){
       Page<Produce> producePage = new Page<>(1,1);
       Page<Produce> page = produceService.page(producePage);
       System.out.println(producePage == page);
       List<Produce> records = page.getRecords();
       for (Produce record : records) {
           System.out.println(record);
       }
       return new Response<>(records, ResultEnum.SUCCESS);
   }

Mybatis-Plus分页的使用与注意事项

默认是会查询总条数,都有get、set方法,可以根据自己的需求设置(点开Page类看看)

Mybatis-Plus分页的使用与注意事项

3.注意

我们传入的page对象和查询返回的page对象是同一个

Mybatis-Plus分页的使用与注意事项

Mybatis-Plus分页的使用与注意事项

4.如果你还有查询条件

比如我们只查询id和price,id小于5的分页查询

Mybatis-Plus分页的使用与注意事项

1.Lambda表达式

@GetMapping("/test")
public Response test(){
   Page<Produce> producePage = new Page<>(1,2);
   Page<Produce> page = new LambdaQueryChainWrapper<>(produceService.getBaseMapper())
           .select(Produce::getPid,Produce::getPrice)
           .lt(Produce::getPid,5)
           .page(producePage);

return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}

Mybatis-Plus分页的使用与注意事项

2.普通查询

@GetMapping("/test")
public Response test(){
   Page<Produce> producePage = new Page<>(1,2);
   QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
   queryWrapper.select("pid","price");
   queryWrapper.lt("pid",5);
   Page<Produce> page = produceService.page(producePage, queryWrapper);
   return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
}

Mybatis-Plus分页的使用与注意事项

Mybatis-Plus分页的使用与注意事项

来源:https://blog.csdn.net/qq_42682745/article/details/121574682

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com