In the previous article, we used a single data source connection to access, insert data, but often as the volume of business development, we usually do the database split or introduce other databases, so we need to configure multiple data sources, The following is based on the previous jdbctemplate and SPRING-DATA-JPA examples of how the two multiple data sources are configured.
First look at the jar referenced by the pom file
?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--> </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 used to read different configurations in the application.properties. In the following example, the main data source is configured as a spring.datasource.primary start configuration, and the second data source is configured as the spring.datasource.secondary at the beginning of the configuration.
01 |
Package Com.xiaojingg; |
03 |
Import Org.springframework.beans.factory.annotation.Qualifier; |