@Select注解里添加动态sql语句

@Select注解里添加动态sql语句

在@Select注解中使用<script><if><where>等标签实现动态sql
注:为什么要使用这种方式,因为正常的SQL只能传参数,而不能直接拼接SQL语句,会报错。

@Select("<script>" +
            "SELECT pr.*,lr.scxh,lr.bzxh,lr.wpmc,lr.gg,lr.djp,lr.dyph,lr.bb,lr.bc,lr.jz from package_record pr LEFT JOIN line_record lr ON pr.line_id = lr.id" +
            "<where>"+
            "<if test='ph != null and ph != \"\"'> and pr.ph = #{ph}</if>" +
            "<if test='scxh != null and scxh != \"\"'> and lr.scxh = #{scxh}</if>" +
            "<if test='bb != null and bb != \"\"'> and lr.bb = #{bb}</if>" +
            "<if test='djp != null and djp != \"\"'> and lr.djp = #{djp}</if>" +
            "<if test='xcpd02dxbzdhStatus != null'> and pr.xcpd02dxbzdh_status = #{xcpd02dxbzdhStatus}</if>" +
            "<if test='bzjlhStatus != null'> and pr.bzjlh_status = #{bzjlhStatus}</if>" +
            "<if test='dateRangeStart != null and dateRangeStart != \"\"'> and pr.create_time &gt;= #{dateRangeStart}</if>" + // 大于>
            "<if test='dateRangeEnd != null and dateRangeEnd != \"\"'> and pr.create_time &lt;= #{dateRangeEnd}</if>" +  // 小于<
            "</where>"+
            "limit #{offset},#{limit}" +
            "</script>")
    List<PackageRecordVo> getList(long offset, long limit,
                                  String ph, String scxh, String bb, String djp, Integer xcpd02dxbzdhStatus, Integer bzjlhStatus, String dateRangeStart, String dateRangeEnd);

几个坑:

  1. <if>标签中的and不能大写;
  2. if后面的test,不是自己的字段名
  3. ><等要使用转义字符,&gt;&lt;对照如下:
原符号  :    <        <=      >       >=       &       '         "
替换符号:   &lt;     &lt;=   &gt;    &gt;=   &amp;   &apos;    &quot;
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值