记录一下费了一些功夫的Mybatis复杂动态Sql

记录一下费了一些功夫的Mybatis动态Sql:

满足以下所有条件:

1.查询time_status 为1和2的数据在前面,为3的数据在后面
2.id 不在dsIds这个范围里的数据;
3.按照dead_line 降序
4.如果timeType=1则查询 start_time 在 startMonth 和 endMonth 之间的数据;timeType=2,则查询start_time大于endMonth的数据;
5.如果customerName不为空,则查询customer_name=customerName的数据;
6.origin='talents‘, enable=‘true’ ,status=1

Dao层:


    List<DualSelectEntity>  dsList(@Param("dsIds")List<Long> dsIds,
                                   @Param("type")Integer type,
                                   @Param("customerName")String customerName,
                                   @Param("timeType")int timeType,
                                   @Param("startMonth")long startMonth,
                                   @Param("endMonth")long endMonth);

xml:

 <select id="dsList" resultType="cn.com.newcapec.modules.jyb.entity.DualSelectEntity">
        (SELECT * FROM `j_dual_select`
        where origin='talents'
        and enable='true'
        and status=1
        <if test="dsIds != null and dsIds.size()>0">
            and id not in
            <foreach item="item" index="index" collection="dsIds" open="("  close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and time_status in(1,2)
        and type=#{type}
        <choose>
            <when test="timeType==1">
                and start_time BETWEEN #{startMonth}
                AND #{endMonth}
            </when>
            <when test="timeType==2">
                and start_time>#{endMonth}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="customerName!=null and customerName!=''">
            and customer_name=#{customerName}
        </if>
        ORDER BY dead_line desc)
        
        union
        
        (SELECT * FROM `j_dual_select`
        where origin='talents'
        and enable='true'
        and status=1
        <if test="dsIds != null and dsIds.size()>0">
            and id not in
            <foreach item="item" index="index" collection="dsIds" open="("  close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and time_status = 3
        and type=#{type}
        <choose>
            <when test="timeType==1">
                and start_time BETWEEN #{startMonth}
                AND #{endMonth}
            </when>
            <when test="timeType==2">
                and start_time>#{endMonth}
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="customerName!=null and customerName!=''">
            and customer_name=#{customerName}
        </if>
        ORDER BY dead_line desc)
        
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值