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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mybatis拦截器 mysql load data local 内存流处理

發布時間:2025/3/20 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis拦截器 mysql load data local 内存流处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Mybatis?攔截器不做解釋了,用過的基本都知道,這里用load?data?local主要是應對大批量數據的處理,提高性能,也支持事務回滾,且不影響其他的DML操作,當然這個操作不要涉及到當前所load的數據,其中在使用的時候一定要local?,?這個命令使用是mysql規定的,否則不加則會認為是服務器本地的文件。這里主要是以流的方式來做處理,這樣可以使用內存流,這樣就可以避免在某些時候需要生成文件才能導入的多余操作,和IO性能消耗。也可以是應用本地的文件。

注:該做法只試用于存入數據的表,不試用于有頻繁更新,查詢操作的表。?因為load?命令的優先級比更新命令,及查詢命令的優先級低。

mybatis 插件配置

<plugins><plugin interceptor="com.yunat.channel.process.util.LoadDataInterceptor"><property name="databaseType" value="mysql"/></plugin> </plugins>

插入SQL

<select id="saveTest" parameterType="map">LOAD DATA LOCAL INFILE 'sql.csv' IGNORE INTO TABLE test (a,b,d) </select>

插件代碼

package com.yunat.channel.process.util;import java.io.InputStream; import java.sql.Statement; import java.util.Properties;import org.apache.ibatis.executor.statement.RoutingStatementHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature;@Intercepts({ @Signature(method = "update", type = StatementHandler.class, args = { Statement.class }) }) public class LoadDataInterceptor implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();BoundSql boundSql = handler.getBoundSql();if (boundSql.getSql().toLowerCase().contains("load data local infile")) {Object in = boundSql.getParameterObject();if (in != null && in instanceof InputStream) { // 如果不使用流的方式, 則會讀取語句中對應在本地的文件Statement statement = (Statement) invocation.getArgs()[0];if (statement.isWrapperFor(com.mysql.jdbc.Statement.class)) {com.mysql.jdbc.PreparedStatement mysqlStatement = statement.unwrap(com.mysql.jdbc.PreparedStatement.class);// 將流設置到執行語句中,在后續執行過程中,會忽略load data 語句中的文件名,改用當前設置流 mysqlStatement.setLocalInfileInputStream((InputStream)in);invocation.getArgs()[0] = mysqlStatement; // 將當前語句執行代理,換成mysql的語句對象,方便下面執行。 }}}return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}}

本文轉自:http://www.oschina.net/code/snippet_144320_24440#66175

轉載于:https://www.cnblogs.com/dreammyle/p/5533504.html

總結

以上是生活随笔為你收集整理的Mybatis拦截器 mysql load data local 内存流处理的全部內容,希望文章能夠幫你解決所遇到的問題。

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