Spring Boot Learning (vii) Web applications use jdbctemplate multiple data source configuration

Source: Internet
Author: User
Tags xmlns

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;
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.