JdbcTemplate 配置多数据源(完全照抄,完整)
Posted 无敌战魔i
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JdbcTemplate 配置多数据源(完全照抄,完整)相关的知识,希望对你有一定的参考价值。
<?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 配置多数据源(完全照抄,完整)的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot之JdbcTemplate多数据源配置与使用
Spring Boot之JdbcTemplate多数据源配置与使用