当前位置:Gxlcms > 数据库问题 > 2500-使用MyBatis操作MySQL进行批量更新的注意事项

2500-使用MyBatis操作MySQL进行批量更新的注意事项

时间:2021-07-01 10:21:17 帮助过:18人阅读

{datasource.port}/${datasource.name}?relaxAutoCommit=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&useSSL=false&allowMultiQueries=true

2. 连接池需要配置

如果使用的数据库连接池是Druid,则需要额外配置参数。其他种类连接池,如C3P0,DBCP等,尚未考证。

## 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,此处去除防火墙
spring.datasource.druid.filters=config,stat,slf4j
## 配置过滤器wall的参数
spring.datasource.druid.filter.wall.config.use-allow=false
spring.datasource.druid.filter.wall.config.multi-statement-allow=true

wall是com.alibaba.druid.wall.WallFilter的简称,提供sql的检查和过滤等功能,默认这里会对混合SQL进行拦截,此处为了执行大SQL,可关闭防火墙功能。(有SQL注入风险,谨慎使用)


附上一个MyBatis的xml脚本demo:


    <update id="batchUpdateForRepay" parameterType="com.xxx.settle.credit.generate.domain.bo.RepayUpdateCreditInfoBO">

        <foreach collection="list" index="index" item="item" separator=";">
            update tb_credit_info
            <trim prefix="set" suffixOverrides=",">
                <if test="item.state != null">state = #{item.state},</if>
                <if test="item.leftPrincipalAmount != null">left_principal = left_principal - #{item.leftPrincipalAmount},</if>
                <if test="item.currentPeriodChange != null">current_period = current_period + #{item.currentPeriodChange},</if>
                <if test="item.receivedPrincipalAmount != null">received_principal = received_principal + #{item.receivedPrincipalAmount},</if>
                <if test="item.remark != null">remark = #{item.remark},</if>
            </trim>
            where product_id = #{item.productId} and credit_id = #{item.creditId}
        </foreach>

    </update>

2500-使用MyBatis操作MySQL进行批量更新的注意事项

标签:count   lam   source   ati   检查   数据库连接池   连接数   lis   rem   

人气教程排行