Spring Boot Learning (vii) Web App uses JdbcTemplate multi-data source configuration

Source: Internet
Author: User

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;
02
03 Import Org.springframework.beans.factory.annotation.Qualifier;
04

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.