• 微服务 第八章 SpringBoot多数据源的配置(通过Spring Data JPA 的方式)


    码云地址:https://gitee.com/yaohuiqin/SpringBootDemo/commits/master

    1、在application.properties 文件中配置连接参数:

    spring.datasource.primary.url=jdbc:oracle:thin:@localhost:1521:orcl
    spring.datasource.primary.username=microservice
    spring.datasource.primary.password=microservice
    spring.datasource.primary.driver-class-name=oracle.jdbc.driver.OracleDriver
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    
    spring.datasource.secondary.url=jdbc:oracle:thin:@localhost:1521:orcl
    spring.datasource.secondary.username=microservice2
    spring.datasource.secondary.password=microservice2
    spring.datasource.secondary.driver-class-name=oracle.jdbc.driver.OracleDriver
    

    2、多数据源配置(和通过jdbcTemplate方式配置一样):

    @Configuration
    public class DataSourceConfig {
        //配置第一个数据源
        @Bean(name = "primaryDataSource")
        @Qualifier("primaryDataSource")
        @ConfigurationProperties(prefix="spring.datasource.primary")
        public DataSource primaryDataSource() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "primaryJdbcTemplate")
        public JdbcTemplate primaryJdbcTemplate(
                @Qualifier("primaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
        //配置第二个数据源
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        @Primary
        @ConfigurationProperties(prefix="spring.datasource.secondary")
        public DataSource secondaryDataSource() {
            return DataSourceBuilder.create().build();
        }
        @Bean(name = "secondaryJdbcTemplate")
        public JdbcTemplate secondaryJdbcTemplate(
                @Qualifier("secondaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
    }
    

     3、配置每个数据源的一些参数,比如与表对应的实体类所在的包。一个配置对应一个java类。

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef="entityManagerFactoryPrimary",
            transactionManagerRef="transactionManagerPrimary",
            basePackages= { "per.yhq.domain.primary" }) //设置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("per.yhq.domain.primary") //设置实体类所在位置
                    .persistenceUnit("primaryPersistenceUnit")
                    .build();
        }
    
        @Autowired
        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());
        }
    
    }
    

      

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef="entityManagerFactorySecondary",
            transactionManagerRef="transactionManagerSecondary",
            basePackages= { "per.yhq.domain.secondary" }) //设置Repository所在位置
    public class SecondaryConfig {
    
        @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("per.yhq.domain.secondary") //设置实体类所在位置
                    .persistenceUnit("secondaryPersistenceUnit")
                    .build();
        }
    
        @Autowired
        private JpaProperties jpaProperties;
    
        private Map<String, String> getVendorProperties(DataSource dataSource) {
            return jpaProperties.getHibernateProperties(dataSource);
        }
    
        @Bean(name = "transactionManagerSecondary")
        PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
            return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
        }
    
    }
    

      4、在配置的包下新增实体类和对应的Repository接口。

  • 相关阅读:
    Python基础之内存管理与垃圾回收机制
    Git常用命令
    Git分支操作
    码云配置SSH公钥
    Git基本操作
    Git基本理论
    版本控制
    Python基础之Python语法
    成为一名JAVA高级工程师你需要学什么【转】
    一个java高级工程师的进阶之路【转】
  • 原文地址:https://www.cnblogs.com/yaohuiqin/p/9399942.html
Copyright © 2020-2023  润新知