日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis 批量更新,批量更新

發布時間:2024/9/27 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis 批量更新,批量更新 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 INTO里面不要加id

<insert id="insertBatch" parameterType="java.util.List"><selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">SELECT LAST_INSERT_ID()</selectKey>INSERT INTOxxxxx(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=";">UPDATEXXX<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>WHEREid = #{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>

總結

以上是生活随笔為你收集整理的MyBatis 批量更新,批量更新的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。