一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

springboot整合mybatis-plus实现分页查询功能代码示例

时间:2020-09-08 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下springboot整合mybatis-plus实现分页查询功能代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

建一个config类

@Configuration
public class MybatisPlusConfig {

  @Bean
  public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
  }
}

编写controller

 post /article/search/{page}/{size}
@PostMapping("search/{page}/{size}")
  public Result findByPage(@PathVariable Integer page,
               @PathVariable Integer size,
              @RequestBody Map map){

    //根据条件分页查询
    Page
pageDate = articleService.findByPage(map,page,size); //封装分页返回对象 PageResult
pageResult =new PageResult<>( pageDate.getTotal(),pageDate.getRecords() ); return new Result(true,StatusCode.OK,"查询分页成功",pageResult); }

编写service

public Page
findByPage(Map map, Integer page, Integer size) { //设置查询条件 EntityWrapper
wrapper =new EntityWrapper<>(); Set keySet = map.keySet(); for (String key : keySet) { // if (map.get(key) !=null){ // wrapper.eq(key,map.get(key)); // } wrapper.eq(map.get(key) !=null,key,map.get(key)); } //设置分页参数 Page
pageData =new Page<>(page,size); //第一个是分页参数,第二个是查询条件 List
list = articleDao.selectPage(pageData, wrapper); pageData.setRecords(list); return pageData; }

整合完成!!!

热门栏目