SpringBoot2.0 + SpringDataJpa 配置mysql双数据源

1、配置文件yml,笔者这里将cdr作为主数据源,mdc作为次数据源。

注:如果在配置多数据源时报错 jdbcUrl is required with driverClassName,主要原因是在1.0 配置数据源的过程中主要是写成:url  driverClassName。而在2.0升级之后需要变更成:jdbc-url driver-class-name即可解决!

  datasource:
    cdr:
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://10.188.188.103:10457/cdr?useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: cdr
      password: cdr
      hikari:
        maximum-pool-size: 10
    mdc:
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://10.188.188.103:10457/mdc?useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: mdc
      password: mdc
      hikari:
        maximum-pool-size: 10

2、双数据源总的配置

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.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * mysql双数据源总的配置
 * @author sxp
 */
@Configuration
public class DataSourceConfig {

    //这里的前缀与yml自定义的前缀保持一致,其中只能有一个@Primary用于标识主数据源
    @Bean(name="cdrDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.cdr")
    public DataSource primaryDataSource() {
       return DataSourceBuilder.create().build();
    }

    @Bean(name="mdcDataSource")
    @ConfigurationProperties(prefix="spring.datasource.mdc")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

3、主数据源cdr 的配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * 主数据源cdr mysql 配置
 * @author sxp
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="cdrEntityManagerFactory",
        //repository 所在路径
        basePackages = {"org.scbit.lsbi.cdr.repository.mysql"},
        transactionManagerRef = "cdrTransactionManager")
public class CdrDataSourceConfig {

    @Autowired
    @Qualifier("cdrDataSource")
    private DataSource cdrDataSource;

    /**
     * JdbcTemplate 支持
     * @return
     */
    @Primary
    @Bean(name = "cdrJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate() {
        return new JdbcTemplate(cdrDataSource);
    }

    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return cdrEntityManagerFactory(builder).getObject().createEntityManager();
    }

    private Properties jpaProperties;

    /**
     * 设置实体类所在位置
     * 对@Primary修饰的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,
     * spring上下文自动使用默认的JpaTransactionManager,但是对于第二个或第三个等等必须指定TransactionManager,笔者在实测过程中却发现即使是@Primar修饰的也得指定TransactionManager
     */
    @Primary
    @Bean(name = "cdrEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean cdrEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(cdrDataSource)
                .packages("org.scbit.lsbi.cdr.dataobject.model")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }

    @Primary
    @Bean(name = "cdrTransactionManager")
    public PlatformTransactionManager cdrTransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(cdrEntityManagerFactory(builder).getObject());
    }

}

4、次数据源mdc 的配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * 次数据源mdc mysql 配置
 * @author sxp
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="mdcEntityManagerFactory",
        basePackages = {"org.scbit.lsbi.mdc.mysql.repository"},
        transactionManagerRef = "mdcTransactionManager")
public class MdcDataSourceConfig {

    @Autowired
    @Qualifier("mdcDataSource")
    private DataSource mdcDataSource;

    /**
     * JdbcTemplate 支持
     * @return
     */
    @Bean(name = "mdcJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate() {
        return new JdbcTemplate(mdcDataSource);
    }

    @Primary
    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return mdcEntityManagerFactory(builder).getObject().createEntityManager();
    }

    private Properties jpaProperties;

    /**
     * 设置实体类所在位置
     * 对@Primary修饰的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,
     * spring上下文自动使用默认的JpaTransactionManager,但是对于第二个或第三个等等必须指定TransactionManager
     */
    @Bean(name = "mdcEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean mdcEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(mdcDataSource)
                .packages("org.scbit.lsbi.mdc.mysql.model")
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }

    @Primary
    @Bean(name = "mdcTransactionManager")
    public PlatformTransactionManager mdcTransactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(mdcEntityManagerFactory(builder).getObject());
    }

}

5、至此,双数据源已配置完成,下面是jdbcTemplate、transactionManager的在项目中的具体使用:

@Autowired
@Qualifier("mdcJdbcTemplate")
private JdbcTemplate mdcJdbcTemplate;
/*
* 事务
*/
@Transactional(transactionManager = "cdrTransactionManager")
private void updateDatasetTree(CdrDataset cdrDataset, String newPid){

}

 

你可能感兴趣的:(mysql学习笔记)