Mybatis動態Sql的Foreach遍歷拼接輸入引數中的List或陣列
pojo、pojo包裝類
public class User {
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
//省略get/set方法
}
//繼承User類
public class UserCustom extends User {
}
/*
包裝類
* */
public class UserQueryVo {
private UserCustom userCustom;
private List<Integer> ids; //從pojo類中讀取List
}
當paramterType是UserQueryVo時,需要取出ids的值,用foreach遍歷
UserMapper.xml<sql id="findUser_where">
<if test="userCustom">
<if test="userCustom.sex != null and userCustom.sex != ''">
and sex = #{userCustom.sex}
</if>
<!--
使用foreach遍歷傳入的ids集合
collection:指定輸入的物件中集合的屬性
item:每次遍歷的元素生成的物件
open:開始遍歷時生成的串
close:結束遍歷時生成的串
separator:遍歷的兩個物件中需要生成的串
-->
<if test="ids != null">
<!--最終生成的SQL語句: select * from user where sex = '' and (id= or id= or id= ) and username like '' -->
<foreach collection="ids" item="user_id" open="and (" close=")" separator="or">
<!-- 每個遍歷需要生成的串 -->
id=#{user_id}
</foreach>
</if>
<!-- 與上面的foreach等價
<if test="ids != null">
最終生成的SQL語句: select * from user where sex = '' and id in(1,2,3) and username like ''
<foreach collection="ids" item="user_id" open="and id in(" close=")" separator=",">
#{user_id}
</foreach>
</if>
-->
<if test="userCustom.username != null and userCustom.username != ''">
and username like '%${userCustom.username}%'
</if>
</if>
</sql>
<select id="findUserByIds" parameterType="cn.itcast.mybatis.po.UserQueryVo" resultType="cn.itcast.mybatis.po.UserCustom">
select * from user
<!-- 最好不要將where語句包括進去 -->
<where>
<!-- 引用sql片段 ,如果refid指定的id不在本mapper中,需要指定namesapce-->
<include refid="findUser_where"></include>
</where>
</select>
相關文章
- MyBatis中foreach傳入引數為list、陣列、map的不同寫法MyBatis陣列
- mybatis sql foreach 引數的傳入的三種情況!!MyBatisSQL
- 遍歷陣列的常用方法forEach,filter,map等陣列Filter
- MyBatis的XML配置:如何判斷List為空並遍歷拼接MyBatisXML
- 利用c:forEach標籤遍歷陣列陣列
- Mybatis中Foreach動態SQL標籤(map和list兩種情況)MyBatisSQL
- BIRT 中如何根據引數動態拼接 SQLSQL
- MyBatis中傳入引數為List集合的MyBatis
- JavaScript 的 4 種陣列遍歷方法: for VS forEach() VS for/in VS for/ofJavaScript陣列
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- js 遍歷陣列取出字串用逗號拼接JS陣列字串
- JS中遍歷陣列、物件的方式JS陣列物件
- List介面(動態陣列)陣列
- 陣列遍歷陣列
- 動態陣列ArrayList的初始化,新增資料,與遍歷陣列
- 在lambda的foreach遍歷中break退出(lambda foreach break)
- java陣列遍歷的方法Java陣列
- JS中陣列的遍歷方法(3種)JS陣列
- Mybatis傳入引數為List物件MyBatis物件
- mybatis關於list的foreach的使用MyBatis
- 陣列常見的遍歷迴圈方法、陣列的迴圈遍歷的效率對比陣列
- 在PHP中陣列遍歷的三種方法PHP陣列
- 陣列遍歷方法陣列
- 遍歷陣列物件陣列物件
- 遍歷陣列的幾種方法陣列
- JS中陣列遍歷方法foreach,filter,some,every,map方法介紹與總結JS陣列Filter
- Mybatis框架:foreach迴圈遍歷欄位(為了解決動態表、動態欄位查詢資料)MyBatis框架
- java8 對list集合中的物件遍歷,重新賦值兩種方法,遍歷某個屬性返回陣列Java物件賦值陣列
- jstl forEach遍歷JS
- php手冊 php陣列的遍歷有哪幾種方式?php陣列如何遍歷?PHP陣列
- JS中陣列與物件的遍歷方法例項JS陣列物件
- 輸入一個整數陣列,輸出奇偶數相間排列的陣列陣列
- 遍歷PHP陣列的6種方式PHP陣列
- java陣列如何遍歷全部的元素Java陣列
- js 遍歷陣列方式JS陣列
- mybatis的配置檔案中使用兩個或多個foreach進行多個集合遍歷的問題MyBatis
- 遍歷物件和陣列的方法總結物件陣列
- 遍歷陣列和物件的方法都有哪些?陣列物件
- c++遍歷陣列的多種方式C++陣列