Spring Boot 2.0多數(shù)據(jù)源配置方法實例詳解
兩個數(shù)據(jù)庫實例,一個負(fù)責(zé)讀,一個負(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ù)源配置方法實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解Spring Boot整合Mybatis實現(xiàn) Druid多數(shù)據(jù)源配置
- 深入理解spring多數(shù)據(jù)源配置
- spring+Jpa多數(shù)據(jù)源配置的方法示例
- 詳解基于Spring Boot與Spring Data JPA的多數(shù)據(jù)源配置
- Spring+MyBatis多數(shù)據(jù)源配置實現(xiàn)示例
- springboot-mongodb的多數(shù)據(jù)源配置的方法步驟
- springboot v2.0.3版本多數(shù)據(jù)源配置方法
- Spring動態(tài)多數(shù)據(jù)源配置實例Demo
- Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
- spring基于通用Dao的多數(shù)據(jù)源配置詳解
- Servlet+MyBatis項目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- spring多數(shù)據(jù)源配置實現(xiàn)方法實例分析
相關(guān)文章
@MapperScan掃描包里混有@Service等問題如何解決
這篇文章主要介紹了@MapperScan掃描包里混有@Service等問題如何解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java輸出1~100之間的全部素數(shù)的5種方式總結(jié)
這篇文章主要介紹了java輸出1~100之間的全部素數(shù)的5種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Java?EasyExcel利用填充模版動態(tài)生成多個sheet頁
這篇文章主要為大家詳細(xì)介紹了Java?EasyExcel如何利用填充模版動態(tài)生成多個sheet頁,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Java中將String類型依照某個字符分割成數(shù)組的方法
下面小編就為大家分享一篇Java中將String類型依照某個字符分割成數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Java實戰(zhàn)之用hutool-db實現(xiàn)多數(shù)據(jù)源配置
在微服務(wù)搭建中經(jīng)常會使用到多數(shù)據(jù)庫情形這個時候,下面這篇文章主要給大家介紹了關(guān)于Java實戰(zhàn)之用hutool-db實現(xiàn)多數(shù)據(jù)源配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12

