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}), '%')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章