Item dual spring separate read and write data source

  We are the first to do a new project always wanted to read and write when separated from the main database from the synchronization, for some reason has not to do this thing, we need to configure the causes of this double data source because we made a new project with the another database, the database needs to display data to the original later. Separate read and write before nature is actually written into the main library, read from the library, is to switch multiple data sources, there are a lot of middleware can help us to achieve complete also have their own, most of the practices are implemented by aop, then the service operation put the data source is switched. Realization of ideas are about the same, the specific use is eyes of the beholder wise see wisdom.

  Step 1: Set the data source, in the original configuration file with new data source

  

 

 

   Step two: Modify mybatis configuration sqlSessionFactory

  

 

 

   The third step: modify transaction transactionManager

  

  Step Four: Configure dateSource references

  

   Step Five: define a data source class switch

  

 

  Final step: annotation embodiment are denoted defined class or method which requires the use of a new data source via the interceptor intercepts all requests According existing business decisions

  Defined Note

  

 

   Connector Configuration

  

 

   Create source switching for data interceptor (not postHandle Method)

  

 

   

 

   Then re-requires the use of a class or method plus @TestDataSource ( "testDataSource") can switch the data source. There are many ways to use paper introduces the only business I used are ultimately use to switch classes to switch databases dateSource. Front-end time has been very busy, not retired and sit to learn something new project in the busy process also encountered some problems not seen me, I will write it down and share. This article describes the springmvc only used, but also when the same is in springboot, can also be achieved by way of comment.

  

 

 

Guess you like

Origin www.cnblogs.com/HuuuWnnn/p/11814246.html