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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux load average,Linux Load Average详解

    linux load average,Linux Load Average详解定义在Linux,以及其他类Unix的系统中,常用该系统正在进行的运算工作来衡量该系统的系统负荷(SystemLoad)。一个完全空闲的系统,它的负荷(SystemLoad)标记为0;每一个正在运行或者正在等待CPU资源的进程,会导致平均负荷(SystemLoad)加1。所以,如果一个系统的负荷是4,就是说有4个进程正在使用,或者正在等待CPU资源。因为系统负荷(SystemLoad)是…

    2022年7月17日
    17
  • hadoop 资源[通俗易懂]

    hadoop 资源[通俗易懂]1)Cygwin相关资料  (1)Cygwin上安装、启动ssh服务失败、sshlocalhost失败的解决方案  地址:http://blog.163.com/pwcrab/blog/static/16990382220107267443810/  (2)windows2003+cygwin+ssh  地址:http://wenku.baidu.com/view/3777b…

    2022年5月21日
    34
  • 常用端口列表[通俗易懂]

    常用端口列表[通俗易懂]常见端口0|无效端口,通常用于分析操作系统1|传输控制协议端口服务多路开关选择器2|管理实用程序3|压缩进程5|远程作业登录7|回显9|丢弃11|在线用户13|时间17|每日引用18|消息发送协议19|字符发生器20|FTP文件传输协议(默认数据口)21|FTP文件传输协议(控制)22|SSH远程登录协议23|telnet(终端仿真协议),木马TinyTelnetServer开放此端口24|预留给个人用邮件系统25|SMTP服务器所…

    2025年11月3日
    5
  • Laravel Exceptions——异常与错误处理「建议收藏」

    Laravel Exceptions——异常与错误处理

    2022年2月14日
    45
  • 搜索引擎技术之概要预览

    搜索引擎技术之概要预览

    2021年11月16日
    56
  • 【小5聊】C#基础之Response.ContentType响应内容类型[通俗易懂]

    【小5聊】C#基础之Response.ContentType响应内容类型[通俗易懂]Response.ContentType1、文本格式:html、xml、txtResponse.ContentType=”text/html”;Response.ContentType=”text/xml”;Response.ContentType=”text/plain”;2、图片格式Response.ContentType=”image/jpg”;…

    2022年7月19日
    29

发表回复

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

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