Due to work needs, xiaobian recently developed a simple automatic shadow table switching plug-in based on Mybatis interceptor, which can dynamically modify the table name according to the configuration, that is, the operation of the original Source table is automatically switched to the operation of the target table. The plug-in can support both simple single-table operations and complex multi-table joint operations.

Mybatis shadow table switch plugin

How to implement mybatis to customize the plugin?

1. Core components of MyBatis

From the perspective of MyBatis code implementation, the main core components of MyBatis are as follows:

Name Description SqlSession is the main top-level API of MyBatis, which represents the session to interact with the database. It is the core of MyBatis scheduling. SQL Statement generation and query cache maintenance StatementHandler encapsulates JDBC Statement operations and is responsible for JDBC Statement operations. ParameterHandler converts user-passed parameters into parameters required by the JDBC Statement ResultSetHandler TypeHandler is responsible for the mapping and conversion between Java and JDBC data types. MappedStatement MappedStatement maintains a wrapper for a node SqlSource is responsible for dynamically generating SQL statements based on the parameterObject passed by the user, encapsulating the information into BoundSql objects. BoundSql is BoundSql, indicating dynamically generated SQL statements and corresponding parameter information. Configuration MyBatis All Configuration information is maintained in the Configuration object

2. Hierarchical structure relationship of core components of MyBatis

3. How to scale?

Based on the MyBatis Interceptor plug-in mechanism

  • MyBatis provides an Executor method for intercepting an Executor, a ParameterHandler method for intercepting an argument, and a ParameterHandler method for intercepting an argument. ResultSetHandler (intercepting result set processing), StatementHandler (intercepting Sql syntax construction processing), and the message “the details of the methods in these classes can be found by looking at the signature of each method, or directly look at the source code in the MyBatis distribution package”.
  • The use scenario of interceptor is mainly to update the general field of the database, sub-database sub-table, encryption and decryption and so on
  • We use the Interceptor mechanism to intercept the executed Sql and append logic. The sample code is as follows:
/** * @author zhangjb * @ClassName: MyInterceptor <br> * @Description: Test mybatis interceptor < p > < br > * * intercept method {@ link org. Apache. Ibatis. Executor. Statement. StatementHandler# prepare (Connection, Integer)} */ @Slf4j @Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}) public class MyInterceptor implements Interceptor { ..... @Override public Object intercept(Invocation invocation) throws Throwable { try { // do something } catch (Throwable throwable) { log.warn("MyInterceptor: {}", Throwables.getStackTraceAsString(throwable)); } return invocation.proceed(); }}... }Copy the code

4. Compatibility

In addition, we need to pay attention to the compatibility of mybatis- Spring, MyBatis and Spring. The official compatibility scheme is as follows: