MyBatis——批量操作

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 语句

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章