Multi data source configuration of SSM framework

Application scenario of multiple data sources: mainly how to combine multiple databases to meet business requirements after database splitting.

SSM framework (Spring + spring MVC + mybatis (mybatis plus)) is the most commonly used one at present, and it is still Maven project this time.

For this example of multiple data sources, I have uploaded it to my GitHub at: https://github.com/youcong1996/study_simple_demo.git

However, it should be noted that the branch is Demo1, not the main branch, as shown in the figure:

If you can't understand or understand the following example, you can git clone my address

In the world of programming, simplicity is perfection.

How to implement multiple data sources?

In a word, three classes plus XML configuration can achieve this goal.

1、 Write three classes

AbstractDynamicDataSource. java

DBContextHolder. java

DruidDynamicDataSource. java

2、 Modify profile

Mainly modify spring mybatis xml

3、 Unit test

After the test, the console is as shown in the figure:

Summary:

In fact, there are many ways to configure multiple data sources, including AOP and multiple beans. Of course, as long as the purpose can be achieved, it is the king. Of course, I also emphasize that it doesn't matter if the implementation is completed. Why is behind it more important than just implementation.

In fact, one thing I want to say is that sometimes when you encounter a problem, the best way is to solve the problem head-on, rather than escape or worry alone. At the same time, facing the problem directly is also the best way to solve anxiety. I have experienced this deeply.

In addition, the example of multiple data sources uploaded to GitHub is also the construction of SSM framework. Any friend who can't build a framework can refer to mine. I hope it can help you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>