SpringBoot配置JPA多数据源

SpringBoot版本

不同的版本其配置方式可能有所区别,我采用的是SpringBoot2.0.4.RELEASE。

多数据源配置

application.yml配置如下:

spring:
  application:
    name: test
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  datasource:
    primary:
      jdbc-url: jdbc:mysql://127.0.0.1:3306/laboratory
      driver-class-name: com.mysql.jdbc.Driver
      username: root
      password: root
    secondary:
      jdbc-url: jdbc:mysql://127.0.0.1:3306/test
      driver-class-name: com.mysql.jdbc.Driver
      username: root
      password: root

注意:使用jdbc-url属性代替之前配置中的url属性

创建一个Spring配置类,定义两个DataSource来读取application.yml中的不同配置,在上述yaml中,spring.datasource.primary为主数据源配置,spring.datasource.secondary为第二数据源配置,代码如下:

@Configuration
public class DataSourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

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

Spring-data-jpa支持

增加对第一数据源的JPA配置

注意其中指定数据源对应的Entity实体和Repository定义的位置,用@Primary来区分主数据源,代码如下:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"cn.ntshare.laboratory.domain.master"}
)
public class PrimarySourceConfig {

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

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

    @Resource
    private Properties jpaProperties;

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(primaryDataSource)
                .packages("cn.ntshare.laboratory.domain.master") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }

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

增加第二数据源的JPA配置

第二数据源的配置与第一数据源配置类似,代码如下:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "cn.ntshare.laboratory.domain.slave" }) //设置Repository所在位置
public class SecondarySourceConfig {

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

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

    @Resource
    private Properties jpaProperties;

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(secondaryDataSource)
                .packages("cn.ntshare.laboratory.domain.slave") //设置实体类所在位置
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }


    @Bean(name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

完成上述配置之后

主数据源的实体和数据访问对象位于:cn.ntshare.laboratory.domain.master

次数据源的实体和数据访问对象位于:cn.ntshare.laboratory.domain.slave

新建实体类和Repository

主数据源下创建MUserMUserRepository

@Entity(name = "m_user")
@Getter
@Setter
@NoArgsConstructor
public class MUser {

    @Id
    @GeneratedValue
    private Integer id;

    private String username;
}
@Repository
public interface MUserRepository extends JpaRepository {
}

次数据源下创建SUserSUserRepository

@Entity(name = "s_user")
@Getter
@Setter
@NoArgsConstructor
public class SUser {

    @Id
    @GeneratedValue
    private Integer id;

    private String username;
}

@Repository
public interface SUserRepository extends JpaRepository {
}

测试

你可能感兴趣的:(SpringBoot,ORM)