赞
踩
第一次配置多数据源,理所当然的去网上查了。然后就看见一个感觉挺靠谱的,就这个。
https://segmentfault.com/a/1190000012148813
结果不能用,因为他这个只是实现了,在项目中肯定要封装的,自己封装工作量太大了。果断弃用
不过那里面的思路是正确的,jar包也是正确的
<?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.adagio</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>multiple-data-sources</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-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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>
#数据源配置
spring.datasource.primary.jdbc-url=jdbc:oracle:thin:@###,###,###,###:####:accttest
spring.datasource.primary.username=****
spring.datasource.primary.password=*****
spring.datasource.primary.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.secondary.jdbc-url=jdbc:mysql:// ###,###,###,###:####/acct?useUnicode=true&characterEncoding=utf8
spring.datasource.secondary.username=*****
spring.datasource.secondary.password=*****
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration //mapper接口文件位置 @MapperScan(basePackages = "com.asiainfo.comm.commIntface.*.mapper", sqlSessionTemplateRef = "secondaryJdbcTemplate") public class MysqlDataSourceConfig { @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryJdbcFactory") public SqlSessionFactory secondaryJdbcFactory(@Qualifier(value = "secondaryDataSource") DataSource dataSource ) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml") ); return bean.getObject(); } @Bean(name = "secondaryJdbcManager") public DataSourceTransactionManager secondaryJdbcManager(@Qualifier(value = "secondaryDataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name = "secondaryJdbcTemplate") public SqlSessionTemplate secondaryJdbcTemplate(@Qualifier(value = "secondaryJdbcFactory") SqlSessionFactory sqlSessionFactory) throws Exception{ return new SqlSessionTemplate(sqlSessionFactory); } }
上面这个是mysql的,oracle同上
多的话可以一直配置
这里可能会有个问题,如果你的SpringBoot和我版本一样的话,应该没事,如果是2.0.0以下的话
import org.springframework.boot.jdbc.DataSourceBuilder;
这个类可能会找不到,2.0以下路径应该是
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。