spring boot 多数据源配置
2018-09-03 本文已影响0人
menghuijia
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
此项配置用于配置数据库方言为InnoDB
,MySQL
默认方言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<Primary, Long> {}
package com.meng.dynamic.repository2;
import com.meng.dynamic.domain2.Secondary;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SecondaryRepostitory extends JpaRepository<Secondary, Long> {}
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
-
这只是多数据源配置,不是动态数据源 ↩