In the previous article, we talked about single data connection access, inserting data, but often as the traffic grows, we usually split the database or introduce other databases, so we need to configure multiple data sources, The configuration of two multi-data sources is described below, based on the previous jdbctemplate and SPRING-DATA-JPA examples.
See the jar referenced by the pom file first
<?XML version= "1.0" encoding= "UTF-8"?> <Project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>com.xiaojingg</groupId> <Artifactid>springbootstudy-demo7-jdbctemplate-more</Artifactid> <version>0.0.1-snapshot</version> <Packaging>jar</Packaging> <name>springbootstudy-demo7-jdbctemplate-more</name> <Description>demo Project for Spring boot</Description> <Parent> <groupId>org.springframework.boot</groupId> <Artifactid>spring-boot-starter-parent</Artifactid> <version>1.5.8.release</version> <RelativePath/> <!--lookup parent from repository to </Parent> <Properties> <project.build.sourceEncoding>utf-8</project.build.sourceEncoding> <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </Properties> <Dependencies> <Dependency> <groupId>org.springframework.boot</groupId> <Artifactid>spring-boot-starter</Artifactid> </Dependency> <Dependency> <groupId>org.springframework.boot</groupId> <Artifactid>spring-boot-starter-test</Artifactid> <Scope>test</Scope> </Dependency> <Dependency> <groupId>mysql</groupId> <Artifactid>mysql-connector-java</Artifactid> <version>5.1.21</version> </Dependency> <Dependency> <groupId>org.springframework.boot</groupId> <Artifactid>spring-boot-starter-jdbc</Artifactid> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>org.springframework.boot</groupId> <Artifactid>spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build> </Project>
Multi-Data source configuration
Create a spring configuration class that defines two datasource to read the different configurations in the application.properties. In the following example, the primary data source is configured with a configuration beginning with Spring.datasource.primary, and the second data source is configured with a configuration beginning with spring.datasource.secondary.
01 |
Package Com.xiaojingg; |
03 |
Import Org.springframework.beans.factory.annotation.Qualifier; |