SPRINGBOOT 多数据源配置(mysql+oracle)
最近有个小工具需要用到mysql数据库和oracle数据库的,网上找了半天资料,可能是版本原因,各种报错。结合了几篇博客才搞定,记录一下过程。
配置文件
pom.xml文件配置
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 我这mysql的版本是5.0+的所有需要表明版本号 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 加上这个包解决字符集的问题 -->
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
application.properties文件配置
配置数据源
在配置mysql的驱动时需要注意我这使用的是mysql5+的版本需要使用com.mysql.jdbc.Driver,如果是mysql6+版本使用com.mysql.cj.jdbc.Driver且记得配置时区&serverTimezone=Asia/Shanghai。
我这使用的是hibernate查询,记得配置spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl,否则你在entity类的@table标签设置的大写是不会生效的。
#MySQL
mysql.spring.datasource.jdbcUrl=jdbc:mysql://172.18.1.210:3306/test?autoReconnect=true&characterEncoding=utf8&useSSL=false
mysql.spring.datasource.username=lcl
mysql.spring.datasource.password=lcl6230
mysql.spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#oracle
oracle.spring.datasource.jdbcUrl=jdbc:oracle:thin:@//172.18.1.210:1523/cdsdh
oracle.spring.datasource.username=bizservice
oracle.spring.datasource.password=BIZ_ccsf_cd0831
oracle.spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
#multiple Setting
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.show-sql=true
项目结构
dao类和entity类需要创建对应的package,便于指定某路径下的类访问对应的数据库。
config包中的配置类
DataSourceConfig
package com.example.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
//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.context.annotation.Primary;
@Configuration
public class DataSourceConfig {
@Bean(name = "mysqlDS")
@Qualifier("mysqlDS")
@Primary
@ConfigurationProperties(prefix="mysql.spring.datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "oracleDS")
@Qualifier("oracleDS")
@ConfigurationProperties(prefix="oracle.spring.datasource")
public DataSource secondaryDataSource(){
return DataSourceBuilder.create().build();
}
}
MySql数据库 配置类
package com.example.config;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
*
* @author
*
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMySql", transactionManagerRef = "transactionManagerMySql", basePackages = {
"com.example.dao.mysql" }) // 设置dao(repo)所在位置
public class RepositoryMySqlConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
@Autowired
@Qualifier("mysqlDS")
private DataSource mysqlDS;
@Bean(name = "entityManagerMySql")
@Primary
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryMySql(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryMySql")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryMySql(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(mysqlDS)// 设置数据源
.properties(jpaProperties.getProperties())// 设置jpa配置
.properties(getVendorProperties())// 设置hibernate配置
.packages("com.example.entity.mysql") //设置实体类所在位置
.persistenceUnit("MySqlPersistenceUnit")// 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
.build();
}
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Bean(name = "transactionManagerMySql")
@Primary
PlatformTransactionManager transactionManagerMySql(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryMySql(builder).getObject());
}
}
oracle 数据库 配置类
package com.example.config;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryOracle",
transactionManagerRef="transactionManagerOracle",
basePackages= { "com.example.dao.oracle" })
public class RepositoryOracleConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired @Qualifier("oracleDS")
private DataSource oracleDS;
@Autowired
private HibernateProperties hibernateProperties;
@Bean(name = "entityManagerOracle")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryOracle(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryOracle")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryOracle (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(oracleDS)
.properties(getVendorProperties())
.packages("com.example.entity.oracle")
.persistenceUnit("OraclePersistenceUnit")
.build();
}
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Bean(name = "transactionManagerOracle")
PlatformTransactionManager transactionManagerOracle(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryOracle(builder).getObject());
}
}
项目启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = {"com.example.*"})
public class ChoreographerApplication {
public static void main(String[] args) {
SpringApplication.run(ChoreographerApplication.class, args);
}
}
注意这个注解@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 没有这个注解的时候,项目可能会去找默认的数据源,但是现在未配置,启动会报错