spring boot 2.0 jpa多数据源配置

spring boot 1.x内多数据源配置方式参考Spring Boot多数据源配置与使用,但升级到2后该配置无效,以下是自测可行的配置方式(spring boot版本2.0.6.RELEASE)。

1. application.properties配置

#数据源1
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver

#数据源2
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driverClassName = com.mysql.jdbc.Driver

2.第一数据源配置

import javax.sql.DataSource;

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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "primaryEntityManagerFactory",
		transactionManagerRef = "primaryTransactionManager",
		// 此处指定第一数据源对于dao包路径
		basePackages = "com.onecodespace.codegenerator.business.dao")
public class PrimaryConfig {

	@Bean
	PlatformTransactionManager primaryTransactionManager() {
		return new JpaTransactionManager(primaryEntityManagerFactory().getObject());
	}

	@Bean
	LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() {
		HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
		jpaVendorAdapter.setGenerateDdl(true);

		LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
		factoryBean.setDataSource(primaryDataSource());
		factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
		// 此处指定第一数据源对应实体类包路径
		factoryBean.setPackagesToScan("com.onecoderspace.codegenerator.business.domain");
		return factoryBean;
	}

//	@Bean
//	@ConfigurationProperties(prefix = "spring.datasource.primary")
//	DataSource primaryDataSource() {
//		return new EmbeddedDatabaseBuilder().
//				setType(EmbeddedDatabaseType.H2).
//				build();
//	}

	@Bean
	@ConfigurationProperties(prefix="spring.datasource.primary")
	public DataSource primaryDataSource() {
		//通过DataSourceBuilder构建数据源
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}
}

3. 第二数据源配置

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "secondaryEntityManagerFactory",
		transactionManagerRef = "secondaryTransactionManager",
		// 第二数据源对应dao包路径
		basePackages = "com.onecodespace.codegenerator.schema.dao")
public class SecondaryConfig extends DataSourceAutoConfiguration {

	@Bean
	PlatformTransactionManager secondaryTransactionManager() {
		return new JpaTransactionManager(secondaryEntityManagerFactory().getObject());
	}

	@Bean
	LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
		HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
		jpaVendorAdapter.setGenerateDdl(true);

		LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
		factoryBean.setDataSource(secondaryDataSource());
		factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
		// 第二数据源对应实体的包路径
		factoryBean.setPackagesToScan("com.onecodespace.codegenerator.schema.domain");
		return factoryBean;
	}

	@Bean
	@ConfigurationProperties(prefix="spring.datasource.secondary")
	public DataSource secondaryDataSource() {
		//通过DataSourceBuilder构建数据源
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}

         // 如果使用jdbcTemplate,进行如下设置即可
	@Bean(name = "secondaryJdbcTemplate")
	public JdbcTemplate secondaryJdbcTemplate(
			@Qualifier("secondaryDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

在两个数据源对应包下创建实体、dao、service,运行系统测试,可看到实体对应表分别在两个库内操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Spring BootJPA实现多数据源时,需要进行以下步骤: 1. 配置数据源:在application.properties文件中,配置多个数据源的连接信息。每个数据源都需要设置独特的前缀。 2. 创建多个数据源Bean:在代码中,使用@Configuration和@Bean注解创建多个数据源的实例。每个数据源的实例需要设置对应的连接信息。 3. 创建EntityManagerFactory:使用LocalContainerEntityManagerFactoryBean创建多个EntityManagerFactory实例。每个实例需要设置对应的数据源和持久化单元。 4. 创建JpaTransactionManager:使用PlatformTransactionManager的实现类JpaTransactionManager创建多个事务管理器实例。每个实例都需要设置对应的EntityManagerFactory。 5. 定义Repository:创建多个Repository接口,并分别使用@PersistenceContext注解注入不同的EntityManager实例。 6. 配置事务:使用@EnableTransactionManagement注解启用事务管理,并使用@Transactional注解定义事务的范围。 7. 使用不同的数据源:在代码中使用@Qualifier注解指定需要使用的数据源。在进行相关数据库操作时,使用相应的Repository来访问对应的数据源。 通过以上步骤,就能够使用Spring BootJPA实现多数据源配置和使用。每个数据源都可以连接到不同的数据库,对应的Repository可以访问并操作各自的数据源。这样就可以实现在同一个应用程序中对多个数据源的管理和操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值