MyBatis引數傳入集合之foreach動態sql
foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。foreach元素的屬性主要有item,index,collection,open,separator,close。item表示集合中每一個元素進行迭代時的別名,index指定一個名字,用於表示在迭代過程中,每次迭代到的位置,open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號作為分隔符,close表示以什麼結束,在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:
public class Employees {
private Integer employeeId;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private Date hireDate;
private String jobId;
private BigDecimal salary;
private BigDecimal commissionPct;
private Integer managerId;
private Short departmentId;
}
對映檔案:
<!--List:forech中的collection屬性型別是List,collection的值必須是:list,item的值可以隨意,Dao介面中引數名字隨意 -->
<select id="getEmployeesListParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="list" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Array:forech中的collection屬性型別是array,collection的值必須是:list,item的值可以隨意,Dao介面中引數名字隨意 -->
<select id="getEmployeesArrayParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="array" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Map:不單單forech中的collection屬性是map.key,其它所有屬性都是map.key,比如下面的departmentId -->
<select id="getEmployeesMapParams" resultType="Employees">
select *
from EMPLOYEES e
<where>
<if test="departmentId!=null and departmentId!=''">
e.DEPARTMENT_ID=#{departmentId}
</if>
<if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
AND e.EMPLOYEE_ID in
<foreach collection="employeeIdsArray" item="employeeId"
index="index" open="(" close=")" separator=",">
#{employeeId}
</foreach>
</if>
</where>
</select>
Mapper類:
public interface EmployeesMapper {
List<Employees> getEmployeesListParams(List<String> employeeIds);
List<Employees> getEmployeesArrayParams(String[] employeeIds);
List<Employees> getEmployeesMapParams(Map<String,Object> params);
}
- 如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list .
- 如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array .
- 如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map了,當然單引數也可以封裝成map,實際上如果你在傳入引數的時候,在MyBatis裡面也是會把它封裝成一個Map的,map的key就是引數名,所以這個時候collection屬性值就是傳入的List或array物件在自己封裝的map裡面的key.
下面我們通過程式碼實踐:
資料表:
採用Oracle的HR.Employees表
實體:Employees
public class Employees {
private Integer employeeId;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private Date hireDate;
private String jobId;
private BigDecimal salary;
private BigDecimal commissionPct;
private Integer managerId;
private Short departmentId;
}
對映檔案:
<!--List:forech中的collection屬性型別是List,collection的值必須是:list,item的值可以隨意,Dao介面中引數名字隨意 -->
<select id="getEmployeesListParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="list" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Array:forech中的collection屬性型別是array,collection的值必須是:list,item的值可以隨意,Dao介面中引數名字隨意 -->
<select id="getEmployeesArrayParams" resultType="Employees">
select *
from EMPLOYEES e
where e.EMPLOYEE_ID in
<foreach collection="array" item="employeeId" index="index"
open="(" close=")" separator=",">
#{employeeId}
</foreach>
</select>
<!--Map:不單單forech中的collection屬性是map.key,其它所有屬性都是map.key,比如下面的departmentId -->
<select id="getEmployeesMapParams" resultType="Employees">
select *
from EMPLOYEES e
<where>
<if test="departmentId!=null and departmentId!=''">
e.DEPARTMENT_ID=#{departmentId}
</if>
<if test="employeeIdsArray!=null and employeeIdsArray.length!=0">
AND e.EMPLOYEE_ID in
<foreach collection="employeeIdsArray" item="employeeId"
index="index" open="(" close=")" separator=",">
#{employeeId}
</foreach>
</if>
</where>
</select>
Mapper類:
public interface EmployeesMapper {
List<Employees> getEmployeesListParams(List<String> employeeIds);
List<Employees> getEmployeesArrayParams(String[] employeeIds);
List<Employees> getEmployeesMapParams(Map<String,Object> params);
}
測試方法(未貼完整程式碼):
@Test
public void testGetEmployeesListParams() {
List<String> employeeIds = Arrays.asList("100", "101", "200");
List<Employees> result = employeesMapper
.getEmployeesListParams(employeeIds);
assertEquals(3, result.size());
}
@Test
public void testGetEmployeesArrayParams() {
String[] employeeIds = new String[] { "100", "200" };
List<Employees> result = employeesMapper
.getEmployeesArrayParams(employeeIds);
assertEquals(2, result.size());
}
@Test
public void testGetEmployeesMapParams() {
String departmentId = "60";
List<String> employeeIdsList = Arrays.asList("103", "104", "105");
String[] employeeIdsArray = new String[] { "103", "104" };
// 傳入多個引數
Map<String, Object> params = new HashMap<String, Object>();
params.put("departmentId", departmentId);
params.put("employeeIdsList", employeeIdsList);
params.put("employeeIdsArray", employeeIdsArray);
List<Employees> result = employeesMapper.getEmployeesMapParams(params);
assertEquals(3, result.size());
}
public void testGetEmployeesListParams() {
List<String> employeeIds = Arrays.asList("100", "101", "200");
List<Employees> result = employeesMapper
.getEmployeesListParams(employeeIds);
assertEquals(3, result.size());
}
@Test
public void testGetEmployeesArrayParams() {
String[] employeeIds = new String[] { "100", "200" };
List<Employees> result = employeesMapper
.getEmployeesArrayParams(employeeIds);
assertEquals(2, result.size());
}
@Test
public void testGetEmployeesMapParams() {
String departmentId = "60";
List<String> employeeIdsList = Arrays.asList("103", "104", "105");
String[] employeeIdsArray = new String[] { "103", "104" };
// 傳入多個引數
Map<String, Object> params = new HashMap<String, Object>();
params.put("departmentId", departmentId);
params.put("employeeIdsList", employeeIdsList);
params.put("employeeIdsArray", employeeIdsArray);
List<Employees> result = employeesMapper.getEmployeesMapParams(params);
assertEquals(3, result.size());
}
相關文章
- mybatis sql foreach 引數的傳入的三種情況!!MyBatisSQL
- Mybatis foreach 請求引數是物件集合MyBatis物件
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- MyBatis中傳入引數為List集合的MyBatis
- MyBatis中foreach傳入引數為list、陣列、map的不同寫法MyBatis陣列
- mybatis 傳入多個引數MyBatis
- Mybatis介紹之 動態SQLMyBatisSQL
- Spring Boot入門系列(十九)整合mybatis,使用註解實現動態Sql、引數傳遞等常用操作!Spring BootMyBatisSQL
- Mybatis傳入引數為List物件MyBatis物件
- MyBatis框架之SQL對映和動態SQLMyBatis框架SQL
- MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法MyBatisSQL
- Mybatis中Foreach動態SQL標籤(map和list兩種情況)MyBatisSQL
- MyBatis 動態 SQLMyBatisSQL
- mybatis動態SQLMyBatisSQL
- Mybatis動態傳入order by 引數的時候不生效的問題解決方案MyBatis
- mybatis如何傳引數MyBatis
- Mybatis引數傳遞MyBatis
- mybatis 的傳入引數如何既有物件又有單個引數MyBatis物件
- Mybatis parameterType 傳入多個引數的使用MyBatis
- MyBatis(七) 動態SQLMyBatisSQL
- 1 Mybatis動態SQLMyBatisSQL
- Mybatis--動態SQLMyBatisSQL
- MyBatis從入門到精通(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- mybatis動態sql總結MyBatisSQL
- Mybatis-06 動態SqlMyBatisSQL
- 15.MyBatis傳入多個引數的問題MyBatis
- MyBatis系列(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- MyBatis對動態SQL的支援MyBatisSQL
- mybatis動態sql與分頁MyBatisSQL
- Java-Mybatis動態SQL整理JavaMyBatisSQL
- SpringBoot系列Mybatis之引數傳遞的幾種姿勢Spring BootMyBatis
- BIRT 中如何根據引數動態拼接 SQLSQL
- 好程式設計師分享MyBatis之動態SQL語句程式設計師MyBatisSQL
- Mybatis 傳入多個引數查詢資料 (3種方法)MyBatis
- Mybatis 動態執行SQL語句MyBatisSQL
- Mybatis(五)--原始碼分析傳入單個list引數和多個list引數寫法MyBatis原始碼
- Mybatis引數傳遞&註解開發MyBatis
- 【MyBatis學習總結 (五),動態SQL】MyBatisSQL
- Mybatis where 1=1 動態sql問題MyBatisSQL