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


相关推荐

  • c# combobox属性dropdownlist赋值

    c# combobox属性dropdownlist赋值combobox属性设置为dropdownlist后,text赋值如果combobox的内容里有则显示,没有则不显示。 即如果combobox1包含ABCD,赋值A则可以显示,F则不行。

    2022年10月10日
    0
  • 解决Python扩展: Unable to find vcvarsall.bat[通俗易懂]

    解决Python扩展: Unable to find vcvarsall.bat[通俗易懂]安装mingw,以我的安装为例:我的mingw安装在C:\MinGW,python安装在C:\Python26然后将以下目录加入系统环境变量C:\MinGW\bin;C:\MinGW\msys\1.0;C:\MinGW\mingw32;C:\MinGW\mingw32\bin;C:\MinGW\msys\1.0\bin然后在目录C:\Python26\Lib\di…

    2022年5月29日
    40
  • 搭建Git本地服务器

    搭建Git本地服务器

    2021年10月12日
    54
  • golang coredump分析「建议收藏」

    背景最近在分析golang的一个内存泄漏问题。一般来讲,使用golang自带的pprof工具就可以分析内存的使用,协程情况,是否有block等情况。但是我们项目中调用了C库,导致C库的一些东西没法通过pprof来进行监控分析。实际上通过pprof来监控程序的话,内存是稳定的,但是占用Linux的内存是一直增长的,即RES一直增长,实际上程序是有泄漏的。怀疑是使用C库导致,所以通过coredump…

    2022年4月12日
    249
  • 【SQL】SQL中distinct的用法

    【SQL】SQL中distinct的用法转载自:https://www.cnblogs.com/leonlee/p/6042461.html1.作用于单列2.作用于多列3.COUNT统计4.distinct必须放在开头5.其他在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词distinct用于返回唯一不同的值。表A:表B:1.作用于

    2022年7月20日
    19
  • C#中如何遍历ArrayList

    C#中如何遍历ArrayList 前言:ArrayList是非常方便的动态数组,在使用ArrayList时经常会遇到一些问题,码了一些百度文库查找到的资料以及例子,希望可以帮助大家在需要时方便查找。1、什么是ArrayListArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:     &lt;1&gt;动态的增加和减少元素     &…

    2022年7月22日
    4

发表回复

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

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