MybatisPlus自定义sql分页查询

2023-05-16

自定义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关键字,并且条件构建时不会带上表别名

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MybatisPlus自定义sql分页查询 的相关文章

随机推荐

  • 东北大学秦皇岛分校通信工程中外合作2020级C语言实验6

    1 定义结构体类型 xff0c 包括候选人名和选票两个成员 xff0c 编程实现对候选人得票的统计 1 Write a C program that implements the statistics of the candidate vo
  • C/CPP三种排序算法

    一 简单选择排序 span class token keyword void span span class token function sort span span class token punctuation span span c
  • ACLGUI IN SSTC(PIA)2020中可能遇到的一些知识点

    文章目录 xff08 一 xff09 条件编译 xff08 二 xff09 部分头文件 xff08 三 xff09 空指针具体操作示例常见问题1 xff1a 空指针指向了内存的什么地方 xff1f 常见问题2 xff1a 在实际的操作中 x
  • 计算机网络基础(一)概述

    计算机网络是一组自治 xff08 拥有独立的计算能力 xff09 计算机互联的集合 IEEE高级委员会 坦尼鲍姆 本文参考书目为 计算机网络 xff08 第七版 xff09 xff08 谢希仁 xff09 书中为方便 xff0c 将计算机网
  • vultr购置配置在线kali

    vultr购置配置kali 购买 这里使用vultr可能需要一个小小的 xff0c 反正我没有 是上不去得 xff0c 大家这里看自己 xff0c 注册好账号我们需要重置 xff0c 这里我们可以选择支付宝进行充值 选择好充值得费用就可以了
  • 华为服务器装CentOS 7系统

    参考文章 https blog csdn net weixin 43897572 article details 98513207 用网线插入服务器网口 xff0c 使用kvm客户端或者浏览器 记录一下华为服务器的默认密码 有进主板的密码
  • c++重学笔记21 - 类型选择器

    喜欢这篇文章吗 xff1f 喜欢的话去看博主的置顶博客 xff0c 即可依据分类找到此文章的原版得到更好的体验 xff0c 图片及代码显示的问题 xff0c 笔者深感抱歉 xff0c 想要更好的体验去原博文即可 title c 43 43
  • Ubuntu 20 安装包下载(清华镜像)

    Ubuntu 20 安装包下载 在国内推荐使用清华大学镜像 清华镜像地址 xff1a https mirrors tuna tsinghua edu cn 在搜索框中输入Ubuntu xff0c 然后点击Ubuntu release xff
  • 今日arXiv精选 | ICCV 2021/CIKM 2021/ACM MM 2021

    关于 今日arXiv精选 这是 AI 学术前沿 旗下的一档栏目 xff0c 编辑将每日从arXiv中精选高质量论文 xff0c 推送给读者 SUNet Symmetric Undistortion Network for Rolling S
  • 在Windows上面安装WSL以使用Linux

    在Windows上面安装WSL以使用Linux 0 WSL xff08 Windows Subsystem for Linux xff09 1 安装Ubuntu步骤1 1 检查Windows版本1 2 激活WSL服务1 3 安装Ubuntu
  • Armbian更新国内软件源|N1盒子复活

    N1刷armbian更新apt xff0c 有的源里缺少很多东西 xff0c 尤其是阿里华为这种源 xff0c arm架构的却少了很多 xff0c 谨慎换源 xff01 xff01 xff01 nano etc apt sources li
  • Qt报错:XXX does not name a type,及解决办法

    一 错误 Qt报错 xff1a XXX does not name a type 二 报错原因 在两个类的头文件中 xff0c 相互引用了对方的头文件 例如 xff1a a h include 34 b h 34 class AClass
  • 成功解决AttributeError: ‘str‘ object has no attribute ‘decode‘

    成功解决AttributeError 39 str 39 object has no attribute 39 decode 39 目录 解决问题 解决思路 解决方法 T1 直接去掉 T2 众多网友好评的建议 解决问题 AttributeE
  • 很实用的latex常用计算符

    本文仅供学习参考使用 xff0c 一切版权和解释权均归原作者所有 xff0c 转载地址 xff1a http blog csdn net garfielder007 article details 51646604 数学符号详细内容见 xf
  • R语言实战——距离判别、贝叶斯判别、Fisher判别理论详细推导与R语言实现

    文章目录 前言1 距离判别1 1 双群体1 1 1 理论推导1 1 2 R语言实现1 1 3 实例分析 1 2 多群体1 2 1 理论推导1 2 2 R语言实现1 2 3 实例分析 2 贝叶斯判别2 1 双群体2 1 1 理论推导2 1 2
  • R语言实战——主成分分析理论推导与R语言实现

    目录 1 总体主成分1 1 主成分的定义与导出1 2 主成分的性质1 3 从相关矩阵出发求主成分 2 样本主成分2 1 从S出发求主成分2 2 从R出发求主成分 3 相关的R函数以及实例3 1 96 princomp 96 函数3 2 96
  • GM(1,1)灰色预测及相关检验指标的MATLAB实现

    本篇文章的代码实现了以下三大方面的功能 xff1a 一 计算级比和光滑比并做级比检验 xff1b 二 序列的灰色预测 xff1b 三 精度检验 xff0c 主要做了以下内容 xff1a 相对残差Q检验 xff08 MAPE xff09 xf
  • R语言实战——ROC曲线的绘制

    前言 xff1a 以前使用Matlab绘制ROC曲线常常是工具箱有就画 xff0c 没有就不画 xff0c 而且在想画的时候工具箱恰恰就没有 xff0c 很纳闷 然后无意间发现了一篇用R语言绘制ROC曲线的文章 xff0c 赶紧学了并分享出
  • 含指数函数的不定积分方法归纳

    本篇博客参照了河北大学数计学院时坚所著的 含指数函数的不定积分方法归纳 xff0c 并在其基础上做了拓展 不定积分为数学分析中一类重要的内容 xff0c 其积分技巧和方法在几百年来一步步得到深入研究和探索 而含指数函数的不定积分为积分学中一
  • MybatisPlus自定义sql分页查询

    自定义sql分页的步骤 Dao层定义查询接口 xff0c 第一个参数必须为分页的参数Ipage xff0c 后面可带其他参数作为传入参数定义自定义查询sql 网上很多博客里面写的多表sql分页查询没带参数 xff0c 这里给一个带参数的列子