• springboot 多数据源的实现


    相关的依赖

    yml配置 

     java配置类:

    DataSourceConfigurerjava
    /**
     * Created by zhiqi.shao on 2017/11/20.
     */
    @Configuration
    public class DataSourceConfigurer {
    
        @Bean
        @ConfigurationProperties("datasource.order")
        public DataSourceProperties orderDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean
        @ConfigurationProperties("datasource.order")
        public DataSource orderDataSource() {
            return orderDataSourceProperties().initializeDataSourceBuilder().build();
        }
    
        @Bean
        @Primary
        @ConfigurationProperties("datasource.order-query")
        public DataSourceProperties orderQueryDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean
        @Primary
        @ConfigurationProperties("datasource.order-query")
        public DataSource orderQueryDataSource() {
            return orderQueryDataSourceProperties().initializeDataSourceBuilder().build();
        }
    
    }
    OrderDataSourceConfigurer.java
    /**
     * Created by zhiqi.shao on 2017/11/20.
     */
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef = "entityManagerFactoryOrder",
            transactionManagerRef = "transactionManagerOrder",
            basePackages = {"com.hpe.ordersynchro.ordersEntity"}
    )
    public class OrderDataSourceConfigurer {
    
        @Autowired
        @Qualifier("orderDataSource")
        private DataSource orderDataSource;
    
        @Bean(name = "entityManagerOrder")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder){
              return entityManagerFactoryOrder(builder).getObject().createEntityManager();
        }
    
    
        @Bean(name= "entityManagerFactoryOrder")
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryOrder(EntityManagerFactoryBuilder builder){
            return builder
                    .dataSource(orderDataSource)
                    .properties(getVendorProperties(orderDataSource))
                    .packages(new String[]{"com.hpe.ordersynchro.ordersEntity"})
                    .persistenceUnit("orderPersistenceUnit")
                    .build();
    
        }
    
        @Autowired
        private JpaProperties jpaProperties;
    
        private Map<String,String> getVendorProperties(DataSource dataSource){
            return jpaProperties.getHibernateProperties(dataSource);
        }
    
        @Bean(name ="transactionManagerOrder")
        public PlatformTransactionManager transactionManagerOrder(EntityManagerFactoryBuilder builder){
            return new JpaTransactionManager((entityManagerFactoryOrder(builder).getObject()));
        }
    OrderQueryDataSourceConfigurer.java
    /**
     * Created by zhiqi.shao on 2017/11/20.
     */
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef = "entityManagerFactoryOrderQuery",
            transactionManagerRef = "transactionManagerOrderQuery",
            basePackages = {"com.hpe.ordersynchro.ordersQueryEntity"}
    )
    public class OrderQueryDataSourceConfigurer {
    
        @Autowired
        @Qualifier("orderQueryDataSource")
        private DataSource orderQueryDataSource;
    
        @Primary
        @Bean(name = "entityManagerOrderQuery")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder){
              return entityManagerFactoryOrderQuery(builder).getObject().createEntityManager();
        }
    
        @Primary
        @Bean(name= "entityManagerFactoryOrderQuery")
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryOrderQuery(EntityManagerFactoryBuilder builder){
            return builder
                    .dataSource(orderQueryDataSource)
                    .properties(getVendorProperties(orderQueryDataSource))
                    .packages(new String[]{"com.hpe.ordersynchro.ordersQueryEntity"})
                    .persistenceUnit("orderQueryPersistenceUnit")
                    .build();
    
        }
    
        @Autowired
        private JpaProperties jpaProperties;
    
        private Map<String,String> getVendorProperties(DataSource dataSource){
            return jpaProperties.getHibernateProperties(dataSource);
        }
    
        @Primary
        @Bean(name ="transactionManagerOrderQuery")
        public PlatformTransactionManager transactionManagerOrderQuery(EntityManagerFactoryBuilder builder){
            return new JpaTransactionManager((entityManagerFactoryOrderQuery(builder).getObject()));
        }
    
    
    }

    注解:加了@primary就是默认数据源

    分别是jpa和jpa对应实体的位置,我这里还是将它们放在了一个包下,所以上面配置的扫描路劲相同:

  • 相关阅读:
    git(重点)
    C#技巧记录——持续更新
    结点和节点的区别
    WebSocketSharp send record_stop without send record_start
    cefsharp 拦截所有请求 解决chunked导致数据接收不完整的问题
    计算mp3长度 毫秒
    pydub分割音频文件
    c# 获取文件信息
    实现一边写代码一边展示网页的效果
    c# webapi swagger Area 多级层次分组 添加header参数
  • 原文地址:https://www.cnblogs.com/shaozhiqi/p/8830773.html
Copyright © 2020-2023  润新知