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}), '%')
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐