继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

spring boot 2.0 jpa多数据源配置

慕的地10843
关注TA
已关注
手记 1081
粉丝 200
获赞 962

该配置无效,以下是自测可行的配置方式(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


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP