溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot 2.0多數(shù)據(jù)源配置的示例分析

發(fā)布時(shí)間:2021-07-15 11:26:46 來(lái)源:億速云 閱讀:97 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹了SpringBoot 2.0多數(shù)據(jù)源配置的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

兩個(gè)數(shù)據(jù)庫(kù)實(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ù)庫(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());
  }
}

寫數(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í)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI