SpringBoot JPA 多数据源的使用
来源:互联网 发布:mac os 打开文件夹 编辑:程序博客网 时间:2024/05/22 05:19
SpringBoot JPA 多数据源的使用
参考: http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba/topic/59f6a809a5beea6a3fd8a7f2
多数据源的支持
在项目开发中,常常需要在一个项目中使用多个数据源,因此需要配置Spring data jpa对多数据源的使用.
一般分为以下三步:
- 配置多数据源
- 不同源的repsitory放入不同的包路径
- 声明不同包路径下使用不同的数据源,事物支持
第一步:配置两个数据源的连接属性
其中 application.properties 配置如下:
################ dataSource START ##################primaryspring.primary.datasource.url=jdbc:mysql://localhost:3306/test1spring.primary.datasource.username=rootspring.primary.datasource.password=rootspring.primary.datasource.driver-class-name=com.mysql.jdbc.Driver#secondaryspring.secondary.datasource.url=jdbc:mysql://localhost:3306/test2spring.secondary.datasource.username=rootspring.secondary.datasource.password=rootspring.secondary.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialectspring.jpa.show-sql= true############# dataSource END #############
第二步: 读取两个数据源,:
/** * @author itguang * @create 2017-12-07 16:22 **/@Configurationpublic class DataSourceConfig { @Bean(name = "primaryDataSource") @Primary @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.primary.datasource") public DataSource primaryDatasource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @ConfigurationProperties(prefix = "spring.secondary.datasource") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); }}
第三步: 将数据源注入到 Factory,配置 repository、domian 的位置,构建两个 实体管理器 EntityManager
/** * 将数据源注入到 实体管理器工厂,配置 repository、domian 的位置 * * @author itguang * @create 2017-12-07 16:35 * **/@Configuration@EnableTransactionManagement@EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory transactionManagerRef = "transactionManagerPrimary", //配置 事物管理器 transactionManager basePackages = {"com.itguang.springbootmultidatasource.repository.test1"}//设置dao(repo)所在位置)public class PrimaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource; /** * * @param builder * @return */ @Bean(name = "entityManagerFactoryPrimary") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder //设置数据源 .dataSource(primaryDataSource) //设置数据源属性 .properties(getVendorProperties(primaryDataSource)) //设置实体类所在位置.扫描所有带有 @Entity 注解的类 .packages("com.itguang.springbootmultidatasource.domain") // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后, // Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作 .persistenceUnit("primaryPersistenceUnit") .build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } /** * 配置事物管理器 * * @param builder * @return */ @Bean(name = "transactionManagerPrimary") @Primary PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); }}
第四步:测试
@RunWith(SpringRunner.class)@SpringBootTestpublic class SpringbootMultiDatasourceApplicationTests { @Resource private UserTest1Repository userTest1Repository; @Resource private UserTest2Repository userTest2Repository; @Test public void testSave() throws Exception { Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String formattedDate = dateFormat.format(date); userTest1Repository.save(new User("aa", "aa123456","aa@126.com", "aa", formattedDate)); userTest2Repository.save(new User("cc", "cc123456","cc@126.com", "cc", formattedDate)); } @Test public void contextLoads() { }}
实体类和respoitory实现请参考源代码.
https://github.com/itguang/gitbook-smile/tree/master/springboot-multi-datasource
阅读全文
0 0
- SpringBoot JPA 多数据源的使用
- Springboot+jpa多数据源
- springBoot jpa 多数据源的动态切换
- springboot-druid-mybatis多数据源使用
- springboot 多数据源
- springboot多数据源
- springboot的多数据源配置
- SpringBoot的多数据源配置
- springBoot+mybatis多数据源的配置
- SpringBoot多数据源的配置(SpringBoot+MyBatis)
- JPA多数据源配置
- JPA 配置多数据源
- spring-boot 使用 spring-data-jpa多数据源配置
- springboot 配置多数据源
- springboot 配置多数据源
- SpringBoot-SpringData-多数据源
- springboot 多数据源配置
- springboot + mybatis + 多数据源
- 公众号分享到qq空间,公众号jssdk开发
- 基于Docker持续交付平台建设的实践
- Andrew Ng 卷积神经网络笔记(第一周)
- java基础-泛型 枚举
- LOJ6014「网络流 24 题
- SpringBoot JPA 多数据源的使用
- 读懂Word2Vec之Skip-Gram
- 前端项目积累和总结——AJAX异步更新网页技术
- tomacat 启动时报错 : BeanFactory not initialized or already closed
- 汇编实现字符串的连接
- Google 将于明年彻底关闭 Chrome Apps,以后就是 PWA 的天下了
- struts2访问servletAPI的几种方式
- 【JSON】JSON 语法
- nginx安装