目录
前言
前置环境
pom
yml
Entity
Dao
Config
Controller
演示
前言
一般一个系统至少有一个数据源,用来持久化业务数据以及查询。单个数据源的系统很常见,在 Spring Boot 框架下配置也很简单。在约定大于配置这个思想下,只需要在配置文件中简单配置下数据库连接信息就行了。
除了单数据源系统,在实际工作中,还会遇到多数据源系统。有可能是因为业务需要,以实际中做过的资产清查相关的项目为例,数据的存储分成了两个库,一个当前库和归档库,系统就需要配置两个数据源来满足业务需求。也有可能是技术架构,比如 MySQL 数据库使用了主从复制架构来实现读写分离,然后在应用层做分流,就需要配置主库和从库数据源,并在代码中决定使用哪个库进行数据库操作。
以下是Spring Boot JPA 多数据源的配置教程
前置环境
JDK8 + SringBoot2 + MySQL8
分别创建数据库 test1 test2
分别在两个数据库中创建 user 表
create table user (
id int auto_increment primary key,
username varchar(255),
password varchar(255)
);
在test1.user 表中插入数据
insert into user(username, password) values('张三', '123456');
在test2.user 表中插入数据
insert into user(username, password) values('李四', '123456');
pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<dependencies>
yml
server: port: 8888 spring: datasource: primary: url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true username: root password: mysql driver-class-name: com.mysql.cj.jdbc.Driver secondary: url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true username: root password: mysql driver-class-name: com.mysql.cj.jdbc.Driver jpa: primary: show-sql: true properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect secondary: show-sql: true properties: hibernate: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Entity
主库和从库的实体类需要放在不同的包下,以本文为例
主库的实体类包路径为:com.jpa.entity.primary
@Entity
@Table ( name = "user")
public class PrimaryUserEntity {
private Integer id;
private String username;
private String password;
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY)
@Column ( name = "id" )
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column ( name = "username" )
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column ( name = "password" )
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
从库的实体类包路径为:com.jpa.entity.secondary
@Entity
@Table ( name = "user")
public class SecondaryUserEntity {
private Integer id;
private String username;
private String password;
@Id
@GeneratedValue ( strategy = GenerationType.IDENTITY)
@Column ( name = "id" )
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column ( name = "username" )
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column ( name = "password" )
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Dao
主库和从库的Dao需要放在不同的包下,以本文为例
主库的Dao包路径为:com.jpa.dao.primary
@Repository (value = IPrimaryUserDao.DAO_BEAN_NAME )
public interface IPrimaryUserDao extends JpaRepository<PrimaryUserEntity, Integer> {
String DAO_BEAN_NAME = "primaryUserDao";
}
从库的Dao包路径为:com.jpa.dao.secondary
@Repository (value = ISecondaryUserDao.DAO_BEAN_NAME )
public interface ISecondaryUserDao extends JpaRepository<SecondaryUserEntity, Integer> {
String DAO_BEAN_NAME = "secondaryUserDao";
}
Config
记得将类中的两个包路径修改成自己项目的包路径
REPOSITORY_PACKAGE
ENTITY_PACKAGE
主库数据源配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories (
basePackages = PrimaryDatasourceAndJpaConfig.REPOSITORY_PACKAGE,
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager"
)
public class PrimaryDatasourceAndJpaConfig {
private static final String REPOSITORY_PACKAGE = "com.jpa.dao.primary";
private static final String ENTITY_PACKAGE = "com.jpa.entity.primary";
//--------------数据源配置-------------------
/**
* 扫描spring.datasource.primary开头的配置信息
*
* @return 数据源配置信息
*/
@Primary
@Bean(name = "primaryDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
/**
* 取主库数据源对象
*
* @param dataSourceProperties 注入名为primaryDataSourceProperties的bean
* @return 数据源对象
*/
@Primary
@Bean(name = "primaryDataSource")
public DataSource dataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
/**
* 扫描spring.jpa.primary开头的配置信息
*
* @return jpa配置信息
*/
@Primary
@Bean (name = "primaryJpaProperties")
@ConfigurationProperties (prefix = "spring.jpa.primary")
public JpaProperties jpaProperties() {
return new JpaProperties();
}
/**
* 获取主库实体管理工厂对象
*
* @param primaryDataSource 注入名为primaryDataSource的数据源
* @param jpaProperties 注入名为primaryJpaProperties的jpa配置信息
* @param builder 注入EntityManagerFactoryBuilder
* @return 实体管理工厂对象
*/
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
@Qualifier ("primaryDataSource") DataSource primaryDataSource,
@Qualifier("primaryJpaProperties") JpaProperties jpaProperties,
EntityManagerFactoryBuilder builder
) {
return builder
// 设置数据源
.dataSource(primaryDataSource)
// 设置jpa配置
.properties(jpaProperties.getProperties())
// 设置实体包名
.packages(ENTITY_PACKAGE)
// 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
.persistenceUnit("primaryPersistenceUnit").build();
}
/**
* 获取实体管理对象
*
* @param factory 注入名为primaryEntityManagerFactory的bean
* @return 实体管理对象
*/
@Primary
@Bean(name = "primaryEntityManager")
public EntityManager entityManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
return factory.createEntityManager();
}
/**
* 获取主库事务管理对象
*
* @param factory 注入名为primaryEntityManagerFactory的bean
* @return 事务管理对象
*/
@Primary
@Bean(name = "primaryTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
}
}
从库数据源配置
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = SecondaryDatasourceAndJpaConfig.REPOSITORY_PACKAGE,
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager"
)
public class SecondaryDatasourceAndJpaConfig {
static final String REPOSITORY_PACKAGE = "com.jpa.dao.secondary";
static final String ENTITY_PACKAGE = "com.jpa.entity.secondary";
//--------------数据源配置-------------------
/**
* 扫描spring.datasource.secondary开头的配置信息
*
* @return 数据源配置信息
*/
@Bean(name = "secondaryDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
/**
* 获取次数据源对象
*
* @param dataSourceProperties 注入名为secondaryDataSourceProperties的bean
* @return 数据源对象
*/
@Bean("secondaryDataSource")
public DataSource dataSource(@Qualifier("secondaryDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
/**
* 扫描spring.jpa.secondary
*
* @return jpa配置信息
*/
@Bean(name = "secondaryJpaProperties")
@ConfigurationProperties(prefix = "spring.jpa.secondary")
public JpaProperties jpaProperties() {
return new JpaProperties();
}
/**
* 获取次库实体管理工厂对象
*
* @param secondaryDataSource 注入名为secondaryDataSource的数据源
* @param jpaProperties 注入名为secondaryJpaProperties的jpa配置信息
* @param builder 注入EntityManagerFactoryBuilder
* @return 实体管理工厂对象
*/
@Bean(name = "secondaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier("secondaryDataSource") DataSource secondaryDataSource,
@Qualifier("secondaryJpaProperties") JpaProperties jpaProperties,
EntityManagerFactoryBuilder builder
) {
return builder
// 设置数据源
.dataSource(secondaryDataSource)
// 设置jpa配置
.properties(jpaProperties.getProperties())
// 设置实体包名
.packages(ENTITY_PACKAGE)
// 设置持久化单元名,用于@PersistenceContext注解获取EntityManager时指定数据源
.persistenceUnit("secondaryPersistenceUnit").build();
}
/**
* 获取实体管理对象
*
* @param factory 注入名为secondaryEntityManagerFactory的bean
* @return 实体管理对象
*/
@Bean(name = "secondaryEntityManager")
public EntityManager entityManager(@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory factory) {
return factory.createEntityManager();
}
/**
* 获取事务管理对象
*
* @param factory 注入名为secondaryEntityManagerFactory的bean
* @return 事务管理对象
*/
@Bean(name = "secondaryTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
}
}
Controller
主库
@RestController
@RequestMapping(value = "/primary")
public class PrimaryJpaController {
@Resource
private IPrimaryUserDao primaryUserDao;
@RequestMapping(value = "/findAll", method = RequestMethod.GET)
public List<PrimaryUserEntity> findAll() {
return primaryUserDao.findAll();
}
}
从库
@RestController
@RequestMapping(value = "/secondary")
public class SecondaryJpaController{
@Resource
private ISecondaryUserDao secondaryUserDao;
@RequestMapping(value = "/findAll", method = RequestMethod.GET)
public List<SecondaryUserEntity> findAll() {
return secondaryUserDao.findAll();
}
}
演示
请求 /primary/findAll
请求 /secondary/findAll