JdbcTemplate 配置多數據源(完全照抄,完整)
阿新 • • 發佈:2017-11-04
framework -i into repos .so not scrip version org
<?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; @Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") // @Qualifier("primaryDataSource")//原博主這裏加上@Qualify 沒有必要 刪了也沒有影響 @ConfigurationProperties("spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "secondaryDataSource") // @Qualifier("secondaryDataSource") //原博主這裏加上@Qualify 沒有必要 刪了也沒有影響 @ConfigurationProperties("spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } /** * @param dataSource * @return * @Bean明確地指示了一種方法,什麽方法呢——產生一個bean的方法, 並且交給Spring容器管理;從這我們就明白了為啥@Bean是放在方法的註釋上了, * 因為它很明確地告訴被註釋的方法,你給我產生一個Bean,然後交給Spring容器, * 實現依賴註入 */ @Bean(name = "primaryJdbcTemplate") public JdbcTemplate primaryTemplate(@Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
測試 通過測試主鍵判斷數據源是否配置成功
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) @SpringBootTest public class TwoDatasourceApplicationTests { @Autowired /*@Qualify 多個同類型bean?存在時 Spring不知道應該綁定哪個實現類 指定綁定的類名@Bean(name="**") */ @Qualifier("primaryJdbcTemplate") protected JdbcTemplate jdbcTemplate1; @Autowired //@Qualify 多個同類型bean?存在時 指定綁定的類名@Bean(name="**") @Qualifier("secondaryJdbcTemplate") protected JdbcTemplate jdbcTemplate2; @Before //首先執行清空數據庫操作 public void setUp(){ jdbcTemplate1.update("DELETE FROM USER "); jdbcTemplate2.update("DELETE FROM USER "); } @Test public void test(){ jdbcTemplate1.update ("INSERT INTO USER (id,name,age) VALUES (?,?,?)",1,"aaa",20); jdbcTemplate2.update ("INSERT INTO USER (id,name,age) VALUES (?,?,?)",1,"bbb",30); 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)); } }
數據庫
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
JdbcTemplate 配置多數據源(完全照抄,完整)