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引數傳入集合之foreach動態sqlMyBatisSQL
- js遍歷拼接list集合物件,JSONArray陣列物件JSON陣列
- ***PHP遍歷陣列的方法foreachPHP陣列
- 遍歷陣列的常用方法forEach,filter,map等陣列Filter
- Html遍歷物件、list集合、陣列HTML物件陣列
- BIRT 中如何根據引數動態拼接 SQLSQL
- 利用c:forEach標籤遍歷陣列陣列
- mybatis sql foreach 引數的傳入的三種情況!!MyBatisSQL
- MyBatis中傳入引數為List集合的MyBatis
- Mybatis中Foreach動態SQL標籤(map和list兩種情況)MyBatisSQL
- mybatis list作為引數 foreach迴圈MyBatis
- JavaScript 的 4 種陣列遍歷方法: for VS forEach() VS for/in VS for/ofJavaScript陣列
- JS中遍歷陣列、物件的方式JS陣列物件
- for in語句遍歷陣列中的元素陣列
- js如何遍歷陣列中的元素JS陣列
- 如何在mybatis 中傳多個引數,如何在mybatis 中遍歷 集合?MyBatis
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- 陣列遍歷陣列
- List介面(動態陣列)陣列
- JS中陣列的遍歷方法(3種)JS陣列
- Python中list的遍歷Python
- java陣列遍歷的方法Java陣列
- 物件和陣列的遍歷物件陣列
- Mybatis傳入引數為List物件MyBatis物件
- 在PHP中陣列遍歷的三種方法PHP陣列
- OC中陣列、字典的遍歷的三種方法陣列
- 陣列遍歷方法陣列
- 陣列常見的遍歷迴圈方法、陣列的迴圈遍歷的效率對比陣列
- 遍歷陣列的幾種方法陣列
- Jvascript陣列迭代,遍歷的方法陣列
- JAVA多維陣列的遍歷Java陣列
- java8 對list集合中的物件遍歷,重新賦值兩種方法,遍歷某個屬性返回陣列Java物件賦值陣列
- Mybatis框架:foreach迴圈遍歷欄位(為了解決動態表、動態欄位查詢資料)MyBatis框架
- JS中陣列遍歷方法foreach,filter,some,every,map方法介紹與總結JS陣列Filter
- JS中陣列與物件的遍歷方法例項JS陣列物件
- javascript如何遍歷陣列中的每一個元素JavaScript陣列
- php手冊 php陣列的遍歷有哪幾種方式?php陣列如何遍歷?PHP陣列