spring boot 多数据源配置

1. 配置文件[1]

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    password: root
    username: root
    url: jdbc:mysql://localhost:3306/demo1?useUnicode=true&characterEncoding=utf8&useSSL=true
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true
    properties:
      hibernate:
       format_sql: true
    database-platform: org.hibernate.dialect.MySQL57Dialect
secondary:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    password: root
    username: root
    url: jdbc:mysql://localhost:3306/demo2?useUnicode=true&characterEncoding=utf8&useSSL=true
  • spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect此项配置用于配置数据库方言为InnoDBMySQL默认方言MyISAM不支持事务
  • spring.datasource为第一个数据库配置
  • secondary.datasource为第二个数据库配置

2. 实体

package com.meng.dynamic.domain1;

import javax.persistence.*;

@Entity
@Table(name = "t_primary")
public class Primary {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    
    // 省略了set get 方法
}
package com.meng.dynamic.domain2;

import javax.persistence.*;

@Entity
@Table(name = "t_secondary")
public class Secondary {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    
    // 省略了set get 方法
}

3. Repository

package com.meng.dynamic.repository1;

import com.meng.dynamic.domain1.Primary;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PrimaryRepostitory extends JpaRepository {}
package com.meng.dynamic.repository2;

import com.meng.dynamic.domain2.Secondary;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SecondaryRepostitory extends JpaRepository {}

4. config

package com.meng.dynamic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(
        basePackages = "com.meng.dynamic.repository1",
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary"
)
public class PrimaryConfig {

    @Autowired
    private JpaProperties jpaProperties;

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperties dataSourcePropertiesPrimary() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource dataSourcePrimary() {
        return dataSourcePropertiesPrimary().initializeDataSourceBuilder().build();
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSourcePrimary())
                .packages("com.meng.dynamic.domain1")
                .persistenceUnit("primary")
                .properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
                .build();
    }

    @Bean
    @Primary
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactory entityManagerFactoryPrimary) {
        return new JpaTransactionManager(entityManagerFactoryPrimary);
    }

}
package com.meng.dynamic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableJpaRepositories(
        basePackages = "com.meng.dynamic.repository2",
        entityManagerFactoryRef = "entityManagerFactorySecondary",
        transactionManagerRef = "transactionManagerSecondary"
)
public class SecondaryConfig {

    @Autowired
    private JpaProperties jpaProperties;

    @Bean
    @ConfigurationProperties(prefix = "secondary.datasource")
    public DataSourceProperties dataSourcePropertiesSecondary() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource dataSourceSecondary() {
        return dataSourcePropertiesSecondary().initializeDataSourceBuilder().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSourceSecondary())
                .packages("com.meng.dynamic.domain2")
                .persistenceUnit("secondary")
                .properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
                .build();
    }

    @Bean
    public PlatformTransactionManager transactionManagerSecondary(@Qualifier("entityManagerFactorySecondary") EntityManagerFactory entityManagerFactorySecondary) {
        return new JpaTransactionManager(entityManagerFactorySecondary);
    }

}
  • JpaProperties为Jpa的配置,即spring.jpa中的配置
  • DataSourceProperties为数据库配置
  • @EnableJpaRepositories中的basePackages属性为Repository类所在的package
  • EntityManagerFactoryBuilder中的packages方法为实体类所在package

  1. 这只是多数据源配置,不是动态数据源 ↩

你可能感兴趣的:(spring boot 多数据源配置)