温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot 2.0多数据源配置的示例分析

发布时间:2021-07-15 11:26:46 来源:亿速云 阅读:104 作者:小新 栏目:编程语言

这篇文章主要介绍了SpringBoot 2.0多数据源配置的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

两个数据库实例,一个负责读,一个负责写。

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

读数据库配置

@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());
  }
}

写数据库配置

@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相关属性配置

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;
  }
}

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot 2.0多数据源配置的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI