自已闲暇时间写了一个SpringBoot的多数据源的配置,考虑了一点通用性,可以考虑放在公司的相关封装包里,因为单数据源时也不影响。直接上核心代码了,注意修改一下相关包名和Dao实现类
1、默认的数据源配置类
/**
* @author lqb
* @date 2017/9/1 17:15
* 默认的数据源配置
* 数据源配置信息以spring为前缀
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = {"cn.gfire.**.dao"},
repositoryBaseClass = BaseRepositoryImpl.class,
excludeFilters = @ComponentScan.Filter({SecondDatabase.class, ThirdDatabase.class})
)
public class PrimaryDbConfig {
@Resource
private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
AnnotatedTypeScanner scanner = new AnnotatedTypeScanner(Entity.class);
Set<Class<?>> classesSet = scanner.findTypes("cn.gfire.**.domain");
List<Class<?>> classList = new ArrayList<>();
for (Class<?> class1 : classesSet) {
if (!class1.isAnnotationPresent(SecondDatabase.class) && !class1.isAnnotationPresent(ThirdDatabase.class)) {
classList.add(class1);
}
}
Class[] classes = classList.toArray(new Class[]{});
return builder.dataSource(dataSource).packages(classes).persistenceUnit("primary").properties(getVendorProperties(dataSource)).build();
}
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryBean(builder, dataSource()).getObject().createEntityManager();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
第二数据源相关注解与配置
/**
* @author lqb
* @date 2017/9/8 20:113
* 标记为第二数据源,Domain与Dao上添加
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SecondDatabase {
}
/**
* @author lqb
* @date 2017/9/1 17:15
* 第二数据源配置类,数据源配置信息以second为前缀
* 同一包下只能放同一数据源的Domain与Dao
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
repositoryBaseClass = BaseRepositoryImpl.class,
entityManagerFactoryRef = "secondEntityManagerFactory",
basePackages = "cn.gfire.**.dao",
transactionManagerRef = "secondTransactionManager",
includeFilters = @ComponentScan.Filter(SecondDatabase.class)
)
@ConditionalOnProperty(prefix = "second.datasource", name = {"url", "username", "password"})
public class SecondDbConfig {
@Resource
private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "second.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder,
@Qualifier("secondDataSource") DataSource dataSource) {
AnnotatedTypeScanner scanner = new AnnotatedTypeScanner(SecondDatabase.class);
Set<Class<?>> classesSet = scanner.findTypes("cn.gfire.**.domain");
Class[] classes = new Class[classesSet.size()];
int i = 0;
for (Class<?> class1 : classesSet) {
classes[i] = class1;
i++;
}
return builder.dataSource(dataSource).packages(classes).properties(getVendorProperties(dataSource))
.persistenceUnit("secondUnit").build();
}
@Bean(name = "entityManagerSecond")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryBean(builder, dataSource()).getObject().createEntityManager();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("secondEntityManagerFactory") EntityManagerFactory
entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}