Spring Boot 2.0多数据源配置方法实例详解

网友投稿 231 2023-01-22


Spring Boot 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.setDathttp://aSource(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 tranhttp://sactionManagerSecondary() {

return new JpaTransactionManager(entityManagerFactorySecondary().getObject());

}

}

Hibernate相关属性配置

public class HibernatePropertiessifXVEfyBuilder {

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多数据源配置方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈Java中Spring Boot的优势
下一篇:使用IDEA工具配置和运行vue项目及遇到的坑
相关文章

 发表评论

暂时没有评论,来抢沙发吧~