zoukankan      html  css  js  c++  java
  • 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 {
     
        @Override
        public 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();
        }
     
        @Override
        public Object plugin(Object target) {
            return Plugin.wrap(target, this);
        }
     
        @Override
        public void setProperties(Properties properties) {
     
        }
     
    }

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

  • 相关阅读:
    一起来学SpringBoot(十七)优雅的参数校验
    使用JDBC创建出版社和书籍管理系统
    springMvc(初识+操作步骤)
    模拟Java-Sping,实现其IOC和AOP核心
    python多个装饰器的执行顺序
    JAVA——不简单的fianl关键字
    Java HTTP 组件库选型看这篇就够了
    这一次,我连 web.xml 都不要了,纯 Java 搭建 SSM 环境!
    13数据结构与算法分析之---链式栈
    12数据结构与算法分析之---顺序栈
  • 原文地址:https://www.cnblogs.com/dreammyle/p/5533504.html
Copyright © 2011-2022 走看看