博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 多数据源的实现
阅读量:5290 次
发布时间:2019-06-14

本文共 4219 字,大约阅读时间需要 14 分钟。

相关的依赖

yml配置 

 java配置类:

DataSourceConfigurerjava
/** * Created by zhiqi.shao on 2017/11/20. */@Configurationpublic 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
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
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对应实体的位置,我这里还是将它们放在了一个包下,所以上面配置的扫描路劲相同:

 

转载于:https://www.cnblogs.com/luotuoke/p/8830773.html

你可能感兴趣的文章
将多张图片和文字合成一张图片
查看>>
自己动手写ORM(01):解析表达式树生成Sql碎片
查看>>
如何使用USBWebserver在本机快速建立网站测试环境
查看>>
百度Ueditor编辑器的Html模式自动替换样式的解决方法
查看>>
变量提升
查看>>
线性表可用顺序表或链表存储的优缺点
查看>>
在现有的mysql主从基础上,搭建mycat实现数据的读写分离
查看>>
opencv安装配置
查看>>
JAVA-初步认识-第六章-面向对象(举例)
查看>>
js合并数组
查看>>
cNoteSetCursor_命令窗口光标设置
查看>>
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>
Benelux Algorithm Programming Contest 2014 Final(第二场)
查看>>
随机变量的期望为什么把不是自己密度函数当成自己的权重来求期望呢?
查看>>
6-1 并行程序模拟 uva210
查看>>
JavaScript动画打开半透明提示层
查看>>
Mybatis生成resulteMap时的注意事项
查看>>
jquery-jqzoom 插件 用例
查看>>
javascript知识点记录01
查看>>