MyBatis 批量更新,批量更新

Mapper的内容以下:java

package com.xxx.user.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.xxx.user.entity.TbSysUserPhoneBelong;

import java.util.List;

/** * <p> * 功能: * </p> * * @author tuzq * Copyright 2018 xxx.com, Inc. All rights reserved * @version v1.0 * @ClassName: xxxx * @date 2018/3/22 */
public interface TbSysUserPhoneBelongMapper extends BaseMapper<TbSysUserPhoneBelong> {

    /** * 归属地相关的批处理 * @param phoneBelongs :归属地list */
    public void insertBatch(@Param("phoneBelongs")List<TbSysUserPhoneBelong> phoneBelongs);
}

xml的内容以下:web

<insert id="insertBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO
            xxxxx
            (
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx,
                xxx
            )
        VALUES
        <foreach collection="xxxxx" item="item" index="index" separator=",">
            (
                #{item.id},
                #{item.xxx},
                #{item.xxx},
                #{item.types},
                #{item.xxx},
                #{item.xxx},
                #{item.xx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx},
                #{item.xxx}
            )
        </foreach>

批量更新
具体参考:http://www.javashuo.com/article/p-fvinnmyg-mh.htmlsql

Mapper.java文件中的定义以下:数据库

public void updateBatchById(@Param("keywordsList")List<CommunityKeywords> keywordsList);

mapper.xml中的内容以下ruby

<update id="updateBatch" parameterType="java.util.List">
        update mydata_table
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                     when id=#{item.id} then #{item.status}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

方法二:mybatis

<update id="updateBatchById" parameterType="java.util.List">
        <foreach collection="keywordsList" item="item" index="index" separator=";">
            UPDATE
                XXX
            <set>
                <trim suffixOverrides=",">
                    <if test="item.articleId != null">
                        article_id = #{item.articleId},
                    </if>
                    <if test="item.keywords != null">
                        keywords = #{item.keywords},
                    </if>
                    xxxxxx
                </trim>
            </set>
            WHERE
            id = #{item.id}
        </foreach>
    </update>

这种方法可能会报:app

Caused by: java.sql.SQLException: sql injection violation, multi-statement not allow : update device_bd_token 
                 SET access_token=? 
                where device_id = ?
          ; 
                update device_bd_token 
                 SET access_token=? 
                where device_id = ?
    at com.alibaba.druid.wall.WallFilter.check(WallFilter.java:714)
    at com.alibaba.druid.wall.WallFilter.connection_prepareStatement(WallFilter.java:240)
    at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448)
    at com.alibaba.druid.filter.FilterAdapter.connection_prepareStatement(FilterAdapter.java:928)
    at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:122)
    at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448)
    at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342)
    at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:318)

缘由是druid给控制住了,解决办法是:ide

<!-- 配置数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}"/>
        <property name="username" value="${jdbc_username}"/>
        <property name="password" value="${jdbc_password}"/>
        <!-- 初始化链接大小 -->
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <!-- 链接池最大使用链接数量 -->
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <!-- 链接池最小空闲 -->
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <!-- 获取链接最大等待时间 -->
        <property name="maxWait" value="${jdbc.maxWait}"/>
        <property name="validationQuery" value="${validationQuery}"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="true"/>
        <!-- 配置间隔多久才进行一次检测,检测须要关闭的空闲链接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个链接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
        <!-- 关闭abanded链接时输出错误日志 -->
        <property name="logAbandoned" value="true"/>
        <!-- 监控数据库 wall sql防火墙,注意这里的wall-filter,默认是wall,这里使用咱们本身定义的wall-filter -->
        <property name="filters" value="mergeStat,wall-filter"/>
        <!-- 支持emoji表情 -->
        <property name="connectionInitSqls" value="set names utf8mb4;"/>
    </bean>


    <!-- 下面两个bean是增长的过滤器,为了解决批量更新被拦截了的问题 -->
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="config" ref="wall-config" />
    </bean>
    <bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
        <property name="multiStatementAllow" value="true"/>
    </bean>