mybatis IN 方法, 字符串集合参数处理方式 in(?,?,?) 及 逗号分隔的字符串参数处理方法 and(a=? or a=? or a=?)

mapper.xml

1、IN 方法, 字符串集合参数处理方式 in(’?’ ,’?’ ,’?’)

入参数 timeList —> List 集合 -->
参数为:
索引0= ‘2020-06-09’
索引1= ‘2020-06-10’

   where day_time in
   <foreach collection="timeList" item="item" index="index" open="(" close=")" separator=",">
                #{item, jdbcType=VARCHAR}
    </foreach>

处理后sql为 :

WHERE day_time in ( '2020-06-09' , '2020-06-10' )

2、逗号分隔的字符串参数处理方法 and(a=’?’ or a=’?’ or a=’?)

入参:facilities=" 1,2,3" —> CONCAT(CONCAT('%','1'), '%') 是为了处理模糊查询

   <if test="facilities != null">
            <foreach item="item" index="index" collection="facilities.split(',')" open="and(1 != 1 " separator=""
                     close=")">
                or hr.facilities like CONCAT(CONCAT('%',#{item}), '%')
                or h.facilities like CONCAT(CONCAT('%',#{item}), '%')
            </foreach>
        </if>

处理后sql为 :

and(
1 != 1 
or h.facilities like CONCAT(CONCAT('%','1'), '%') 
or h.facilities like CONCAT(CONCAT('%','2'), '%') 
or h.facilities like CONCAT(CONCAT('%','3'), '%') 
)

3、特殊符号处理

在mapper 中某些符号是不可用的,如: >, <

 <![CDATA[
             and hr.guard_num >= #{guardNum}
   ]]>

4、模糊查询处理

在mapper 中是不是直接输入 % 号的,需要CONCAT方法处理

  h.city like CONCAT(CONCAT('%',#{search}), '%')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值