先在Spring Boot的配置文件application.properties
中设置两个你要链接的数据库配置,比如这样:
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=123456
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=123456
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
日志打印执行的SQL
spring.jpa.show-sql=true
Hibernate的DDL策略
spring.jpa.hibernate.ddl-auto=create-drop
这里除了JPA自身相关的配置之外,与JdbcTemplate配置时候的数据源配置完全是一致的
说明与注意:
-
多数据源配置的时候,与单数据源不同点在于
spring.datasource
之后多设置一个数据源名称primary
和secondary
来区分不同的数据源配置,这个前缀将在后续初始化数据源的时候用到。 -
数据源连接配置2.x和1.x的配置项是有区别的:2.x使用
spring.datasource.secondary.jdbc-url
,而1.x版本使用spring.datasource.secondary.url
。如果你在配置的时候发生了这个报错java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
,那么就是这个配置项的问题。
完成多数据源的配置信息之后,就来创建个配置类来加载这些配置信息,初始化数据源,以及初始化每个数据源要用的JdbcTemplate。
由于JPA的配置要比JdbcTemplate的负责很多,所以我们将配置拆分一下来处理:
- 单独建一个多数据源的配置类,比如下面这样:
@Configuration
public class DataSourceConfiguration {
@Primary
@Bean
@ConfigurationProperties(prefix = “spring.datasource.primary”)
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = “spring.datasource.secondary”)
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
可以看到内容跟JdbcTemplate时候是一模一样的。通过@ConfigurationProperties
可以知道这两个数据源分别加载了spring.datasource.primary.*
和spring.datasource.secondary.*
的配置。@Primary
注解指定了主数据源,就是当我们不特别指定哪个数据源的时候,就会使用这个Bean真正差异部分在下面的JPA配置上。
- 分别创建两个数据源的JPA配置。
Primary数据源的JPA配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef=“entityManagerFactoryPrimary”,
transactionManagerRef=“transactionManagerPrimary”,
basePackages= { “com.didispace.chapter38.p” }) //设置Repository所在位置
public class PrimaryConfig {
@Autowired
@Qualifier(“primaryDataSource”)
private DataSource primaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Primary
@Bean(name = “entityManagerPrimary”)
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = “entityManagerFactoryPrimary”)
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.packages(“com.didispace.chapter38.p”) //设置实体类所在位置
.persistenceUnit(“primaryPersistenceUnit”)
.properties(getVendorProperties())
.build();
}
@Primary
@Bean(name = “transactionManagerPrimary”)
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
Secondary数据源的JPA配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef=“entityManagerFactorySecondary”,
transactionManagerRef=“transactionManagerSecondary”,
basePackages= { “com.didispace.chapter38.s” }) //设置Repository所在位置
public class SecondaryConfig {
@Autowired
@Qualifier(“secondaryDataSource”)
private DataSource secondaryDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
private Map<String, Object> getVendorProperties() {
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Bean(name = “entityManagerSecondary”)
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
@Bean(name = “entityManagerFactorySecondary”)
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.packages(“com.didispace.chapter38.s”) //设置实体类所在位置
.persistenceUnit(“secondaryPersistenceUnit”)
.properties(getVendorProperties())
.build();
}
@Bean(name = “transactionManagerSecondary”)
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {