mybatis 批量操作增删改查
一.foreach标签相关属性介绍
在介绍批量操作之前首先需要了解一下关于批量操作的标签,我们都知道在Mybatis中我们使用foreach标签进行批量操作;
foreach标签中相关属性介绍:
这里值得注意的是:separator属性表示在元素之间使用什么样的分隔符进行分割,这里的元素是指在之间定义的部分为一个元素,正因有这个不同之处所以在进行IN操作和VALUES操作时会有所不同,后面会进行详细的讲解;
二.批量增删改查操作讲解
1.入参为List<?>,批量插入
示例一批量插入数据:
Mapper接口:
Integer batchAdd(List<UserEntity> userEntity);
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>
注:这种情况由于在mapper接口上并没有使用@Param注解指定名称,所以在collection中只能使用list作为key;同时进行批量添加数据时每个元素开始和结尾的“()”需要我们自己进行手动添加;
示例二批量删除:
入参为List,使用@Param(“KeyName”)设置键,批量删除
Mapper接口:
Integer batchDelete(@Param("idList") List<Integer> idList);
XML文件:
由于设置了@Param,因此在collection的部分需要与@Param的名称相同,这里使用了close和open,因此在可以用“ #{item}” 来代替“(#{item})
<delete id="batchDelete" parameterType="java.util.List">
DELETE FROM a where id in //由于我们在mapper接口上使用了@param注解指定keyname,所以在collection中可以使用这个名称进行匹配
<foreach collection="idList" item="item" separator="," open="(" close=")">
#{item}
</foreach>
</delete>
示例三多参数批量操作:
当接口只有一个入参的时候,可以不适用@Paramter,但当入参达到两个及以上时,必须使用
Mapper接口:
Integer batchUpdateOneVariable(@Param("user") UserEntity user,@Param("idList") List idList);
注:在mapper接口参数上有多个参数时,一定要使用@param指定各个参数的keyName,原因很简单以为我们在定义xml文件时只指定一个参数类型parameterType,这时如果有两参数势必要自己进行指定;
<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>
示例四批量查询操作:
入参为对象的某个字段
实体类:
@Data
@EqualsAndHashCode(callSuper = false)
public class UserEntity2 {
private List<Integer> ids;
private String name;
private String gender;
private Integer age;
private String psw;
private Integer seq;
}
Mapper接口:
List<UserEntity> batchSelect2(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>
示例五批量查询操作:
入参为Map对象,批量查询
Mapper接口:
List<UserEntity> batchSelect4(Map<String,Object> 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>
注:
1.如果在接口上不使用@param指定参数的别名那么在mybatis标签中进行获取属性时直接取内层属性即可,但是要写清楚参数的parameterType;
例如:在示例四中就是无参数别名的应用;
2.如果使用了@param进行mapper接口上的参数名称修饰,可以使用这个key.property的形式进行获取,这种情况一般不需要指明参数的parameterType,在mapper接口中有多个参数时经常使用这种方法进行定义;
例如:在示例三中,就是这种方式的应用;
这两种方式本身没有那种更好,我们在不同情况下可以选择不同的方式进行开发;
相关链接:https://www.cnblogs.com/wulisz/p/10194824.html