zoukankan      html  css  js  c++  java
  • 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对应实体的位置,我这里还是将它们放在了一个包下,所以上面配置的扫描路劲相同:

  • 相关阅读:
    LTPA Cookie原理
    如何对更改internet密码所需的缓存时间进行调整?
    Freemem eclipse查看内存的小插件
    Java中static、final用法小结
    MANIFEST.MF内容属性名详细解释
    Java程序员的良药:应用程序的开发技巧
    spring struts2 ibatis框架整合开发
    java中静态代码块的用法 static用法详解 类的加载顺序
    从svn上直接导入项目到workspace中
    Eclipse自动生成UML图 Green UML和ModelGoon(直接推拽)
  • 原文地址:https://www.cnblogs.com/shaozhiqi/p/8830773.html
Copyright © 2011-2022 走看看