mybatis 批量操作增删改查
在介绍批量操作之前,首先先介绍一个语法:foreach。可以说是,foreach是整个批量操作的灵魂。
属性 | 描述 |
item |
循环体中的具体对象。 支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection |
要做foreach的对象,作为入参时, List<?>对象默认用list代替作为键, 数组对象用array代替作为键, Map对象没有默认的键 当然在作为入参时可以使用@Param("keyName")来设置键, 设置keyName后,list,array将会失效。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。 |
separator |
元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开, 避免手动输入逗号导致sql错误,如in(1,2,)这样。 该参数可选。 |
open |
foreach代码的开始符号,一般"("和close=")"合用。常用在in(),values()时。 该参数可选 |
close |
foreach代码的关闭符号,一般是")"和open="("合用。常用在in(),values()时。 该参数可选 |
index |
在list和数组中,index是元素的序号,在map中,index是元素的key。 该参数可选 |
接下来,就是批量操作的内容了。
本文按入参的形式分类,穿插着整理了批量更新、删除、插入、查询的方法,希望能对各位小伙伴有帮助。
项目源代码传送门
正文开始~~~~~
实体类UserEntity.java
@Data @EqualsAndHashCode(callSuper = false) public class UserEntity implements Serializable{ private Integer id; private String name; private String gender; private Integer age; private String psw; private Integer seq; }
入参为List<?>,批量插入
Mapper接口:
Integer batchAdd(ListuserEntity);
XML:
collection的部分需要填写list作为键,由于在foreach中手动填写了“(”和“)”,因此不需要使用close和open,通过"item."的点路径访问UserEntity的属性。
<insert id="batchAdd" parameterType="java.util.List"> INSERT INTO a(name, age, gender, psw, seq) values <foreach collection="list" item="item" index="index" separator=","> ( #{item.name},#{item.age},#{item.gender},#{item.psw},#{item.seq}) foreach> insert>
入参为List,使用@Param("KeyName")设置键,批量删除
Mapper接口:
Integer batchDelete(@Param("idList") ListidList);
XML文件:
由于设置了@Param,因此在collection的部分,需要与@Param的名称相同
这里使用了close和open,因此在可以用“ #{item}” 来代替“(#{item})”
<delete id="batchDelete" parameterType="java.util.List"> DELETE FROM a where id in <foreach collection="idList" item="item" separator="," open="(" close=")"> #{item}
foreach>
delete>
入参为两个,批量更新
当接口只有一个入参的时候,可以不适用@Paramter,但当入参达到两个及以上时,必须使用哦
Mapper接口:
Integer batchUpdateOneVariable(@Param("user") UserEntity user,@Param("idList") List idList);
XML文件:
<update id="batchUpdateOneVariable" > UPDATE a set psw=#{user.psw} <where> id in ( <if test="idList.size()!=0"> <foreach collection="idList" separator="," item="item" index="index"> #{item} foreach> if> )
where> update>
入参为对象的某字段,批量查询
实体类UserEntity2.java
@Data @EqualsAndHashCode(callSuper = false) public class UserEntity2 { private Listids; private String name; private String gender; private Integer age; private String psw; private Integer seq; }
Mapper接口:
ListbatchSelect2(UserEntity2 userEntity2);
XML文件:
<select id="batchSelect2" parameterType="cn.com.exercise.batch.entity.UserEntity2" resultMap="user"> select * from a <where> id in <foreach collection="ids" separator="," open="(" close=")" index="index" item="item"> #{item} foreach> where> select>
入参为数组对象,批量查询
Mapper接口:
ListbatchSelect3(Integer[] idArray);
XML文件:
传入SQL的参数形如"(1,2,3)”,因此SQL语句中的参数类型设置为String即可。
但是collection的类型需要设置为array。
<select id="batchSelect3" parameterType="String" resultMap="user"> select * from a <where> id in <foreach collection="array" separator="," open="(" close=")" index="index" item="item"> #{item} foreach> where> select>
入参为Map对象,批量查询
Mapper接口:
ListbatchSelect4(Map myMap);
XML文件:
入参为Map是,collecttion的名称写待循环的对象即可
<select id="batchSelect4" parameterType="java.util.Map" resultMap="user"> select * from a <where> <if test="ageMap!=null"> and age = #{ageMap} if> <if test="idMap!=null"> and id in <foreach collection="idMap" separator="," open="(" close=")" index="index" item="item"> #{item} foreach> if> where> select>
封装的入参:
MapmyMap = new HashMap<>(); List ids = new ArrayList(); ids.add(11); ids.add(12); ids.add(13); myMap.put("idMap",ids); myMap.put("ageMap",32);