mybatis可进行批量插入、删除、更新。具体的语法为
<foreach collection="" item="" index="索引" open="" close="" separator="">
。。。。
</foreach>
- 当传入的是单参数,且参数类型是数组,collection=”array“
- 当传入的是单参数,且参数类型是List,collection=”list“
- 当使用@param("keyname")时,collection="keyname"
- 当传入多参数的时候,可以使用Map
属性 | 描述 |
item | 循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection | 要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。 |
separator | 元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index | 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
批量插入
实体类
import java.io.Serializable;
public class AttachmentTable implements Serializable {
private static final long serialVersionUID = 8325882509007088323L;
private Integer id;
// 附件名称
private String name;
// 日志ID
private Integer logid;
// 附件URL
private String url;
// getter/setter.......
}
mapper接口
import java.util.List;
import model.AttachmentTable;
public interface AttachmentTableMapper {
int insert(AttachmentTable record);
void insertByBatch(List<AttachmentTable> attachmentTables);
}
Mapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.AttachmentTableMapper">
<resultMap id="BaseResultMap" type="model.AttachmentTable">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="logID" jdbcType="INTEGER" property="logid" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="model.AttachmentTable">
<result column="url" jdbcType="LONGVARCHAR" property="url" />
</resultMap>
<sql id="Base_Column_List">
id, name, logID
</sql>
<sql id="Blob_Column_List">
url
</sql>
<insert id="insert" parameterType="model.AttachmentTable">
insert into attachment_table (id, name, logID,url)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{logid,jdbcType=INTEGER},#{url,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertByBatch" parameterType="java.util.List">
insert into attachment_table (name, logID,url)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR})
</foreach>
</insert>
</mapper>
<insert id="insert" parameterType="model.AttachmentTable">
insert into attachment_table (id, name, logID,url)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{logid,jdbcType=INTEGER},# {url,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertByBatch" parameterType="java.util.List">
insert into attachment_table (name, logID,url)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.name,jdbcType=VARCHAR}, #{item.logid,jdbcType=INTEGER},#{item.url,jdbcType=LONGVARCHAR})
</foreach>
</insert>
批量删除
- 当传入的是单参数,切参数类型是数组,collection=”array“
- 当传入的是单参数,切参数类型是List,collection=”list“
- 当使用@param("keyname")时,collection="keyname"
- 当传入多参数的时候,可以使用Map
情况1、2
EmpMapper.xml:
<!-- 批量删除员工信息 -->
<delete id="batchDeleteEmps" parameterType="int">
delete from emp where empno in
<foreach item="empnoItem" collection="array" open="(" separator="," close=")">
#{empnoItem}
</foreach>
</delete>
说明
emp : 表名
empno : 字段名
collection:表示类型,这里参数是数组,就写成array,如果是集合,就写成list
item : 是一个变量名,自己随便起名
EmpMapper.java :
/*
* 批量删除员工信息
*/
void batchDeleteEmps(int[] empno);
情况3 、多参数批量删除
void deleteByLogIdAndNames(@Param("logid") Integer logID, @Param("names") String[] names);
<delete id="deleteByLogIdAndNames">
delete from attachment_table
where logid = #{logid,jdbcType=INTEGER} AND NAME IN
<foreach collection="names" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
此处因为使用了@Param,array失效,故collection="names"
批量更新
更新单条记录
UPDATE course SET name = 'course1' WHERE id = 'id1';
更新多条记录的同一个字段为同一个值
UPDATE course SET name = 'course1' WHERE id in ('id1', 'id2', 'id3);
更新多条记录为多个字段为不同的值
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update course
<set>
name=${item.name}
</set>
where id = ${item.id}
</foreach>
</update>
Mapper接口
int updateBatch(List<WaterEle> list);
1、传list集合、单个字段
mapper.xml
批量更新测试
<update id="updateByBatch" parameterType="java.util.List">
update t_goods
set NODE_ID=
<foreach collection="list" item="item" index="index"
separator=" " open="case" close="end">
when GOODS_ID=#{item.goodsId} then #{item.nodeId}
</foreach>
where GOODS_ID in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.goodsId,jdbcType=BIGINT}
</foreach>
</update>
单个字段方法二
<update id="updateByBatch" parameterType="java.util.List">
UPDATE
t_goods
SET NODE_ID = CASE
<foreach collection="list" item="item" index="index">
WHEN GOODS_ID = #{item.goodsId} THEN #{item.nodeId}
</foreach>
END
WHERE GOODS_ID IN
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.goodsId}
</foreach>
</update>
以上单字段更新实际执行:UPDATE t_goods SET NODE_ID = CASE WHEN GOODS_ID = ? THEN ? END WHERE GOODS_ID IN ( ? )
2、传list集合、多字段
<update id="updateBatch" parameterType="java.util.List">
update t_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="STATUS =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.status!=null">
when USER_ID=#{i.userId} then #{i.status}
</if>
</foreach>
</trim>
<trim prefix=" OPERATE_TIME =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.operateTime!=null">
when USER_ID=#{i.userId} then #{i.operateTime}
</if>
</foreach>
</trim>
<trim prefix="OPERATOR =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.operator!=null">
when USER_ID=#{i.userId} then #{i.operator}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
USER_ID=#{i.userId}
</foreach>
</update>
int updateBatch(List<WaterEle> list);
更新多条记录的同一个字段为同一个值
<update id="updateByBatchPrimaryKey" parameterType="java.util.Map">
UPDATE t_goods
SET NODE_ID = #{nodeId}
WHERE GOODS_ID IN (${goodsIdList})
</update>
UPDATE t_goods SET NODE_ID = ? WHERE GOODS_ID IN (1,2,5);
3、传map/ 传String(同批量删除的"传map/ 传String")
<update id="deleteByPrimaryKey" parameterType="java.util.Map">
UPDATE t_order_checkout
SET NODE_ID = #{nodeId, jdbcType=VARCHAR}, OPERATOR = #{operator, jdbcType=VARCHAR}
WHERE CHECKOUT_ID IN (${checkoutIdList})
</update>