时间:2021-05-19
两个数据库实例,一个负责读,一个负责写。
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-8datasource-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; }}总结
以上所述是小编给大家介绍的Spring Boot 2.0多数据源配置方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了spring多数据源配置实现方法。分享给大家供大家参考,具体如下:在网上找到的配置多数据源的方法。1.扩展org.springframework.
SpringBoot如何快速配置数据源;有如下两种方式:通过spring-boot-starter-jdbc快速配置数据源自定义数据源DataSource首先我
这篇文章主要介绍了Spring-boot集成pg、mongo多数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
由于项目需要,最近研究了一下基于springBoot与SpringDataJPA的多数据源配置问题。以下是传统的单数据源配置代码。这里使用的是Spring的An
此方案适用于解决springboot项目运行时动态添加数据源,非静态切换多数据源!!!一、多数据源应用场景:1.配置文件配置多数据源,如默认数据源:master