Detailed mybatis multi data source configuration (read-write separation)

Mybatis multi data source configuration (read write separation)

First of all, the most direct way of configuration in this paper may be troublesome in practice.

There may be many combinations in practical application. You can understand the meaning of this article and don't use it rigidly.

Possible scenarios for multiple data sources

1. Master slave

It is usually the case of MySQL with one master and multiple slaves. The example in this paper is the case of master and slave, but there are only two data sources, so the direct configuration will not be too troublesome, but it is not conducive to subsequent expansion. It is mainly used as an example to illustrate. Please consider the actual operation carefully.

2. Sub warehouse

When the business independence is strong and the amount of data is large, in order to improve concurrency, tables may be divided into databases. After the database is divided, each database needs to be configured with a data source.

In this case, you can refer to this article, but it should be noted that the mapper corresponding to each database should be easily distinguished and configured under different packages.

In addition, in the case of sub databases, there will also be master-slave situations. If your database has too many slave databases, please refer to the methods provided above or find other ways to solve them.

Mapper subcontracting

In the case of sub databases, mappers of different databases must be placed in different packages.

In the case of master-slave, the same mapper will be read and written at the same time. It is not appropriate to create two. Use the same mapper. However, in this case, it should be noted that the names and types automatically generated by spring for mapper are the same, so it is not possible to directly inject mapper interface. It needs to be solved through sqlsession.

Spring basic configuration

applicationContext. xml

This file mainly introduces spring datasource master XML and spring datasource slave xml。

spring-datasource-master. xml

spring-datasource-slave. xml

It is not very different from the master, mainly because the ID name and data source configuration are different.

Note that < TX: method name = "*" read only = "true" / > is read-only. If it is not a slave library, it can be configured according to the master library.

In the following code:

You must specify a different sqlsessionfactory through sqlsessionfactorybeanname.

config. properties

Using mapper

This is set for the master-slave situation. The mappers scanned by the two configurations are the same, so they cannot be injected directly. They need to be injected through the following troublesome methods.

Because sqlsessions can be distinguished by name, mapper is obtained from sqlsession.

In addition, if you need to consider writing and reading in the same transaction, you need to use the same writemapper, so that you can obtain the latest data in the transaction when reading.

The above is the master-slave situation.

In the case of sub database, because different mappers are in different packages, you can directly inject mapper using @ resource or @ Autowired without obtaining it through sqlsession.

This article is only a reference for multiple data sources. Please consider it according to your own situation in practical application.

In the future, I will try to develop plug-ins that can automatically switch data sources for MySQL multiple data sources on the basis of this article and the above two related links in my spare time. Because I am not very familiar with the practical application in this field, you are welcome to leave a message and share your solutions. The more you know about these, The more likely it is to develop a general data source switching plug-in.

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