- 多数据库配置
配置我们目录结构设置:
config
---datasource
----jpa
----mybatis
----redis
Datasource中是数据的相关配置
Jap中是springDatajpa的相关配置
Mybatis中是mybatis的相关配置
Redis中是redis的相关配置
如果还有其他的可以自行创建目录
3.1.DataSource的配置
/**
* @author youkun
* @data 2018/7/22
* 多数据库连接
*/
@Configuration
public class DataSourceConfig {
/**
* 数据库1
* @return
*/
@RefreshScope
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
}
/**
* 数据库2
* @return
*/
@RefreshScope
@Bean(name="secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource(){
return DataSourceBuilder.create().build();
}
}
需要注意的是我们需要表明默认使用哪个需要加上@Primary注解
@ConfigurationProperties中设置的是我们需要读取哪个库的配置可以参考后面的配置
3.2.Jap的相关配置
每一个库对应一个jpa的配置类,默认的需要加上@Primary注解
如:库一
/**
* @author youkun
* @data 2018/7/22
* jpa__多数据库自动生成表设置
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryPrimary",
transactionManagerRef = "transactionManagerPrimary",
basePackages = {"com.tzbank.common.entity"})
public class JpaPrimaryConfig { @Autowired
@Qualifier("primaryDataSource") private DataSource primaryDataSource; /**
* jpa__设置
* @param builder * @return */ @Primary
@Bean(name="entityManagerPrimary") @Qualifier("entityManagerPrimary") public EntityManager entityManager(EntityManagerFactoryBuilder builder){ return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
} @Primary
@Bean(name = "entityManagerFactoryPrimary") @Qualifier("entityManagerFactoryPrimary") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder){ return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
.packages("com.tzbank.common.entity.primary")
.persistenceUnit("primaryPersistenceUnit")
.build();
} @Autowired(required = false) private JpaProperties jpaProperties; private Map getVendorProperties(DataSource dataSource){ return jpaProperties.getHibernateProperties(dataSource);
} @Primary
@Bean(name = "transactionManagerPrimary") @Qualifier("transactionManagerPrimary")
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder){ return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
库二:
/**
* @author youkun
* @data 2018/7/22
* jpa_多数据库自动生成表设置_
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManagerSecondary",
basePackages = {"com.tzbank.common.entity.secondary"})
public class JpaSecondaryConfig {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
/**
* jpa_设置_
* @param builder
* @return
*/
@Bean(name="entityManager")
@Qualifier("entityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder){
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactory")
@Qualifier("entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder){
return builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties(secondaryDataSource))
.packages("com.tzbank.common.entity.secondary")
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map getVendorProperties(DataSource dataSource){
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerSecondary")
@Qualifier("transactionManagerSecondary")
PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder){
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
需要注意的:如果在启动保持entityManagerFactory****相关的错误我们只需把entityManagerFactoryRef 改成的 entityManagerFactory 同样 LocalContainerEntityManagerFactoryBean 中的这个的方法的bean的名称也需要改成一样的,
EnableJpaRepositories注解中basePackages 设置的是jpa中Respository所在的位置,因为我们项目中没有用到属于就设置的是实体所在的位置
LocalContainerEntityManagerFactoryBean 中的packages是实体所在的包
- mybatis设置
每一个库对应一个mybatis的配置类,默认的需要加上@Primary注解
如:库一
/**
* @author youkun
* @data 2018/7/22
* mybatis_多数据设置_
*/
@Configuration
@MapperScan(value = "com.tzbank.common.dao.primary",sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class MybatisPrimaryConfig {
@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Bean
@Primary
@RefreshScope
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(primaryDataSource);
_//_配置xml目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath*:mapperXML/primary/*.xml"));
return bean.getObject();
}
@Bean
@Primary
@RefreshScope
public SqlSessionTemplate sqlSessionTemplatePrimary(@Qualifier("sqlSessionFactoryPrimary") SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
库二:
/**
* @author youkun
* @data 2018/7/22
* mybatis_多数据设置_
*/
@Configuration
@MapperScan(value = "com.tzbank.common.dao.secondary",sqlSessionTemplateRef = "sqlSessionTemplateSecondary")
public class MybatisSecondaryConfig {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;
@Bean
@RefreshScope
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(secondaryDataSource);
_//_配置xml目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath*:mapperXML/secondary/*.xml"));
return bean.getObject();
}
@Bean
@RefreshScope
public SqlSessionTemplate sqlSessionTemplateSecondary(@Qualifier("sqlSessionFactorySecondary") SqlSessionFactory sqlSessionFactory)throws Exception{
return new SqlSessionTemplate(sqlSessionFactory);
}
}
需要注意的配置
@MapperScan注解中的value是dao所在的位置
SqlSessionFactory中的getResources是xml所在的位置
5.redis的配置(集群)
/**
* redis 集群的配置
*/
@Configuration
public class RedisClusterConfig {
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
@RefreshScope
public class ClusterConfigurationProperties{
List
}
@Bean
@RefreshScope
@Primary
public RedisConnectionFactory connectionFactory(ClusterConfigurationProperties clusterConfigurationProperties){
return new JedisConnectionFactory(new RedisClusterConfiguration(clusterConfigurationProperties.getNodes()));
}
}