springboot Jpa多数据源(不同库)配置
原文链接: https://cloud.tencent.com/developer/article/2147878?areaSource=102001.2&traceId=pjZ5wzrM7kIDFLonYcL2O
一、前言
springboot版本不同对多数据源配置代码有一定影响,部分方法和配置略有不同。 本文采用的springboot版本为2.3.12,数据源为mysql和postgresql
二、配置实战
2.1 基础pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${
mysql.version}</version>
</dependency>
</dependencies>
2.2 配置文件
spring.datasource.mysql.jdbc-url=jdbc:mysql://localhost:3306/heilongjiang?characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.mysql.username=root
spring.datasource.mysql.password=123456
spring.datasource.pg.jdbc-url=jdbc:postgresql://localhost:5432/hljsyjt?useUnicode=true&characterEncoding=utf8¤tSchema=emergencydev,expert,public
spring.datasource.pg.driver-class-name=org.postgresql.Driver
spring.datasource.pg.username=postgres
spring.datasource.pg.password=postgres
spring.jpa.properties.hibernate.mysql-dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.pg-dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
2.3 数据源配置类
package com.gsafety.bg.industrial.config;
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;
import javax.sql.DataSource;
/** * @author Mr.wanter * @time 2021-8-11 0011 * @description */
@Configuration
public class DataSourceConfig {
@Bean("dataSourceMysql")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource dataSourceMysql() {
return DataSourceBuilder.create().build();
}
@Bean("dataSourcePg")
@ConfigurationProperties(prefix = "spring.datasource.pg")
public DataSource dataSourcePg() {
return DataSourceBuilder.create().build();
}
}
2.4 数据源指定配置类
mysql指定数据源:
package com.gsafety.bg.industrial.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/** * @author Mr.wanter * @time 2021-8-11 0011 * @description */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryMysql",//配置连接工厂 entityManagerFactory
transactionManagerRef = "transactionManagerMysql", //配置 事物管理器 transactionManager
basePackages = {
"com.gsafety.bg.industrial.dao"}//设置持久层所在位置
)
public class MysqlDataSourceConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
// 自动注入配置好的数据源
@Autowired
@Qualifier("dataSourceMysql")
private DataSource mysqlDataSource;
// 获取对应的数据库方言
@Value("${spring.jpa.properties.hibernate.mysql-dialect}")
private String mysqlDialect;
/** * @param builder * @return */
@Bean(name = "entityManagerFactoryMysql")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(EntityManagerFactoryBuilder builder) {
Map<String, String> map = new HashMap<>();
// 设置对应的数据库方言
map.put("hibernate.dialect", mysqlDialect);
jpaProperties.setProperties(map);
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(), new HibernateSettings());
return builder
//设置数据源
.dataSource(mysqlDataSource)
//设置数据源属性
.properties(properties)
//设置实体类所在位置.扫描所有带有 @Entity 注解的类
.packages("com.gsafety.bg.industrial.dao.po")
// Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
// Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
.persistenceUnit("mysqlPersistenceUnit")
.build();
}
/** * 配置事物管理器 * * @param builder * @return */
@