關于springboot配置多資料源,整理了這篇筆記,分享給有需要的小伙伴們,視頻看的動力節點王鶴老師講的springboot
視瞥澩:https://www.bilibili.com/video/BV1XQ4y1m7ex
一、目錄結構
目錄結構

?
二、依賴包(pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
</dependencies>
三、組態檔
server:
port: 8080
spring:
datasource:
first:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/first?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
username: root
password: root
second:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/second?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
username: root
password: root
jpa:
hibernate:
ddl-auto: update
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: mysql
四、多資料源配置(jpa)
1. DataSourceConfiguration
package com.cetc.config;
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;
/**
* 資料庫配置
*/
@Configuration
public class DataSourceConfiguration {
/**
* 第一個資料連接,默認優先級最高
* @return
*/
@Bean(name = "dataSourceFirst")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource dataSourceFirst() {
//這種方式的配置默認只滿足spring的配置方式,如果使用其他資料連接(druid),需要自己獨立獲取配置
return DataSourceBuilder.create().build();
}
/**
* 第二個資料源
* @return
*/
@Bean(name = "dataSourceSecond")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource dataSourceSecond() {
return DataSourceBuilder.create().build();
}
}
說明:其實這里配置已經完成了,這里就配置了兩個資料源了,可以加入對應的JdbcTemplate,這里不做介紹,比較簡單
2. JpaFirstConfiguration
package com.cetc.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
/**
* 第一個資料源,jpa的相關配置
*/
@Configuration
@EntityScan(basePackages = "com.cetc.domain.first")
//1、物體掃描
//2、物體管理ref
//3、事務管理
@EnableJpaRepositories(
basePackages = "com.cetc.repository.first",
entityManagerFactoryRef = "firstEntityManagerFactoryBean",
transactionManagerRef = "firstTransactionManager")
@EnableTransactionManagement
public class JpaFirstConfiguration {
//第一個資料源,可以不加Qualifier
@Autowired
@Qualifier("dataSourceFirst")
private DataSource dataSource;
//jpa其他引數配置
@Autowired
private JpaProperties jpaProperties;
//物體管理工廠builder
@Autowired
private EntityManagerFactoryBuilder factoryBuilder;
/**
* 配置第一個物體管理工廠的bean
* @return
*/
@Bean(name = "firstEntityManagerFactoryBean")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
return factoryBuilder.dataSource(dataSource)
//這一行的目的是加入jpa的其他配置引數比如(ddl-auto: update等)
//當然這個引數配置可以在事務配置的時候也可以
.properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
.packages("com.cetc.domain.first")
.persistenceUnit("firstPersistenceUnit")
.build();
}
/**
* EntityManager不過解釋,用過jpa的應該都了解
* @return
*/
@Bean(name = "firstEntityManager")
@Primary
public EntityManager entityManager() {
return entityManagerFactoryBean().getObject().createEntityManager();
}
/**
* jpa事務管理
* @return
*/
@Bean(name = "firstTransactionManager")
@Primary
public JpaTransactionManager transactionManager() {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return jpaTransactionManager;
}
}
3. JpaSecondConfiguration
package com.cetc.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
/**
* 第二個資料源,jpa的相關配置
*/
@Configuration
@EntityScan(basePackages = "com.cetc.domain.second")
//1、物體掃描
//2、物體管理ref
//3、事務管理
@EnableJpaRepositories(
basePackages = "com.cetc.repository.second",
entityManagerFactoryRef = "secondEntityManagerFactoryBean",
transactionManagerRef = "secondTransactionManager")
@EnableTransactionManagement
public class JpaSecondConfiguration {
//第二個資料源,必須加Qualifier
@Autowired
@Qualifier("dataSourceSecond")
private DataSource dataSource;
//jpa其他引數配置
@Autowired
private JpaProperties jpaProperties;
//物體管理工廠builder
@Autowired
private EntityManagerFactoryBuilder factoryBuilder;
/**
* 配置第二個物體管理工廠的bean
* @return
*/
@Bean(name = "secondEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
return factoryBuilder.dataSource(dataSource)
.properties(jpaProperties.getHibernateProperties(new HibernateSettings()))
.packages("com.cetc.domain.second")
.persistenceUnit("secondPersistenceUnit")
.build();
}
/**
* EntityManager不過解釋,用過jpa的應該都了解
* @return
*/
@Bean(name = "secondEntityManager")
public EntityManager entityManager() {
return entityManagerFactoryBean().getObject().createEntityManager();
}
/**
* jpa事務管理
* @return
*/
@Bean(name = "secondTransactionManager")
public JpaTransactionManager transactionManager() {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
return jpaTransactionManager;
}
}
?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/445886.html
標籤:其他
