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