MyBatis Interceptor MySQL load data local memory stream processing

Source: Internet
Author: User

Mybatis interceptors do not explain, used by the basic know, here with the load data local mainly to deal with large amounts of processing, improve performance, also support transaction rollback, and do not affect other DML operations, of course, this operation does not involve the current load of data, In the use of the time must be local, this command is specified by the MySQL, otherwise it will be considered a server local files. This is mostly done in a streaming way, so that you can use the memory stream so that you can avoid unnecessary operations that need to be generated at some point in order to import, and IO performance consumption. It can also be an application-local file.

Note: This practice only applies to tables that are stored in the data, not to the tables that have frequent updates and query operations. Because the load command has a lower priority than the Update command, and the query command.

MyBatis Plug-in configuration
< Plugins >   <  Interceptor= "Com.yunat.channel.process.util.LoadDataInterceptor">       <  name= "DatabaseType"  value= "MySQL"/>   </ plugin > </ Plugins >
Insert SQL
<id= "Savetest"  parametertype= "map">     LOAD DATA LOCAL INFILE ' sql.csv ' IGNORE into TABLE test (a,b,d)</Select>
Plug-in code
 PackageCom.yunat.channel.process.util;ImportJava.io.InputStream;Importjava.sql.Statement;Importjava.util.Properties;ImportOrg.apache.ibatis.executor.statement.RoutingStatementHandler;ImportOrg.apache.ibatis.executor.statement.StatementHandler;Importorg.apache.ibatis.mapping.BoundSql;ImportOrg.apache.ibatis.plugin.Interceptor;Importorg.apache.ibatis.plugin.Intercepts;Importorg.apache.ibatis.plugin.Invocation;ImportOrg.apache.ibatis.plugin.Plugin;Importorg.apache.ibatis.plugin.Signature; @Intercepts ({@Signature (method= "Update", type = Statementhandler.class, args = {Statement.class }) }) Public classLoaddatainterceptorImplementsInterceptor {@Override PublicObject Intercept (invocation invocation)throwsThrowable {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&& ininstanceofInputStream) {//if the stream is not used, the corresponding local file in the statement is readStatement 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); //The stream is set to the execution statement, and the file name in the load data statement is ignored during subsequent executions, using the current settings stream insteadMysqlstatement.setlocalinfileinputstream ((InputStream) in); Invocation.getargs () [0] = mysqlstatement;//The current statement executes the proxy and is replaced by the MySQL statement object for easy execution.                 }            }        }        returninvocation.proceed (); } @Override PublicObject Plugin (object target) {returnPlugin.wrap (Target, This); } @Override Public voidSetProperties (Properties properties) {}}

This article transferred from: http://www.oschina.net/code/snippet_144320_24440#66175

MyBatis Interceptor MySQL load data local memory stream processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.