這篇文章主要介紹了SpringBoot 2.0多數(shù)據(jù)源配置的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)執(zhí)著的堅(jiān)持網(wǎng)站建設(shè),微信小程序;我們不會(huì)轉(zhuǎn)行,已經(jīng)持續(xù)穩(wěn)定運(yùn)營(yíng)10多年。專(zhuān)業(yè)的技術(shù),豐富的成功經(jīng)驗(yàn)和創(chuàng)作思維,提供一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
兩個(gè)數(shù)據(jù)庫(kù)實(shí)例,一個(gè)負(fù)責(zé)讀,一個(gè)負(fù)責(zé)寫(xiě)。
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ù)庫(kù)配置
@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()); } }
寫(xiě)數(shù)據(jù)庫(kù)配置
@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; } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot 2.0多數(shù)據(jù)源配置的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)站欄目:SpringBoot2.0多數(shù)據(jù)源配置的示例分析
文章分享:http://jinyejixie.com/article36/posisg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、服務(wù)器托管、App設(shè)計(jì)、Google、App開(kāi)發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)