JdbcTemplate Configuring multiple data sources (completely copied, complete)

Source: Internet
Author: User
Tags assert

Tag:rom   plugins   dem    Full    ota    reporting   port   boot   demo   

<?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>top.zekk </groupid><artifactid>two-datasource</artifactid><version>0.0.1-snapshot</version ><packaging>jar</packaging><name>two-datasource</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-data-jpa</artifactid>--><!--</dependency>-->< dependency><groupid>org.projectlombok</groupid><artifactid>lombok</artifactid></ dependency><dependency><groupid>mysql</groupid><artifactid>mysql-connector-java< /artifactid></dependency><dependency><groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid></dependency><dependency><groupid >org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>< Scope>test</scope></dependency></dependencies><build><plugins><plugin> <groupid>org.springframeworK.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></plugin></plugins ></build></project>

Package Top.zekk.twodatasource.config;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.boot.autoconfigure.jdbc.datasourcebuilder;import Org.springframework.boot.context.properties.configurationproperties;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.primary;import Org.springframework.jdbc.core.jdbctemplate;import Javax.sql.DataSource, @Configurationpublic class Datasourceconfig {@Bean (name = "Primarydatasource")//@Qualifier ("p Rimarydatasource ")//Yumbo Lord here plus @qualify there is no need to delete or affect @ConfigurationProperties (" spring.datasource.primary ") public DataS    Ource Primarydatasource () {return datasourcebuilder.create (). build ();     } @Primary @Bean (name = "Secondarydatasource")//@Qualifier ("Secondarydatasource")//Yumbo Lord here Plus @qualify no need to delete or affect @ConfigurationProperties ("Spring.datasource.secondary") public datasource SecondaRydatasource () {return datasourcebuilder.create (). build (); }/** * @param dataSource * @return * @Bean clearly indicates a method, what method--a method of generating a bean, and handing it over to the spring container; From here we understand why @bean is put in Comment on the method, * because it tells the annotated method very clearly, you give me a Bean and then hand it to the spring container, * Implement Dependency Injection */@Bean (name = "Primaryjdbctemplate") pub Lic JdbcTemplate primarytemplate (@Qualifier ("Primarydatasource") DataSource DataSource) {return new JdbcTemplate (d    Atasource); } @Bean (name = "Secondaryjdbctemplate") public JdbcTemplate secondarytemplate (@Qualifier ("Secondarydatasource") Data    Source DataSource) {return new JdbcTemplate (DataSource); }}

  

Test determines whether the data source is configured successfully by testing the primary key

Package Top.zekk.twodatasource;import Org.junit.assert;import Org.junit.before;import org.junit.Test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.boot.test.context.springboottest;import Org.springframework.context.annotation.bean;import Org.springframework.jdbc.core.jdbctemplate;import Org.springframework.test.context.junit4.SpringRunner; @RunWith  (springrunner.class) @SpringBootTestpublic class Twodatasourceapplicationtests {@Autowired/* @Qualify more than one type of bean? When present Spring does not know which implementation class should be bound to specify the class name of the binding @bean (name= "* *") */@Qualifier ("Primaryjdbctemplate") protected JdbcTemplate jdbcTemplate1 ; @Autowired//@Qualify more than one type of Bean? The class name @bean (name= "* *") that specifies the binding at the time of existence @Qualifier ("secondaryjdbctemplate") protected JdbcTemplate jdbcTemplate2; @Before//First perform the empty database operation public void SetUp () {jdbctemplate1.update ("DELETE from USER"); Jdbctemplate2.update ("DELETE from USER");} @Testpublic void Test () {JDBCTEMplate1.update ("INSERT into User (Id,name,age) VALUES (?,?,?)", 1, "AAA", and Jdbctemplate2.update ("INSERT into user (ID, Name,age) VALUES (?,?,?) ", 1," BBB "," Jdbctemplate2.update "(" INSERT into USER (id,name,age) VALUES (?,?,?) ", 2," BBB ", 31 ); Assert.assertequals ("1", Jdbctemplate1.queryforobject ("SELECT count (1) from user", String.class)); Assert.assertequals ("2", Jdbctemplate2.queryforobject ("SELECT count (1) from user", String.class));}}

Database

DROP TABLE IF EXISTS ' user '; CREATE TABLE ' user ' (  ' id ' int (one) ' NOT null auto_increment,  ' name ' varchar (255) DEFAULT NULL,  ' age ' int (one) DE FAULT NULL,  PRIMARY KEY (' id ')) engine=innodb DEFAULT Charset=utf8;

  

JdbcTemplate Configuring multiple data sources (completely copied, complete)

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.