1 <foreach> 標籤
在sql、if、choose、where、set、trim標籤中,介紹了<sql>、<if>、<choose>、<where>、<set>、<trim>等標籤的用法,本文主要介紹<foreach>標籤的用法以及批量查詢、批量刪除、批量插入、批量修改等批量操作。
<foreach> 標籤用於對集合或數組進行遍歷。
<foreach collection="" item="" open="" close="" separator="" index=""></foreach>
- collection:指定要遍歷的集合或數組,
- item:設置待訪問的集合元素別名
- open:設置循環體的開始內容
- close:設置循環體的結束內容
- separator:設置每一次循環的分隔符
- index: 若遍歷的是 List 或 Array,index 表示下標;若遍歷的是 Map,index 表示鍵
2 批量查詢
2.1 方法一(String)
(1)SQL 語句
select sid,sname,sex from students where sid in (1005,1006,...)
(2)用法
<!-- public List<Student> batchQueryByString(String ids);//批量查詢(通過String) -->
<select id="batchQueryByString" resultType="com.bean.Student">
select sid,sname,sex from students where sid in(${value})
</select>
注意:這裏只能用 ${} 獲取參數,不能用 #{} 獲取參數,因爲後者會在傳入字符串兩端加上單引號,詳見→使用#{}與${}獲取參數。
2.2 方法二(List)
(1)SQL 語句
select sid,sname,sex from students where sid=1005 or sid=1006 or ...
(2)用法
<!-- public List<Student> batchQueryByList(List<Integer> ids);//批量查詢(通過List) -->
<select id="batchQueryByList" resultType="com.bean.Student">
select sid,sname,sex from students where sid in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
注意:當傳入參數爲 List 時,MyBatis 會默認將 List 放入Map中,並以 list 爲鍵,因此,foreach 標籤中 collection 屬性值爲 list,用戶也可以通過 @Param("key") 註解自定義 Map 中 key 的命名,詳見→使用#{}與${}獲取參數。
3 批量刪除
3.1 方法一(String)
(1)SQL 語句
delete from students where sid in (1005,1006,...)
(2)用法
<!-- public void batchDeleteByString(String ids);//批量刪除(通過String) -->
<delete id="batchDeleteByString">
delete from students where sid in(${value})
</delete>
3.2 方法二(List)
(1)SQL 語句
delete from students where sid in (1005,1006,...)
(2)用法
<!-- public void batchDeleteByList(List<Integer> ids);//批量刪除(通過List) -->
<delete id="batchDeleteByList">
delete from students where sid in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
4 批量插入
SQL 語句:
insert into students values(1005,'孫七','男'),(1006,'周八','女'),...
4.1 方法一(List)
<!-- public void batchInsertByList(List<Student> students);//批量添加(通過List) -->
<insert id="batchInsertByList">
insert into students values
<foreach collection="list" item="student" separator=",">
(#{student.sid},#{student.sname},#{student.sex})
</foreach>
</insert>
4.2 方法二(Array)
<!-- public void batchInsertByArray(Student[] students);//批量添加(通過Array) -->
<insert id="batchInsertByArray">
insert into students values
<foreach collection="array" item="student" separator=",">
(#{student.sid},#{student.sname},#{student.sex})
</foreach>
</insert>
注意:當傳入參數爲 Array 時,MyBatis 會默認將 Array 放入Map 中,並以 array 爲鍵,因此,foreach 標籤中 collection 屬性值爲 array,用戶也可以通過 @Param("key") 註解自定義 Map 中 key 的命名,詳見→使用#{}與${}獲取參數。
5 批量修改
5.1 把每條數據修改會相同內容
SQL 語句:
update students set sex='女' where sid in (1005,1006,...)
update students set sex='女' where sid=1005 or sid=1006 or ...
5.2 把每條數據修改爲不同內容
(1)SQL 語句
update students set sname='孫七七' sex='女' where sid=1005;
update students set sname='周八八' sex='男' where sid=1006;
...
(2)用法
<!-- public void batchUpdate(Student[] students); //批量更新(通過Array) -->
<update id="batchUpdate">
<foreach collection="array" item="student">
update students set sname=#{student.sname},sex=#{student.sex} where sid=#{student.sid};
</foreach>
</update>
注意:傳入參數也可以使用 List,但是 collection 屬性也要同步改爲 list。
運行時,顯示執行了多條 updata 語句,如下:
Preparing: update students set sname=?,sex=? where sid=?; update students set sname=?,sex=? where sid=?;
但是,MyBatis 默認一次只能執行一條 SQL 語句,因此,需要在連接數據庫時將 allowMultiQueries 屬性設置爲 true,如下:
jdbc.properties
# K = V
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/users?characterEncoding=UTF-8&allowMultiQueries=true
jdbc.username=root
jdbc.password=0.
注意:characterEncoding 用於設置編碼格式,避免插入和更新數據有中文時,數據庫裏會顯示“?”;allowMultiQueries 屬性設置爲 true,表示允許一次執行多條 SQL 語句。