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)
上一篇 2022年6月26日 上午11:36
下一篇 2022年6月26日 上午11:36


相关推荐

  • Python中的join()函数

    Python中的join()函数原网址 https www geeksforgeek org join function pythonPython 中的 join 函数 join 是一个字符串方法 它返回被子字符串连接的字符串 语法 string name join iterable string name 这是被连接的子字符串 参数 Thejoin methodtakesj 方法需要可迭

    2026年3月19日
    2
  • C语言中int、long int、long long的区别

    C语言中int、long int、long long的区别1、关于int和longint(1)在VC下没有区别。两种类型均用4个字节存放数据。(2)VC是后出的编译器,之前有很多早期的C编译器,在早期编译器下longint占4个字节,int占2个字节。(3)之所以有“整型”和“长整形”两种不同类型,是C语言在诞生时发明者规定好的,前者存储的整数的值域小于后者。 这个问题不用牵肠挂肚,在VC下用谁都可以。

    2022年5月9日
    101
  • 详解数据存储芯片AT24C02的应用及编程

    详解数据存储芯片AT24C02的应用及编程一 芯片简介 AT24C02 是一个 2K 位串行 CMOSE2PROM 内部含有 256 个 8 位字节 采用先进 CMOS 技术实质上减少了器件的功耗 AT24C02 有一个 8 字节页写缓冲器 该器件通过 IIC 总线接口进行操作 有一个专门的写保护功能 二 芯片参数 1 特点工作电压 1 8V 5 5V 低功耗 CMOS 技术 工作电流 1mA 待机电流 1uA 应用在内部结构 128×8 1K 256×8 2K

    2026年3月20日
    2
  • 腾讯元宝怎么弄deepseep

    腾讯元宝怎么弄deepseep

    2026年3月12日
    2
  • 简述android触屏事件的处理_移动端touch事件有哪些

    简述android触屏事件的处理_移动端touch事件有哪些本文介绍了Android系统中触屏事件的相关知识,包括触屏事件的产生,分类,触屏事件序列,以及触屏事件在代码中的表示方式。了解这些内容,是理解Android触屏事件的分发,拦截和处理的基础。

    2025年10月19日
    4
  • Shell 字符串拼接的方法

    Shell 字符串拼接的方法value1 homevalue2 value1 echo value2 简单粗暴 居然直接怼在一起就好了 资料 1 LinuxShell 脚本中字符串的连接方法 https blog csdn net ysdaniel article details 请教 如何把两个字符串连接后赋给一个变量 http bbs chin

    2026年3月17日
    2

发表回复

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

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