Spring + mybatis realizes AOP database read-write separation and multi database source configuration

At the database level, read-write separation technology is mostly used, that is, one master database and multiple slave databases. The master library is responsible for data update and real-time data query, and the slave library is certainly responsible for non real-time data query. Because in practical applications, Databases are read more and write less (the frequency of reading data is high, and the frequency of updating data is relatively low). Reading data usually takes a long time and occupies more CPU of the database server, which affects the user experience. Our usual practice is to extract queries from the main database, adopt multiple slave databases, and use load balancing to reduce the query pressure of each slave database.

Needless to say, the principle of multi data source configuration is the same as that of master-slave data configuration

1. First configure JDBC Properties two databases A and B

2. Configure spring mybatis XML file [important]

3. Write several Java classes to dynamically call the data source [important]

A: customize an annotation that is responsible for dynamically calling the data source

b. Data source acquisition object AOP implementation (reflection)

c. Datasourceholder data source operation get data source help class

d. We also need to implement spring's abstract class abstractroutingdatasource, which is to implement the determinecurrentlookupkey method:

4. Then we can see the results

I call it directly at the Dao layer

summary

The above is what Xiaobian introduced to you. Spring + mybatis realizes AOP database read-write separation and multi database source configuration. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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