mybatis-sql实战总结

动态条件查询

动态条件查询总结
注意点:
1.可以用where 1=1 也可以用 防止出现后面没有条件的情况
2.模糊查询用concat拼接
A.SSTRING10 like concat(#{adAccEntrie.sstring10},‘%’)
3. 对于前端传来的日期参数,使用STR_TO_DATE来转为统一的格式
STR_TO_DATE( #{adAccEntrie.date01}, ‘%Y-%m-%d’)

4.对于String类型的字段,不但要判断是不是null,还要判断是不是空字符串””.
对于集合类型的字段,如List,不但要判断是不是null,还需要判断是不是空的集合,用size判断就行。
对于Date类型的字段,只需要判断是不是null就行了。

//String类型怎么判断拼接
<if test="adAccEntrie.sstring08 != null and adAccEntrie.sstring08 != ''">
                AND A.SSTRING08 = #{adAccEntrie.sstring08}<!--来源系统-->
</if>
//集合类型字段判断怎么拼接                   
<if test="ids != null and ids.size() > 0">
  AND ID IN
  <foreach collection="ids" item="id" open="(" close=")" separator=",">
    #{id}
  </foreach>
</if>
//Date类型字段判断怎么拼接
<if test="adAccEntrie.date01 != null and adAccEntrie.date02  != null">
                AND A.DATE01 BETWEEN STR_TO_DATE( #{adAccEntrie.date01}, '%Y-%m-%d') AND STR_TO_DATE( #{adAccEntrie.date02}, '%Y-%m-%d' )<!--记账日期-->
            </if>

5.如果只查一个表 select *就足够了,但是如果有多个表select A.*就能查出指定表的所有字段了。

<!--    账户明细查询-->
    <select id="getAccEntrieListByParam" resultMap="AdAccEntrieMap">
        SELECT A.*
        FROM Ad_Acc_Entrie A
        <where>
            <if test="adAccEntrie.sstring08 != null and adAccEntrie.sstring08 != ''">
                AND A.SSTRING08 = #{adAccEntrie.sstring08}<!--来源系统-->
            </if>
            <if test="adAccEntrie.date01 != null and adAccEntrie.date02  != null">
                AND A.DATE01 BETWEEN STR_TO_DATE( #{adAccEntrie.date01}, '%Y-%m-%d') AND STR_TO_DATE( #{adAccEntrie.date02}, '%Y-%m-%d' )<!--记账日期-->
            </if>
            <if test="adAccEntrie.sstring01 != null and adAccEntrie.sstring01 != ''">
                AND A.SSTRING01 = #{adAccEntrie.sstring01}<!--借贷标识-->
            </if>
            <if test="adAccEntrie.mstring11 != null and adAccEntrie.mstring11 != ''">
                AND A.MSTRING11 = #{adAccEntrie.mstring11}<!--管理机构-->
            </if>
            <if test="adAccEntrie.sstring10 != null and adAccEntrie.sstring10 !=''">
                AND A.SSTRING10 like concat(#{adAccEntrie.sstring10},'%')<!--业务项目类型-->
            </if>
            <if test="adAccEntrie.lstring01 != null and adAccEntrie.lstring01 !=''">
                AND A.LSTRING01 like concat(#{adAccEntrie.lstring01},'%')<!--凭证类别-->
            </if>
            <if test="adAccEntrie.mstring07 != null and adAccEntrie.mstring07 != ''">
                AND A.MSTRING07 = #{adAccEntrie.mstring07}<!--业务号码-->
            </if>
            <if test="adAccEntrie.sstring17 != null and adAccEntrie.sstring17 != ''">
                AND A.SSTRING17 = #{adAccEntrie.sstring17}<!--业务场景编码-->
            </if>
            <if test="adAccEntrie.mstring02 != null and adAccEntrie.mstring02 != ''">
                AND A.MSTRING02 = #{adAccEntrie.mstring02}<!--科目代码-->
            </if>
        </where>

        limit #{ph.page},#{ph.rows}
        
    </select>

怎么List遍历集合进行拼接?

最终会拼接成 (元素1,元素2,元素3,元素4,元素5,…,元素n)

<if test="ids != null and ids.size() > 0">
  AND ID IN
  <foreach collection="ids" item="id" open="(" close=")" separator=",">
    #{id}
  </foreach>
</if>
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值