mybatis?foreach?list特殊處理方式
foreach list特殊處理
最近做一個功能,sql要用到 IN 條件,通過list傳入IN 的值,如:
SELECT * FROM table1 WHERE id in (1,2,3)
對應(yīng)的mybatis寫法為:
? ? <select id="queryByIds" resultMap="resultMap" parameterType="list">
? ? ? ? SELECT * FROM table1
? ? ? ? WHERE id
? ? ? ? <foreach collection="list" item="rid" open="in(" separator="," close=")">
? ? ? ? ? ? #{rid}
? ? ? ? </foreach>
? ? </select>期望結(jié)果是按list的值進(jìn)行查詢。
可是,當(dāng)list為空的時候呢?
sql應(yīng)該是這樣的SELECT * FROM table1 WHERE id in ()。直接在mysql中運(yùn)行肯定是語法錯誤的。
無論如何是不會查到數(shù)據(jù)的,但mybatis又是什么策略呢?直接把這一條where條件給我忽略了…導(dǎo)致查全表數(shù)據(jù)。
這也不好說mybatis做的好不好,算不算bug了。
既然知道套路了,就得有策略來應(yīng)對了。于是多寫了兩個if。
? ? <select id="queryByIds" resultMap="resultMap" parameterType="list">
? ? ? ? SELECT * FROM table1
? ? ? ? WHERE id
? ? ? ? <if test="list != null and list.size() > 0">
? ? ? ? ? ? <foreach collection="list" item="rid" open="in(" separator="," close=")">
? ? ? ? ? ? ? ? #{rid}
? ? ? ? ? ? </foreach>
? ? ? ? </if>
? ? ? ? <if test="list == null or list.size() == 0">
? ? ? ? ? ? = -1
? ? ? ? </if>
? ? </select>不算是這么上策,但也能解決問題,當(dāng)list為空時,給id賦值一個-1,保證查不到數(shù)據(jù),sql也沒有語法錯誤。
foreach用法 List和Array,對象
向sql傳遞數(shù)組或List,mybatis使用foreach解析
foreach元素的屬性主要有item,index,collection,open,separator,close。
item:集合中元素迭代時的別名,該參數(shù)為必選。index:在list和數(shù)組中,index是元素的序號,在map中,index是元素的key,該參數(shù)可選open:foreach代碼的開始符號,一般是(和close=")"合用。常用在in(),values()時。該參數(shù)可選separator:元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導(dǎo)致sql錯誤,如in(1,2,)這樣。該參數(shù)可選。close:foreach代碼的關(guān)閉符號,一般是)和open="("合用。常用在in(),values()時。該參數(shù)可選。
collection:foreach的時候最關(guān)鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的
主要有一下3種情況:
1. 如果傳入的是單參數(shù)且參數(shù)類型是一個List的時候,collection屬性值為list
2. 如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array
3. 如果使用Map封裝了,collection的屬性值為對應(yīng)的Key
1、array數(shù)組的類型
UserMapper.java
List<User> findUser_array(int [] ids)throws Exception;
UserMapper.xml
<!--單參數(shù)array數(shù)組的類型-->?
? <select id="findUser_array" resultType="com.xiaomin.page.pojo.User">?
? ? select * from sys_user?
? ? ? ? <where>?
? ? ? ? ? ? <foreach collection="array" item="item" open="and id in(" separator="," close=")">?
? ? ? ? ? ? ? ? ?#{item}?
? ? ? ? ? ? </foreach>?
? ? ? ? </where>?
? </select>Controller.java
@RequestMapping("/findUser_array")?
? @ResponseBody?
? public List<User> foreach_test()throws Exception{?
? ? int [] ids=new int[]{1,2,3};?
? ? List<User> list = userMapper.findUser_array(ids);?
? ? return list;?
}示例:

2、list的類型
UserMapper.java
List<User> findUser_list(List<Integer>list)throws Exception;
UserMapper.xml
<!--list類型-->?
? ? <select id="findUser_list" resultType="com.xiaomin.page.pojo.User">?
? ? select * from sys_user?
? ? <where>?
? ? ? ? <foreach collection="list" item="user_id" open="and id in(" separator="," close=")">?
? ? ? ? ? ? #{user_id}?
? ? ? ? </foreach>?
? ? </where>?
? </select>Controller.java
/**
?* list類型
?* @return
?* @throws Exception
?*/?
? @RequestMapping("/findUser_list")?
? @ResponseBody?
? public List<User> findUser_list()throws Exception{?
? ? List<User> list = userMapper.findUser_list(Arrays.asList(new Integer[]{1,2,3,6}));?
? ? return list;?
}測試截圖:

3、對象類型
UserMapper.java
List<User> findUser_obj(UserQueryVo vo)throws Exception;
UserMapper.xml
<!--對象類型-->?
? <select id="findUser_obj" parameterType="com.xiaomin.page.pojo.vo.UserQueryVo" resultType="com.xiaomin.page.pojo.User">?
? ? select * from sys_user?
? ? ? ? <where>?
? ? ? ? ? ? <if test="ids !=null">?
? ? ? ? ? ? ? ? <foreach collection="ids" item="user_id" open="and id in (" separator="," close=")">?
? ? ? ? ? ? ? ? ? ? #{user_id}?
? ? ? ? ? ? ? ? </foreach>?
? ? ? ? ? ? </if>?
? ? ? ? </where>?
? </select>Controller.java
/**
?* 對象類型
?* @return
?* @throws Exception
?*/?
? @RequestMapping("/findUser_obj")?
? @ResponseBody?
? public List<User> findUser_obj()throws Exception{
?
? ? List<Integer> ids = new ArrayList<>();?
? ? ids.add(1);?
? ? ids.add(2);?
? ? ids.add(3);
?
? ? UserQueryVo queryVo = new UserQueryVo();?
? ? queryVo.setIds(ids);?
? ? List<User> list = userMapper.findUser_obj(null);?
? ? return list;?
}測試截圖:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis注解實(shí)現(xiàn)動態(tài)SQL問題
這篇文章主要介紹了MyBatis注解實(shí)現(xiàn)動態(tài)SQL問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Spring Security 圖片驗證碼功能的實(shí)例代碼
spring security是一系列的過濾器鏈,所以在這里驗證碼也聲明為過濾器,加在過濾器鏈的 登錄過濾器之前,然后自定義一個異常類,來響應(yīng)驗證碼的錯誤信息.這篇文章主要介紹了Spring Security 圖片驗證碼,需要的朋友可以參考下2018-03-03
Java Spring Controller 獲取請求參數(shù)的幾種方法詳解
這篇文章主要介紹了Java Spring Controller 獲取請求參數(shù)的幾種方法詳解的相關(guān)資料,這里提供了6種方法,需要的朋友可以參考下2016-12-12
解析探秘fescar分布式事務(wù)實(shí)現(xiàn)原理
這篇文章主要為大家解析探秘fescar分布式事務(wù)的實(shí)現(xiàn)原理,fescar的txc模型實(shí)現(xiàn)非常有研究的價值,所以今天我們來好好翻一翻fescar項目的代碼2022-02-02
Java實(shí)現(xiàn)SHA-256加密算法的完全解析
SHA-256是一種散列(哈希)算法,用于將任意長度的數(shù)據(jù)映射為固定長度的散列值,以保證數(shù)據(jù)完整性。本文將為大家介紹一下SHA-256加密算法的原理與實(shí)現(xiàn),希望對大家有所幫助2023-02-02

