Mybatis in條件傳參的三種實(shí)現(xiàn)方式(直接$,List,[])
第一種方法
in 條件為拼接好的字符串
如果直接傳入拼接好的where in 條件, 比如('111','222','333'),則需要使用${idlist}傳參,即絕對引用,而不能使用#
如果使用#傳參會被mybatis當(dāng)成字符串再添加一層''引號,導(dǎo)致錯誤.
- 優(yōu)點(diǎn):簡單方便,高效
- 缺點(diǎn):不能防止SQL注入
第二種方法
in 條件為List對象
in條件直接傳入List對象,讓mybatis再去拼接生成in條件,這個(gè)很麻煩,但是可以防止SQL注入
第三種方法
in 條件為String[] 數(shù)組
in條件直接傳入[]數(shù)組對象,讓mybatis再去拼接生成in條件,這個(gè)很麻煩,但是可以防止SQL注入
如果項(xiàng)目大,其實(shí)可以同時(shí)重載三種都實(shí)現(xiàn),我一般都會這樣,實(shí)現(xiàn)三種DAO接口,service層相同方法名,根據(jù)不同的模塊不同的需求調(diào)用不同的實(shí)現(xiàn)層
Service:
int deleteMenuByIdList(String idlist,int delcount,int lastsort);
int deleteMenuByIdList(List<String> idlist, int delcount,int lastsort);
int deleteMenuByIdList(String[] idlist, int delcount,int lastsort);Dao:
//用這種寫法方便,idlist直接拼接好,xml中用 in ${idlist}接受參數(shù)
int deleteMenuByIdList(@Param("idlist")String idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);
//用這種寫法直接傳List對象,xml中再寫循環(huán)拼接,麻煩
int deleteMenuByIdList2(@Param("idlist")List<String> idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);
//用這種寫法直接傳String[]數(shù)組,xml中再寫循環(huán)拼接,麻煩
int deleteMenuByIdList3(@Param("idlist")String[] idlist, @Param("delcount")int delcount, @Param("lastsort")int lastsort);(2,3)的xml文件中不需要做修改,只需要修改一下id對應(yīng)到DAO的方法名即可。
mappper.xml
1,
<delete id="deleteMenuByIdList" >
delete from s_menu where menu_id in ${idlist};
update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in ${idlist};
</delete>
2,
<delete id="deleteMenuByIdList2" >
delete from s_menu where menu_id in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>
;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>;
</delete>
3,
<delete id="deleteMenuByIdList3" >
delete from s_menu where menu_id in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>
;update s_menu set sort=sort-#{delcount} where sort >= #{lastsort} and menu_id not in
<foreach collection="idlist" item="menu_id" separator="," open="(" close=")">
#{menu_id}
</foreach>;
</delete>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java微信公眾平臺開發(fā)(4) 回復(fù)消息的分類及實(shí)體的創(chuàng)建
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第四步,回復(fù)消息的分類及實(shí)體的創(chuàng)建,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Springboot @Transactional使用時(shí)需注意的幾個(gè)問題記錄
本文詳細(xì)介紹了Spring Boot中使用`@Transactional`注解進(jìn)行事務(wù)管理的多個(gè)方面,包括事務(wù)的隔離級別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個(gè)類中調(diào)用事務(wù)方法時(shí)可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01
SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解
這篇文章主要介紹了SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解,Aspect(切面)通常是一個(gè)類,里面可以定義切入點(diǎn)和通知(切面 = 切點(diǎn)+通知),execution()是最常用的切點(diǎn)函數(shù),需要的朋友可以參考下2024-01-01
關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴
這篇文章主要介紹了關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解Java8的forEach(...)如何提供index值
這篇文章主要介紹了詳解Java8的forEach(...)如何提供index值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
java組件commons-fileupload實(shí)現(xiàn)文件上傳
這篇文章主要介紹了java借助commons-fileupload組件實(shí)現(xiàn)文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

