Spring + mybatis multi data source configuration implementation example

Recently, mybatis was used to configure multiple data sources. Originally, it was thought that a simple configuration would be enough. After actual operation, it was found that it would take some trouble. Record it here for memo

No more nonsense, just go to the code, and there will be a simple implementation introduction later

Configuration of JDBC and log4j

Spring configuration file for single data source

Spring configuration file for multiple data sources

Multipledatasource implementation

Mybatis interface mapper definition, which is directly implemented by annotation

Manual data source switching call

Automatic switching using spring AOP

Call log

Here is a brief explanation of the above implementation. When we configure a single data source, we can see that the data source type uses org apache. commons. dbcp. Basic datasource, and this code implements javax sql. Datasource interface

Error configuring sqlsessionfactory. Org mybatis. spring. The sqlsessionfactorybean injection parameter datasource type is javax sql. DataSource

The way to implement multiple data sources is to customize a multipledatasource. This class inherits from abstractroutingdatasource, and abstractroutingdatasource inherits from abstractdatasource, which implements javax sql. Datasource interface, so our multipledatasource also implements javax sql. Datasource interface, which can be assigned to the datasource property of sqlsessionfactory

Let's talk about the implementation principle of multipledatasource. Multipledatasource implements the abstractroutingdatasource abstract class, and then implements the determinecurrentlookupkey method, which is used to select which data source in targetdatasources to use

It can be seen that the multipledatasource in the spring configuration has two properties defaulttargetdatasource and targetdatasources set. These two properties are defined in abstractroutingdatasource. When mybatis executes the query, the data source will be selected first. When selecting the order, it will be found in targetdatasources according to the value returned by the determinecurrelllookupkey method, If you can find how to return the corresponding data source, if you can't find the default data source defaulttargetdatasource, please refer to the source code of abstractroutingdatasource for details

AOP mode is selected for dynamic data source switching. The implementation here is simple and rough. It can be flexible according to actual needs in specific applications

As an aside, here is a question about the choice of sqlserver drivers. At present, there are mainly two kinds of sqlserver drivers: Microsoft's official driver and JTDS driver. I have tested these two drivers and updated them in batches. When the amount of data is small (below 100), JTDS has slightly higher performance than Microsoft driver. When the amount of data increases by tens of thousands to millions, Microsoft driver has obvious advantages, Therefore, if it is sensitive to performance, it is recommended to use Microsoft driver, otherwise it is optional

Microsoft driver can't be found in Maven library, which is rather frustrating. If you use maven, you have to install it locally first, which is very uncomfortable

JTDS is easy to use, and Maven can directly reference it

Related jar Maven references

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>