Talk about the use of sub-library sub-table Mybatis

Sub-libraries

Sometimes when sub-library for the convenience of some of the tables need to store information about all libraries, called global library. Such as: user table to store all users.

At this point the idea database is divided into sub-library of global libraries and business library, business library where the library is divided into a plurality of N, global library just put individual tables to facilitate the development.

This time we need a global DAO, then we need to support two of Mybatis DAO

Two DAO (bizDao and globalDao) need two sqlSessionFactory, bizSqlSessionFactory and globalSqlSessionFactory and two things managers transactionManager

The specified service database requests a dynamic DataSource

Is a sub-library project has multiple databases, there are multiple data sources (dataSource) in order to specify which data source is dynamically assigned to Myabtis according to your request to determine which library should query

Spring can inherit the AbstractRoutingDataSource rewritten interface determineCurrentLookupKey () Returns the current dbkey according to business needs (which library 1,2,3,4 ...)

Global libraries and business library of things Manager

Because things are Service-based, it is also recommended into Service and Global Service Business Service, according to the type of Service (AOP recommendations with a global process into a ThreadLocal)

Implement PlatformTransactionManager interfaces, interface The getTransaction () method may return a dynamic object specified manager (manager to get something from the current above a ThreadLocal)

Question: Why AbstractRoutingDataSource multiple DataSource things do not need dynamic configuration manager?

Summary: business library and global libraries have two different DataSource, DataSource business library which inherited the Spring AbstractRoutingDataSource ,

Spring is divided by N the DataSource (in terms of only a bizSqlSessionFactory RoutingDataSource, RoutingDataSource which has a collection of data sources )

Global globalSqlSessionFactory also when bizSqlSessionFactory in AbstractRoutingDataSource a property to do so while reducing configuration, but the development is not intuitive.

Points table

Ideas points above the library is dynamically assigned DateSource and TransactionManager

Score sheet: interceptor is to write a dynamic change in the table name in Mybatis

1. interceptor

SqlSessionFactoryBean inside a private Interceptor [] plugins; attributes can be configured interceptors

Our own definition of interceptors to implement Interceptor Interface

Interfaces need to add the following comment

@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class}) })

2. In the interceptor which we need to change the dynamic SQL and SQL parsing

@Intercepts({
    @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})
})
public class MybatisInterceptor implements Interceptor {
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        if (invocation.getTarget() instanceof StatementHandler) {
            RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
            //通过反射拿到statement对象
            StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
            BoundSql boundSql = delegate.getBoundSql();
            String sql = boundSql.getSql();
            String pageSql =sql+" limit 1 ";//操作sql
            //再通过反射把新sql设置进去
            ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
        }
        return invocation.proceed();
    }

Mybatis four objects, and plug-in principle

Guess you like

Origin www.cnblogs.com/ssskkk/p/11109597.html