Mybatis之批量插入、批量删除
Mybatis批量插入
mapping.xml代码如下:
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO user( id,user_name,user_password,user_address,create_by,create_date,
update_by,update_date,remarks,del_flag)
VALUES
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
(
#{item.id},#{item.userName},#{item.userPassword},#{item.userAddress},
#{item.createBy},#{item.createDate},#{item.updateBy},#{item.updateDate},
#{item.remarks},#{item.delFlag}
)
</foreach>
</insert>
如果从DAO里面映射的参数是数组,那么collection="array"。
Mybatis批量删除
mapping.xml代码如下:
<delete id="deleteBatch" parameterType="int">
DELETE FROM user WHERE id IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
如果从DAO里面映射的参数是数组,那么collection="array"。