Spring boot + mybatis multi data source switching (example explanation)
Since the company's business is divided into multiple databases, colleagues will call multiple databases when developing a project. After learning, we implemented it in the way of annotation + AOP
1. First define an annotation class
2. Then create a configuration class. The data source will be loaded when the project is started. At the beginning, hikaricp is used. According to the data, it is the fastest and best performance. Then, Ali Druid is found. This function is comprehensive and the performance is OK. The most important thing is that it also has monitoring function. See the following code for the specific implementation
3. Dynamic data source: select from previously loaded data sources, and use dynamicdatasource and dynamicdatasourceholder together
4. Use AOP to switch data sources at Dao layer
All data connections are configured in XML
The XML path is configured in the configuration file, which is suitable for read-write separation and multiple different data sources, and multiple projects can share this configuration
Finally, reference the annotation. It should be noted that the database name of the annotation corresponds to the databasename node in XML one by one. You can customize it freely. For example, reading and writing is a database name, which can be defined as printtest at this time_ R means read Library
So far, the configuration of multiple data sources has been completed. As for Ali's Druid, the code will be posted next time. If you feel that there are still some deficiencies, please correct them.
The above spring boot + mybatis multi data source switching (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.