1. 配置文件
2. Springboot的配置类?
package com.test.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;
/**
* 多数据源配置
*/
@Configuration
public class DataSourceConfig {
/**
* 创建名为 masterDataSource 的数据源
*/
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 创建名为 slaverDataSource 的数据源
*/
@Bean(name = "slaverDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slaver")
public DataSource slaverDataSource() {
return DataSourceBuilder.create().build();
}
}
? 主数据源配置类
package com.test.config;
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;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 主数据源配置,和从(其他)数据源的区别在于多了个@Primary注解,意思是主要的,多数据源必须设置一个主数据源
* 其他说明请看注释...
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryMaster",//配置连接工厂 entityManagerFactory
transactionManagerRef = "transactionManagerMaster", //配置事物管理器 transactionManager
basePackages = {"com.test.dao.master"} //配置主dao(repository)所在目录
)
public class MasterDataSourceConfig {
@Autowired
@Qualifier("masterDataSource") //指定这是主数据源,为了和从(其他)数据源区别开,因为@Autowired不能导入名称相同的是bean
private DataSource dataSourceMaster;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
/**
* 多了个@Primary
*/
@Primary
@Bean("entityManagerMaster")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return Objects.requireNonNull(entityManagerFactoryBean(builder).getObject()).createEntityManager();
}
/**
* 多了个@Primary
*/
@Primary
@Bean("entityManagerFactoryMaster")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSourceMaster)
.properties(getVendorProperties())
//设置实体类所在目录
.packages("com.test.entity.master")
//持久化单元名称,当存在多个EntityManagerFactory时,需要制定此名称
.persistenceUnit("masterPersistenceUnit")
.build();
}
private Map<String, Object> getVendorProperties() {
Map<String, String> map = new HashMap<>();
map.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.setProperties(map);
return hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(),
new HibernateSettings()
);
}
/**
* 多了个@Primary
*/
@Primary
@Bean("transactionManagerMaster")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(Objects.requireNonNull(entityManagerFactoryBean(builder).getObject()));
}
}
? 从数据源配置类
package com.test.config;
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;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 从数据源配置,和主数据源的配置几乎一样,区别在于没有@Primary注解
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactorySlave",//注意名称和主数据源不一样
transactionManagerRef = "transactionManagerSlave",//注意名称和主数据源不一样
basePackages = {"com.test.dao.slaver"}//配置从dao(repository)所在目录
)
public class SlaverDataSourceConfig {
@Autowired
@Qualifier("slaverDataSource") //注意指定从数据源
private DataSource dataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
@Bean("entityManagerSlave")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return Objects.requireNonNull(localContainerEntityManagerFactoryBean(builder).getObject()).createEntityManager();
}
@Bean("entityManagerFactorySlave")
public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource)
.properties(getVendorProperties())
//设置实体类所在目录
.packages("com.test.entity.slaver")
//持久化单元名称,当存在多个EntityManagerFactory时,需要制定此名称
.persistenceUnit("slavePersistenceUnit")
.build();
}
private Map<String, Object> getVendorProperties() {
Map<String, String> map = new HashMap<>();
map.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
jpaProperties.setProperties(map);
return hibernateProperties.determineHibernateProperties(
jpaProperties.getProperties(),
new HibernateSettings()
);
}
@Bean("transactionManagerSlave")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(Objects.requireNonNull(localContainerEntityManagerFactoryBean(builder).getObject()));
}
}
3. 项目包结构
|