MybatisPlus自定义sql分页查询

MybatisPlus自定义sql分页查询,第1张

自定义sql分页的步骤

Dao层定义查询接口,第一个参数必须为分页的参数Ipage,后面可带其他参数作为传入参数定义自定义查询sql

网上很多博客里面写的多表sql分页查询没带参数,这里给一个带参数的列子

JAVA和xml文件如下:

myPageList为使用mybatisPlus写的,pageList和pageListCount为原始写法

可以看出myPageList跟pageListsql语句一模一样,只是第一个参数必须为Ipage

public interface WfPurchaseFrameDao extends SuperDao
{
    List pageList(@Param("param") WfPurchaseFrameParam param);

    IPage myPageList(IPage page ,@Param("param") WfPurchaseFrameParam param);

    long pageListCount(@Param("param") WfPurchaseFrameParam param);
}




    


    

    

调用 controller层 【省略Service部分】:这里只需要在调用时传递一个Page(long current, long size)即可

@PostMapping(value = "/pageList")
public  BaseResponse> pageList(@RequestBody WfPurchaseFrameParam param){
   BaseResponse>baseResponse=new BaseResponse<>();
   Long page=
         StringHelper.isEmpty(param.getPage())?BizConstants.PAGE:Long.valueOf(param.getPage());
   Long limit=
         StringHelper.isEmpty(param.getLimit())?BizConstants.LIMIT:Long.valueOf(param.getLimit());
   Page resultPage=new Page<>(page,limit);
   // 这里不能使用QueryWrapper 来传递自定义参数
   QueryWrapper queryWrapper=this.createQuery(param);
   IPage resultList= wfPurchaseFrameService.myPageList(resultPage,param);
   BaseResponseList baseResponseList=new BaseResponseList<>();
   baseResponseList.setData(resultList.getRecords());
   baseResponseList.setTotal(resultList.getTotal());
   baseResponse.setData(baseResponseList);
   return baseResponse;
}

上面代码中说了不能使用wrapper查询条件构造器,原因为什么呢

${ew.customSqlSegment} 解析时会自动在前面带上where关键字,并且条件构建时不会带上表别名

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/991335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存