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


相关推荐

  • python fabric实现远程操作和部署

    python fabric实现远程操作和部署

    2021年12月4日
    44
  • 加密网站地址——从根本解决盗连

    加密网站地址——从根本解决盗连这是我在CSDN上的一篇远程文章《加密网站地址——从根本解决盗连》网站防止图片等内容盗链的方法多种多样,其中最常用的方法就是通过HTTP访问头信息,判断访问来源。理解TCP/IP通信员里的读者都知道,由于HTTP头信息处理是在OSI模型的应用层,所以,编造一些假的HTTP头信息发送给服务器,并不需要什么特殊的技术或者工具。下面的演示中,是利用wget命令模拟Fire…

    2022年6月13日
    36
  • 安装java编译器

    安装java编译器安装JDK。参考:https://www.cnblogs.com/mr-wuxiansheng/p/6850437.html1.官网下载JavaSEDevelopmentKit13.0.1(由于是访问国外网站,所以会比较慢。)最好下载EXE版本的,这样什么都不用管,点安装就行。https://www.oracle.com/technetwork/java/javase/…

    2022年6月7日
    35
  • EVE模拟器的使用-带图超详细(学网络用)「建议收藏」

    EVE模拟器的使用-带图超详细(学网络用)「建议收藏」文章目录EVE模拟器的安装EVE模拟器的使用登陆添加一个实验退出一个实验实验分类创建任务(添加路由交换机)选择型号和种类说明修改已创建种类参数界面放大和缩小方式一方式二种类(设备)之间连线和删除连线连线删线添加注释信息(描述)设备开机开启单个设备开启所有设备设备功能配置软件抓包EVE模拟器的安装EVE模拟器的下载及安装见:EVE模拟器的使用登陆根据上面安装步骤进入到这个界面以后:用户名:admin密码:unl(小写L)登陆方式:Nativeconsole登陆成功以后就是这个界面:添

    2022年4月27日
    485
  • I/O模型与Nginx详细配置、优化

    I/O模型与Nginx详细配置、优化

    2021年9月7日
    70
  • pycharm入门教程(非常详细)_php网站部署教程

    pycharm入门教程(非常详细)_php网站部署教程最全Pycharm教程(1)——定制外观  最全Pycharm教程(2)——代码风格  最全Pycharm教程(3)——代码的调试、运行  最全Pycharm教程(4)——有关Python解释器的相关配置  最全Pycharm教程(5)——Python快捷键相关设置  最全Pycharm教程(6)——将Pycharm作为Vim编辑器使用  最全Pyc

    2022年8月29日
    3

发表回复

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

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