1. 程式人生 > 程式設計 >springboot整合mybatis-plus 實現分頁查詢功能

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<String,Object> map){

    //根據條件分頁查詢
    Page<Article> pageDate = articleService.findByPage(map,page,size);
    //封裝分頁返回物件
    PageResult<Article> pageResult =new PageResult<>(
        pageDate.getTotal(),pageDate.getRecords()
    );

    return new Result(true,StatusCode.OK,"查詢分頁成功",pageResult);
  }

編寫service

public Page<Article> findByPage(Map<String,Object> map,Integer page,Integer size) {
    //設定查詢條件
    EntityWrapper<Article> wrapper =new EntityWrapper<>();
    Set<String> 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<Article> pageData =new Page<>(page,size);

    //第一個是分頁引數,第二個是查詢條件
    List<Article> list = articleDao.selectPage(pageData,wrapper);

    pageData.setRecords(list);

    return pageData;
  }

整合完成!!!

到此這篇關於springboot整合mybatis-plus 實現分頁查詢功能的文章就介紹到這了,更多相關mybatis-plus 分頁查詢內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!