相关的依赖
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对应实体的位置,我这里还是将它们放在了一个包下,所以上面配置的扫描路劲相同: