Mapper的内容如下:
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的内容如下:
<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>
批量更新
具体参考:https://blog.csdn.net/xyjawq1/article/details/74129316
Mapper.java文件中的定义如下:
public void updateBatchById(@Param("keywordsList")List<CommunityKeywords> keywordsList);
mapper.xml中的内容如下
<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>
方法二:
<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>
这种方法可能会报:
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给控制住了,解决办法是:
<!-- 配置数据源 -->
<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>