mybatis使用foreach語句實現(xiàn)IN查詢(三種)
foreach語句中, collection屬性的參數(shù)類型可以使:List、數(shù)組、map集合
collection: 必須跟mapper.java中@Param標(biāo)簽指定的元素名一樣
item : 表示在迭代過程中每一個元素的別名,可以隨便起名,但是必須跟元素中的#{}里面的名稱一樣。
- index :表示在迭代過程中每次迭代到的位置(下標(biāo))
- open :前綴, sql語句中集合都必須用小括號()括起來
close :后綴
- separator :分隔符,表示迭代時每個元素之間以什么分隔
Mybatis多條件查詢使用IN語句查詢foreach使用方式
#{}是預(yù)編譯處理,KaTeX parse error: Expected 'EOF', got '#' at position 20: …符串替換。mybatis在處理#̲{}時,會將sql中的#{}替…{}時,就是把${}替換成變量的值。使用#{}可以有效的防止SQL注入,提高系統(tǒng)安全性。
例如:
# 是將傳入的值當(dāng)做字符串的形式,eg:select id,name,age from student where id =#{id},當(dāng)前端把id值1,傳入到后臺的時候,就相當(dāng)于 select id,name,age from student where id =‘1'.
$ 是將傳入的數(shù)據(jù)直接顯示生成sql語句,eg:select id,name,age from student where id =${id},當(dāng)前端把id值1,傳入到后臺的時候,就相當(dāng)于 select id,name,age from student where id = 1.
(1)$ 符號一般用來當(dāng)作占位符,常使用Linux腳本的人應(yīng)該對此有更深的體會吧。既然是占位符,當(dāng)然就是被用來替換的。知道了這點就能很容易區(qū)分$和#,從而不容易記錯了。
(2)預(yù)編譯的機制。預(yù)編譯是提前對SQL語句進(jìn)行預(yù)編譯,而其后注入的參數(shù)將不會再進(jìn)行SQL編譯。我們知道,SQL注入是發(fā)生在編譯的過程中,因為惡意注入了某些特殊字符,最后被編譯成了惡意的執(zhí)行操作。而預(yù)編譯機制則可以很好的防止SQL注入。
select * from HealthCoupon where useType in ( '4' , '3' )
其中useType=“2,3”;這樣的寫法,看似很簡單,但是MyBatis不支持。。但是MyBatis中提供了foreach語句實現(xiàn)IN查詢
正確的寫法有以下幾種寫法:
(一)、selectByIdSet(List idList)
List<User> selectByIdSet(List idList); <select id="selectByIdSet" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> from t_user WHERE id IN <foreach collection="list" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </select>
(二)、List selectByIdSet(String[] idList)
如果參數(shù)的類型是Array,則在使用時,collection屬性要必須指定為 array
List<User> selectByIdSet(String[] idList); <select id="selectByIdSet" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> from t_user WHERE id IN <foreach collection="array" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </select>
(三)、參數(shù)有多個時
當(dāng)查詢的參數(shù)有多個時,有兩種方式可以實現(xiàn),一種是使用@Param(“xxx”)進(jìn)行參數(shù)綁定,另一種可以通過Map來傳參數(shù)。
3.1 @Param(“xxx”)方式
List<User> selectByIdSet(@Param("name")String name, @Param("ids")String[] idList); <select id="selectByIdSet" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> from t_user WHERE name=#{name,jdbcType=VARCHAR} and id IN <foreach collection="idList" item="id" index="index" open="(" close=")" separator=","> #{id} </foreach> </select>
3.2 Map方式
Map<String, Object> params = new HashMap<String, Object>(2); params.put("name", name); params.put("idList", ids); mapper.selectByIdSet(params); <select id="selectByIdSet" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_user where name = #{name} and ID in <foreach item="item" index="index" collection="idList" open="(" separator="," close=")"> #{item} </foreach> </select>
到此這篇關(guān)于mybatis使用foreach語句實現(xiàn)IN查詢(三種)的文章就介紹到這了,更多相關(guān)mybatis foreach 查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決參數(shù)命名不規(guī)范,造成使用@NotNull進(jìn)行校驗出現(xiàn)的問題
這篇文章主要介紹了解決參數(shù)命名不規(guī)范,造成使用@NotNull進(jìn)行校驗出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01如何解決idea的Translation插件google翻譯無法使用問題
這篇文章主要介紹了如何解決idea的Translation插件google翻譯無法使用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05使用springmvc參數(shù)接收boolean類型參數(shù)的問題
這篇文章主要介紹了使用springmvc參數(shù)接收boolean類型參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01