版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/danruoshui315/article/details/89466489
本文借鉴自:https://www.cnblogs.com/zrbfree/p/8378680.html
由于工作中经常需要对一个集合数据进行增、删、改,如果sql直接写进for循环里,大大影响了性能,每次循环一次打开关闭一次数据库连接,数据量小没事,如果数据量大对数据库的性能是大大的降低,执行速率也下降,所以就需要用到批量增、删、改。
废话不多说了,先上代码:
批量更新代码:
<update id="batchUpdateIssueCollect" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
UPDATE tableName
<set>
<if test="item.billStatus!=null">BILL_STATUS=#{item.billStatus,jdbcType=VARCHAR},</if>
<if test="item.auditEmpNo!=null">AUDIT_EMP_NO=#{item.auditEmpNo,jdbcType=VARCHAR},</if>
<if test="item.auditEmpName!=null">AUDIT_EMP_NAME=#{item.auditEmpName,jdbcType=VARCHAR},</if>
<if test="item.auditOrgCode!=null">AUDIT_ORG_CODE=#{item.auditOrgCode,jdbcType=VARCHAR},</if>
<if test="item.auditOrgName!=null">AUDIT_ORG_NAME=#{item.auditOrgName,jdbcType=VARCHAR},</if>
<if test="item.auditTime!=null">AUDIT_TIME=#{item.auditTime,jdbcType=TIMESTAMP},</if>
<if test="item.collectTime!=null">COLLECT_TIME=#{item.collectTime,jdbcType=TIMESTAMP},</if>
<if test="item.payType!=null">PAY_TYPE=#{item.payType,jdbcType=VARCHAR},</if>
<if test="item.payTypeName!=null">PAY_TYPE_NAME=#{item.payTypeName,jdbcType=VARCHAR},</if>
<if test="item.custNo!=null">CUST_NO=#{item.custNo,jdbcType=VARCHAR},</if>
<if test="item.custName!=null">CUST_NAME=#{item.custName,jdbcType=VARCHAR},</if>
<if test="item.billNo!=null">BILL_NO=#{item.billNo,jdbcType=VARCHAR},</if>
<if test="item.amount!=null">AMOUNT=#{item.amount,jdbcType=NUMERIC},</if>
<if test="item.pertainProvinceCode!=null">PERTAIN_PROVINCE_CODE=#{item.pertainProvinceCode,jdbcType=VARCHAR},</if>
<if test="item.pertainProvinceName!=null">PERTAIN_PROVINCE_NAME=#{item.pertainProvinceName,jdbcType=VARCHAR},</if>
<if test="item.pertainOrgCode!=null">PERTAIN_ORG_CODE=#{item.pertainOrgCode,jdbcType=VARCHAR},</if>
<if test="item.pertainOrgName!=null">PERTAIN_ORG_NAME=#{item.pertainOrgName,jdbcType=VARCHAR},</if>
<if test="item.modifyEmpNo!=null">MODIFY_EMP_NO=#{item.modifyEmpNo,jdbcType=VARCHAR},</if>
<if test="item.modifyEmpName!=null">MODIFY_EMP_NAME=#{item.modifyEmpName,jdbcType=VARCHAR}</if>
</set>
WHERE ID = #{item.id,jdbcType=VARCHAR}
</foreach>
</update>
mapper接口代码:
void batchUpdateIssueCollect(@Param("list") List<TStlBnetIssueCollectEntity> list);
void delCustIssueCollectBillById(@Param("list") List<String> list);
void batchInsert(List<TStlBnetIssueCollectEntity> list);
批量删除代码:
<delete id="batchDelById" parameterType="java.util.List">
DELETE FROM tableName T
WHERE
<if test="list != null">
T.ID in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</delete>
批量新增代码:
<insert id="batchInsert" parameterType="java.util.List">
INSERT tableName T (T.ID,
T.COLLECT_NO,
T.PERTAIN_PROVINCE_CODE,
T.PERTAIN_PROVINCE_NAME,
T.PERTAIN_ORG_CODE,
T.PERTAIN_ORG_NAME,
T.CUST_NO,
T.CUST_NAME,
T.PAY_TYPE,
T.PAY_TYPE_NAME,
T.AMOUNT,
T.BILL_STATUS,
T.COLLECT_TIME,
T.BILL_NO,
T.SOURCE_CODE,
T.SOURCE_NAME,
T.REMARK,
T.CREATE_EMP_NO,
T.CREATE_EMP_NAME,
T.CREATE_ORG_CODE,
T.CREATE_ORG_NAME,
T.CREATE_TIME) (
<foreach collection="list" item="item" index= "index" separator ="UNION ALL">
select
#{item.id,jdbcType=VARCHAR},#{item.collectNo,jdbcType=VARCHAR},#{item.pertainProvinceCode,jdbcType=VARCHAR},
#{item.pertainProvinceName,jdbcType=VARCHAR},#{item.pertainOrgCode,jdbcType=VARCHAR},#{item.pertainOrgName,jdbcType=VARCHAR},
#{item.custNo,jdbcType=VARCHAR},#{item.custName,jdbcType=VARCHAR},#{item.payType,jdbcType=VARCHAR},#{item.payTypeName,jdbcType=VARCHAR},
#{item.amount,jdbcType=NUMERIC},#{item.billStatus,jdbcType=VARCHAR},#{item.collectTime,jdbcType=TIMESTAMP},
#{item.billNo,jdbcType=VARCHAR},
#{item.sourceCode,jdbcType=VARCHAR},#{item.sourceName,jdbcType=VARCHAR},#{item.remark,jdbcType=VARCHAR},
#{item.createEmpNo,jdbcType=VARCHAR},
#{item.createEmpName,jdbcType=VARCHAR},#{item.createOrgCode,jdbcType=VARCHAR},#{item.createOrgName,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP}
from dual
</foreach>
)
</insert>