mybatis批量操作之mysql/postgres
1、批量insert2、批量update
1、批量insert
Mapper.java
int insertBatch(@Param("list")List
<?> list
);
Mapper.xml
<insert id="insertBatch" parameterType="java.util.List">
insert into table (id, name, create_time,update_time) values
<foreach collection="list" separator="," item="item" index="index">
(
#{item.id,jdbcType=VARCHAR},
#{item.name,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP},
#{item.updateTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
2、批量update
Mapper.java
int updateBatch(@Param("list")List
<?> list
);
Mapper.xml
<update id="updateBatch" parameterType="java.util.List">
update table
SET
name = CASE id
<foreach collection="list" item="item">
WHEN #{item.id}THEN #{item.name}
</foreach>
END,
create_time = CASE id
<foreach collection="list" item="item">
WHEN #{item.id} THEN #{item.createTime}
</foreach>
END,
update_time = CASE id
<foreach collection="list" item="item">
WHEN #{item.id} THEN #{item.updateTime}
</foreach>
END
WHERE
id IN
<foreach collection="list" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</update>
上一篇:mybatis批量insert和update(oracle版)