赞
踩
一个需求需要在服务里增加一个新的数据源,框架也有点奇葩查了一些资料终于研究好了!供大家借鉴!!!
- spring.datasource.primary.url=jdbc:oracle:thin:@//dev-ceshi:1521/d0prps
- spring.datasource.primary.username=123123
- spring.datasource.primary.password=123456
-
- spring.datasource.secondary.url=jdbc:oracle:thin:@//sit-ceshi:1521/t0prps
- spring.datasource.secondary.username=123456
- spring.datasource.secondary.password=123456
@Configuration:
被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
一句话解释就是用上这注解,系统启动是就会自动注入到容器中
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
-
- import javax.sql.DataSource;
-
- /**
- * @NAME:
- * @DATE: 2021/9/22
- * 双数据源配置
- **/
- @Configuration
- public class DataSourcesConfig {
- @Bean(name = "primaryDataSource")
- @Qualifier("primaryDataSource")
- @Primary//自动装配 就是默认使用此数据源
- @ConfigurationProperties(prefix="spring.datasource.primary")
- public DataSource primaryDataSource() {
- System.out.println("primary db built");
- return DataSourceBuilder.create().build();
- }
-
- @Bean(name = "secondaryDataSource")
- @Qualifier("secondaryDataSource")
- @ConfigurationProperties(prefix="spring.datasource.secondary")
- public DataSource secondaryDataSource() {
- System.out.println("secondary db built");
- return DataSourceBuilder.create().build();
- }
- }
创建secondary数据源单独配置
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- 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.Map;
-
-
- /**
- * @NAME:
- * @DATE: 2021/9/22
- **/
- @Configuration
- @EnableTransactionManagement//事务管理注解
- @EnableJpaRepositories(
- entityManagerFactoryRef = "entityManagerFactorySecondary",
- transactionManagerRef = "transactionManagerSecondary",
- basePackages = {"com.fulan.trade.nbzdata.repository"})
- public class SecondaryConfig
- {
- @Autowired
- private JpaProperties jpaProperties;
-
- @Autowired
- @Qualifier("secondaryDataSource")
- private DataSource secondaryDataSource;
-
- @Bean(name = "entityManagerSecondary")
- public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
- return entityManagerFactorySecondary(builder).getObject().createEntityManager();
- }
-
- @Bean(name = "entityManagerFactorySecondary")
- public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
- return builder
- .dataSource(secondaryDataSource)
- .properties(getVendorProperties(secondaryDataSource))
- .packages("com.fulan.trade.nbzdata.entity") //设置实体类所在位置
- .persistenceUnit("secondaryPersistenceUnit")
- .build();
- }
-
- private Map<String, String> getVendorProperties(DataSource dataSource) {
- return jpaProperties.getHibernateProperties(dataSource);
- }
-
- @Bean(name = "transactionManagerSecondary")
- PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
- return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
- }
- }
创建primary数据源单独配置
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- 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.Map;
-
- /**
- * @NAME: hzan
- * @DATE: 2021/9/22
- **/
- @Configuration
- @EnableTransactionManagement
- @EnableJpaRepositories(
- entityManagerFactoryRef="entityManagerFactoryPrimary",
- transactionManagerRef="transactionManagerPrimary",
- basePackages= { "com.fulan.trade.repository" }) //设置Repository所在位置可以设置多个
- public class PrimaryConfig {
-
- @Autowired
- @Qualifier("primaryDataSource")
- private DataSource primaryDataSource;
-
- @Primary
- @Bean(name = "entityManagerPrimary")
- public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
- return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
- }
-
- @Primary
- @Bean(name = "entityManagerFactoryPrimary")
- public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
- return builder
- .dataSource(primaryDataSource)
- .properties(getVendorProperties(primaryDataSource))
- .packages("com.fulan.trade.entity") //设置实体类所在位置
- .persistenceUnit("primaryPersistenceUnit")
- .build();
- }
-
- @Autowired(required=false)
- private JpaProperties jpaProperties;
-
- private Map<String, String> getVendorProperties(DataSource dataSource) {
- return jpaProperties.getHibernateProperties(dataSource);
- }
-
- @Primary
- @Bean(name = "transactionManagerPrimary")
- public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
- return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
- }
-
- }
按照上面配置的repository位置创建接口就可以实现JPA的查询了
hibernate+EntityManager使用当你使用EntityManager 时
正常引用是 会默认上述自动装载的数据源
@Autowired @PersistenceContext private EntityManager entityManager;可以通过
@PersistenceContext 中的 unitName 属性进行设置@Autowired @PersistenceContext(unitName="entityManagerFactorySecondary") private EntityManager entityManager;这样配置就可以自定数据源了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。