mybatis?foreach?list特殊處理方式
foreach list特殊處理
最近做一個功能,sql要用到 IN 條件,通過list傳入IN 的值,如:
SELECT * FROM table1 WHERE id in (1,2,3)
對應的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>
期望結果是按list的值進行查詢。
可是,當list為空的時候呢?
sql應該是這樣的SELECT * FROM table1 WHERE id in ()。直接在mysql中運行肯定是語法錯誤的。
無論如何是不會查到數(shù)據的,但mybatis又是什么策略呢?直接把這一條where條件給我忽略了…導致查全表數(shù)據。
這也不好說mybatis做的好不好,算不算bug了。
既然知道套路了,就得有策略來應對了。于是多寫了兩個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>
不算是這么上策,但也能解決問題,當list為空時,給id賦值一個-1,保證查不到數(shù)據,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=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該參數(shù)可選。close
:foreach代碼的關閉符號,一般是)和open="("合用。常用在in(),values()時。該參數(shù)可選。
collection:foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的
主要有一下3種情況:
1. 如果傳入的是單參數(shù)且參數(shù)類型是一個List的時候,collection屬性值為list
2. 如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array
3. 如果使用Map封裝了,collection的屬性值為對應的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;? }
測試截圖:
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java Spring Controller 獲取請求參數(shù)的幾種方法詳解
這篇文章主要介紹了Java Spring Controller 獲取請求參數(shù)的幾種方法詳解的相關資料,這里提供了6種方法,需要的朋友可以參考下2016-12-12