mybatis批量插入、批量删除
mybatis
批量插入
int addBatch(@Param("list")List<CustInfo> list);
<insert id="addBatch" parameterType="java.util.List">
INSERT INTO CUSTINFO(
SERIALID,
CUSTID,
INVNM,
UPDATETIMESTAMP
)
<foreach collection="list" item="item" separator="union all" index="index" >
( SELECT
#{item.serialid, jdbcType=VARCHAR},
#{item.custid, jdbcType=VARCHAR},
#{item.invnm, jdbcType=VARCHAR},
TO_TIMESTAMP(#{item.updatetimestamp}, 'syyyy-mm-dd hh24:mi:ss.ff')
FROM DUAL
)
</foreach>
</insert>
批量删除
int delCustInfoBatch(@Param("list")List<CustInfo> list);
<update id="delCustInfoBatch" parameterType="java.util.List" >
DELETE FROM CUSTINFO
WHERE SERIALID IN
<foreach collection="list" item="item" open="(" separator="," close=")" index="index" >
#{item.serialid,jdbcType=VARCHAR}
</foreach>
</update>
要做批量插入数据库,首先得知道该数据库对批量插入所支持的语法。
批量插入
int addBatch(@Param("list")List<CustInfo> list);
<insert id="addBatch" parameterType="java.util.List">
INSERT INTO CUSTINFO(
SERIALID,
CUSTID,
INVNM,
UPDATETIMESTAMP
)
<foreach collection="list" item="item" separator="union all" index="index" >
( SELECT
#{item.serialid, jdbcType=VARCHAR},
#{item.custid, jdbcType=VARCHAR},
#{item.invnm, jdbcType=VARCHAR},
TO_TIMESTAMP(#{item.updatetimestamp}, 'syyyy-mm-dd hh24:mi:ss.ff')
FROM DUAL
)
</foreach>
</insert>
批量删除
int delCustInfoBatch(@Param("list")List<CustInfo> list);
<update id="delCustInfoBatch" parameterType="java.util.List" >
DELETE FROM CUSTINFO
WHERE SERIALID IN
<foreach collection="list" item="item" open="(" separator="," close=")" index="index" >
#{item.serialid,jdbcType=VARCHAR}
</foreach>
</update>
要做批量插入数据库,首先得知道该数据库对批量插入所支持的语法。
每一个数据库批量插入的语法都不一样。
mysql插入
<insert id="batchSave" parameterType="java.util.List">
INSERT INTO TABLE_NAME
( ID,
NAME
)
VALUES
<foreach collection="list" item="item" separator=",">
( #{item.id,jdbcType=VARCHAR},
#{item.name,jdbcType=VARCHAR}
)
</foreach>
</insert>