Spring Boot 2.0多數(shù)據(jù)源配置方法實(shí)例詳解
兩個(gè)數(shù)據(jù)庫實(shí)例,一個(gè)負(fù)責(zé)讀,一個(gè)負(fù)責(zé)寫。
datasource-reader: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://192.168.43.61:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false username: icbc password: icbc driver-class-name: com.mysql.jdbc.Driver continue-on-error: false sql-script-encoding: UTF-8 datasource-writer: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://192.168.43.61:3306/hdfs?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false username: icbc password: icbc driver-class-name: com.mysql.jdbc.Driver continue-on-error: false sql-script-encoding: UTF-8
讀數(shù)據(jù)庫配置
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = { "cn.cib.repository.read"}) public class RepositoryPrimaryConfig { @Autowired @Qualifier("r_ds") private DataSource r_ds; @Bean(destroyMethod = "", name = "entityManagerPrimary") @Primary public EntityManager entityManager() { return entityManagerFactoryPrimary().getObject().createEntityManager(); } @Bean(destroyMethod = "", name = "entityManagerFactoryPrimary") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(r_ds); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); factoryBean.setJpaProperties(HibernatePropertiesBuilder.hibernateProperties()); factoryBean.setPackagesToScan("cn.cib.repository.read", "cn.cib.entity.read"); factoryBean.setPersistenceUnitName("read"); return factoryBean; } @Bean(destroyMethod = "", name = "transactionManagerPrimary") @Primary PlatformTransactionManager transactionManagerPrimary() { return new JpaTransactionManager(entityManagerFactoryPrimary().getObject()); } }
寫數(shù)據(jù)庫配置
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = { "cn.cib.repository.write"}) public class RepositorySecondaryConfig { @Autowired @Qualifier("w_ds") private DataSource w_ds; @Bean(destroyMethod = "", name = "entityManagerSecondary") public EntityManager entityManager() { return entityManagerFactorySecondary().getObject().createEntityManager(); } @Bean(destroyMethod = "", name = "entityManagerFactorySecondary") public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(w_ds); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); factoryBean.setJpaProperties(HibernatePropertiesBuilder.hibernateProperties()); factoryBean.setPackagesToScan("cn.cib.repository.write","cn.cib.entity.write"); factoryBean.setPersistenceUnitName("write"); return factoryBean; } @Bean(destroyMethod = "", name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary() { return new JpaTransactionManager(entityManagerFactorySecondary().getObject()); } }
Hibernate相關(guān)屬性配置
public class HibernatePropertiesBuilder { public static Properties hibernateProperties() { final Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update"); hibernateProperties.setProperty("hibernate.show_sql", "true"); hibernateProperties.setProperty("hibernate.format_sql", "true"); return hibernateProperties; } }
總結(jié)
以上所述是小編給大家介紹的Spring Boot 2.0多數(shù)據(jù)源配置方法實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置
- 深入理解spring多數(shù)據(jù)源配置
- spring+Jpa多數(shù)據(jù)源配置的方法示例
- 詳解基于Spring Boot與Spring Data JPA的多數(shù)據(jù)源配置
- Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例
- springboot-mongodb的多數(shù)據(jù)源配置的方法步驟
- springboot v2.0.3版本多數(shù)據(jù)源配置方法
- Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo
- Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
- spring基于通用Dao的多數(shù)據(jù)源配置詳解
- Servlet+MyBatis項(xiàng)目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- spring多數(shù)據(jù)源配置實(shí)現(xiàn)方法實(shí)例分析
相關(guān)文章
Springboot集成Jasypt實(shí)現(xiàn)配置文件加密的方法
Jasypt是一個(gè)java庫,它允許開發(fā)員以最少的努力為他/她的項(xiàng)目添加基本的加密功能,并且不需要對加密工作原理有深入的了解,這篇文章主要介紹了Springboot集成Jasypt實(shí)現(xiàn)配置文件加密,需要的朋友可以參考下2023-04-04java對象轉(zhuǎn)化成String類型的四種方法小結(jié)
在java項(xiàng)目的實(shí)際開發(fā)和應(yīng)用中,常常需要用到將對象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下2021-08-08spring boot結(jié)合Redis實(shí)現(xiàn)工具類的方法示例
這篇文章主要介紹了spring boot結(jié)合Redis實(shí)現(xiàn)工具類的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11使用WebUploader實(shí)現(xiàn)上傳文件功能(一)
這篇文章主要為大家詳細(xì)介紹了使用WebUploader實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解
項(xiàng)目里使用的是EasyPoi來處理導(dǎo)入導(dǎo)出功能的。近日因業(yè)務(wù)需求調(diào)整,一些導(dǎo)出功能的導(dǎo)出列需要根據(jù)不同的條件動(dòng)態(tài)導(dǎo)出2022-09-09淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別
本文主要介紹了淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11