MybatisPlus自定义sql分页查询

MybatisPlus自定义sql分页查询mybatisplus自定义sql分页查询

大家好,又见面了,我是你们的朋友全栈君。

自定义sql分页的步骤

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

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

JAVA和xml文件如下:

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

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

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

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

    long pageListCount(@Param("param") WfPurchaseFrameParam param);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tongtech.biz.purchase.frame.dao.WfPurchaseFrameDao">

    <select id="pageList" parameterType="com.tongtech.biz.purchase.frame.model.dto.WfPurchaseFrameParam"
            resultType="com.tongtech.biz.purchase.frame.model.domain.WfPurchaseFrame">

        select t.*,inst.proc_inst_id processInstanceId
        from t_wf_purchase_frame t
        left join t_flow_inst inst on inst.business_id = t.apply_id
        where t.`status` = '0'
        and inst.proc_inst_status ='1'
        <if test="param.applyPerson != null and param.applyPerson != ''">
        and t.apply_person like concat('%',#{param.applyPerson},'%')
        </if>
        <if test="param.projectCode != null and param.projectCode != ''">
        and t.project_code like concat('%',#{param.projectCode},'%')
        </if>
        <if test="param.projectName != null and param.projectName != ''">
        and t.project_name like concat('%',#{param.projectName},'%')
        </if>
        limit #{param.page},#{param.limit}
    </select>


    <select id="myPageList" parameterType="com.tongtech.biz.purchase.frame.model.dto.WfPurchaseFrameParam" resultType="com.tongtech.biz.purchase.frame.model.domain.WfPurchaseFrame">

        select t.*,inst.proc_inst_id processInstanceId
        from t_wf_purchase_frame t
        left join t_flow_inst inst on inst.business_id = t.apply_id
        where t.`status` = '0'
        and inst.proc_inst_status ='1'
        <if test="param.applyPerson != null and param.applyPerson != ''">
            and t.apply_person like concat('%',#{param.applyPerson},'%')
        </if>
        <if test="param.projectCode != null and param.projectCode != ''">
            and t.project_code like concat('%',#{param.projectCode},'%')
        </if>
        <if test="param.projectName != null and param.projectName != ''">
            and t.project_name like concat('%',#{param.projectName},'%')
        </if>

    </select>

    <select id="pageListCount" parameterType="com.tongtech.biz.purchase.frame.model.dto.WfPurchaseFrameParam"
            resultType="long">

        select count(1)
        from t_wf_purchase_frame t
        left join t_flow_inst inst on inst.business_id = t.apply_id
        where t.`status` = '0'
        and inst.proc_inst_status ='1'
        <if test="param.applyPerson != null and param.applyPerson != ''">
            and t.apply_person like concat('%',#{param.applyPerson},'%')
        </if>
        <if test="param.projectCode != null and param.projectCode != ''">
            and t.project_code like concat('%',#{param.projectCode},'%')
        </if>
        <if test="param.projectName != null and param.projectName != ''">
            and t.project_name like concat('%',#{param.projectName},'%')
        </if>
    </select>

</mapper>

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

@PostMapping(value = "/pageList")
public  BaseResponse<BaseResponseList<WfPurchaseFrame>> pageList(@RequestBody WfPurchaseFrameParam param){
   BaseResponse<BaseResponseList<WfPurchaseFrame>>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<WfPurchaseFrame> resultPage=new Page<>(page,limit);
   // 这里不能使用QueryWrapper 来传递自定义参数
   QueryWrapper<WfPurchaseFrame> queryWrapper=this.createQuery(param);
   IPage<WfPurchaseFrame> resultList= wfPurchaseFrameService.myPageList(resultPage,param);
   BaseResponseList<WfPurchaseFrame> baseResponseList=new BaseResponseList<>();
   baseResponseList.setData(resultList.getRecords());
   baseResponseList.setTotal(resultList.getTotal());
   baseResponse.setData(baseResponseList);
   return baseResponse;
}

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153259.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • layoutparams方法_LAYOUT

    layoutparams方法_LAYOUT沙翁向昨天要经验;向今天要结果;向明天要动力Android开发:LayoutParams的用法LayoutParams继承于Android.View.ViewGroup.LayoutParams.    LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout

    2022年9月21日
  • JS实现继承的几种方法总结

    JS实现继承的几种方法总结首先定义一个父类://构造函数functionAnimal(name){ this.name=name||’Animal’; this.sleep=function(){ console.log(this.name+’正在睡觉!’); };}//原型上面的方法:Animal.prototype.eat=function(food){ console…

    2022年7月22日
  • Nginx + Tomcat 搭建负载均衡

    Nginx + Tomcat 搭建负载均衡Nginx + Tomcat 搭建负载均衡

    2022年4月22日
  • java的三种代码注释方式「建议收藏」

    java的三种代码注释方式「建议收藏」1.注释的作用注释是对程序语言的说明,有助于开发者之间的交流,方便理解和维护程序。注释不是编程语句,不会被编译器执行。一些代码量少较少的程序,我们加不加注释对我们的理解和修改代码没有太大影响;如果是淘宝;抖音等那种中大型程序,没有了代码注释,对于日后程序运行维护将是很大困难。我们可以在平时编码过程中养成规范代码注释习惯,也是我们成为优秀程序员的原因2.java中的注释分类Java中的注释分为以下三种:单行注释;多行注释;文档注释1.单行注释java代码中使用双斜杠单行注释//,一般单行注释,注释少

    2022年7月7日
  • python测试框架unittest如何设置用例优先级_python 的 unittest 测试框架中的测试依赖怎么解决呢…[通俗易懂]

    python测试框架unittest如何设置用例优先级_python 的 unittest 测试框架中的测试依赖怎么解决呢…[通俗易懂]好啦,问题已解决,谢谢各位啦,感谢各位提出的思路,感谢@xiaoj(杰)和@keke在这里做个总结:xiaoj(杰)的方法defskipTest(value):defdeco(function):defwrapper(self,*args,**kwargs):ifnotgetattr(self,value):self.skipTest(‘跳过用例’)else:functio…

    2022年8月31日
  • windows 10安装sqlyog详细步骤

    windows 10安装sqlyog详细步骤sqlyog下载链接:链接:https://pan.baidu.com/s/1D_iRna8V90omfHsKHyeBtg提取码:bqht复制这段内容后打开百度网盘手机App,操作更方便哦1.下载完以后解压,双击SQLyog-12.0.9-0.x642.如下图,选择Ok3.如图,点击下一步4.选择“我接受“许可证协议”中的条款(A)”单击下一步…

    2022年5月28日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号