brief introduction
I believe that if you have configured too many data sources or are about to configure too many data sources, you will find the following solutions on the Internet:
- 1. Use AOP slice to switch dynamic data source
- 2. Use basePackages of MapperScan to configure different mapper directories and template s
- 3. Database agent middleware Both of them can realize multiple data sources, but each has its own disadvantages:
- 1. The disadvantage of not being able to realize multi data source XA things (Global things Management | JTA) is very fatal. With multi data sources but no global things, it's useless. There are many post tutorials on the Internet. Although the configuration is a little simple, if you want to use the whole thing, it's useless.
- 2. This way can be combined with JTA to achieve global things. At present, I also use this way to run online in my business. After many debugging and online operation and maintenance, there is no doubt that one problem is trouble! You will encounter the need to integrate the druid connection pool and global things. If you find online posts to follow the process, you may have some small problems.
- 3. Data source agent may be a good way, which is adopted by most giant companies, and developers and business personnel do not need to think about these problems. Cutting and connection pool can be realized by using agent, but there is a very awkward problem that open source is not easy to use, because many of them are not suitable for their own business, and their own development costs are not too high.
At this time, I am thinking about a problem. Why not introduce a dependency configuration in the yml file directly like other language frameworks? All of them have the simplest solution now.
Source address
I hope you can support it with star. In the future, we will add simple integration of other dependencies. https://github.com/louislivi/fastdep
start
Edition:
springboot: 2.0+
Introduce dependency:
- Maven
<dependency> <groupId>com.louislivi.fastdep</groupId> <artifactId>fastdep-datasource</artifactId> <version>1.0.1</version> </dependency>
- Gradle
compile group: 'com.louislivi.fastdep', name: 'fastdep-datasource', version: '1.0.1'
application.yml configuration file:
fastdep: datasource: test: #Data source name can be selected at will mapper: com.louislivi.fastdep.test.mapper.test #The mapper directory corresponding to the current data source cannot be the same with multiple data sources password: 123456 url: jdbc:mysql://127.0.0.1:3306/douyin?serverTimezone=Asia/Chongqing&useLegacyDatetimeCode=false&nullNamePatternMatchesAll=true&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false&autoReconnect=true&useSSL=false&pinGlobalTxToPhysicalConnection=true driverClassName: com.mysql.cj.jdbc.Driver username: root # # The following are the supplementary settings for the druid connection pool # initialSize: 10 # minIdle: 5 # maxActive: 100 # connectionInitSqls: 'set names utf8mb4;' test2: #Data source name can be selected at will mapper: com.louislivi.fastdep.test.mapper.test2 #The mapper directory corresponding to the current data source cannot be the same with multiple data sources password: 123456 url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=Asia/Chongqing&useLegacyDatetimeCode=false&nullNamePatternMatchesAll=true&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false&autoReconnect=true&useSSL=false&pinGlobalTxToPhysicalConnection=true driverClassName: com.mysql.cj.jdbc.Driver username: root # # The following are the supplementary settings for the druid connection pool # initialSize: 10 # minIdle: 5 # maxActive: 100 # connectionInitSqls: 'set names utf8mb4;'
Is this the end? Yes, it's that simple. Isn't it very simple? When you go to the online poster, you will find that the same thing is to teach you to always add files for configuration. If MapperScan adds a data source, you need to add another java class every time.
principle
Using importbeandefinitionregister beandefinitionbuilder.genericbeandefinition to dynamically inject a Bean is actually very simple and interesting to see the source code.
Last
To prevent other problems from posting the dependencies introduced under my complete pom.xml:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>com.louislivi.fastdep</groupId> <artifactId>fastdep-datasource</artifactId> <version>1.0.1</version> </dependency> </dependencies>
I hope you can support open source, give a little star, and continue to develop other dependent integrations and the current optimization of multiple data, support Hibernate, etc., and even be compatible with other frameworks. fastdep makes java integration dependency easier. We are also looking for coder s with similar aspirations to improve this project.