spring boot 2.0 jpa多数据源配置
标签:
SpringBoot
该配置无效,以下是自测可行的配置方式(spring boot版本2.0.6.RELEASE)。
1. application.properties配置
#数据源1spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver#数据源2spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driverClassName = com.mysql.jdbc.Driver
2.第一数据源配置
import javax.sql.DataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import com.zaxxer.hikari.HikariDataSource;@Configuration@EnableJpaRepositories(entityManagerFactoryRef = "primaryEntityManagerFactory", transactionManagerRef = "primaryTransactionManager", // 此处指定第一数据源对于dao包路径 basePackages = "com.onecodespace.codegenerator.business.dao")public class PrimaryConfig { @Bean PlatformTransactionManager primaryTransactionManager() { return new JpaTransactionManager(primaryEntityManagerFactory().getObject()); } @Bean LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(primaryDataSource()); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); // 此处指定第一数据源对应实体类包路径 factoryBean.setPackagesToScan("com.onecoderspace.codegenerator.business.domain"); return factoryBean; }// @Bean// @ConfigurationProperties(prefix = "spring.datasource.primary")// DataSource primaryDataSource() {// return new EmbeddedDatabaseBuilder().// setType(EmbeddedDatabaseType.H2).// build();// } @Bean @ConfigurationProperties(prefix="spring.datasource.primary") public DataSource primaryDataSource() { //通过DataSourceBuilder构建数据源 return DataSourceBuilder.create().type(HikariDataSource.class).build(); } }
3. 第二数据源配置
import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;import org.springframework.transaction.PlatformTransactionManager;import com.zaxxer.hikari.HikariDataSource;@Configuration@EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory", transactionManagerRef = "secondaryTransactionManager", // 第二数据源对应dao包路径 basePackages = "com.onecodespace.codegenerator.schema.dao")public class SecondaryConfig extends DataSourceAutoConfiguration { @Bean PlatformTransactionManager secondaryTransactionManager() { return new JpaTransactionManager(secondaryEntityManagerFactory().getObject()); } @Bean LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(secondaryDataSource()); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); // 第二数据源对应实体的包路径 factoryBean.setPackagesToScan("com.onecodespace.codegenerator.schema.domain"); return factoryBean; } @Bean @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { //通过DataSourceBuilder构建数据源 return DataSourceBuilder.create().type(HikariDataSource.class).build(); } // 如果使用jdbcTemplate,进行如下设置即可 @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate( @Qualifier("secondaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); }
作者:思与学
链接:https://www.jianshu.com/p/803c6c5a40a7
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦