Spring dynamically switches data sources

Spring dynamically switches data sources
using Spring-related functions to dynamically switch data sources. It mainly uses the AbstractRouteDataSource provided by Spring. When performing a data read operation, query the specified read database, and when performing a write data operation, write data to the specified database.
1. Go to the relevant configuration files:
Spring-beans.xml:



 

2. Dynamic data source class DynamicDataSource:




 



3. Use ThreadLocal to set/get the data source type:



 

4. Code calling example:


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326495441&siteId=291194637