欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot实现多数据源

程序员文章站 2024-03-22 18:24:58
...

基于上次的SpringBoot初识

1.配置文件

SpringBoot实现多数据源

spring.datasource.primary.driverClassName=com.mysql.jdbc.Driver
spring.datasource.primary.url=jdbc:mysql://10.10.10.110:3306/dbName?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
spring.datasource.primary.username=root
spring.datasource.primary.password=pwd
spring.datasource.primary.test-on-borrow=true
spring.datasource.primary.validation-query=select 1
spring.datasource.primary.test-while-idle=true
spring.datasource.primary.max-idle=20
spring.datasource.primary.max-wait=10000
spring.datasource.primary.min-idle=10
spring.datasource.primary.initial-size=10
spring.datasource.primary.max-active=100

spring.datasource.secondary.driverClassName=org.postgresql.Driver
spring.datasource.secondary.url=jdbc:postgresql://10.10.10.110:5432/dbName
spring.datasource.secondary.username=root
spring.datasource.secondary.password=pwd
spring.datasource.secondary.test-on-borrow=true
spring.datasource.secondary.validation-query=select 1
spring.datasource.secondary.test-while-idle=true
spring.datasource.secondary.max-idle=5
spring.datasource.secondary.max-wait=10000
spring.datasource.secondary.min-idle=1
spring.datasource.secondary.initial-size=5
spring.datasource.secondary.max-active=5

2.数据库配置类

SpringBoot实现多数据源

/**
 * 主数据库配置
 * @author
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(enableDefaultTransactions = false, entityManagerFactoryRef = "primaryLocalContainerEntityManagerFactoryBean", transactionManagerRef = "primaryPlatformTransactionManager", basePackages = {"com.navi.loaddata.core.repository.impl"})
public class SpringBootConfiguration5DBPrimary {

	/** 数据源 */
	@Autowired
	@Qualifier("primaryDataSource")
	@Lazy
	private DataSource dataSource;

	/** JPA配置属性 */
	@Autowired
	private JpaProperties jpaProperties;


	/**
	 * 构建主数据源
	 * @return 数据源
	 */
	@Bean(name = "primaryDataSource")
	@Qualifier(value = "primaryDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.primary")
	@Primary
	public DataSource buildPrimaryDataSource() {
		return DataSourceBuilder.create().build();
	}


	/**
	 * 定义实体类管理对象
	 * @param builder 实体类管理对象工厂
	 * @return 实体类管理对象
	 */
	@Primary
	@Bean(name = "primaryEntityManager")
	public EntityManager primaryEntityManager(final EntityManagerFactoryBuilder builder) {
		return primaryLocalContainerEntityManagerFactoryBean(builder).getObject().createEntityManager();
	}


	/**
	 * 配置实体类管理对象工厂
	 * @param builder 实体类管理对象工厂
	 * @return 实体类管理对象工厂
	 */
	@Primary
	@Bean(name = "primaryLocalContainerEntityManagerFactoryBean")
	public LocalContainerEntityManagerFactoryBean primaryLocalContainerEntityManagerFactoryBean(final EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dataSource).properties(jpaProperties.getHibernateProperties(dataSource)).packages("com.navi.**.mysqlEntity").persistenceUnit("primaryPersistenceUnit").build();
	}


	/**
	 * 定义事务管理对象
	 * @param builder 实体类管理对象工厂
	 * @return 事务管理对象
	 */
	@Primary
	@Bean(name = "primaryPlatformTransactionManager")
	public PlatformTransactionManager primaryPlatformTransactionManager(final EntityManagerFactoryBuilder builder) {
		final JpaTransactionManager transactionManager = new JpaTransactionManager(primaryLocalContainerEntityManagerFactoryBean(builder).getObject());
		return transactionManager;
	}

}

3.配置实体类管理类

SpringBoot实现多数据源

@Repository("daomes")
public class MesRepositoryImpl extends NaviRepositoryImpl {
    /** 实体类管理类对象 */
    @PersistenceContext(unitName = "mesPersistenceUnit")
    private EntityManager entityManager;


    @Override
    public EntityManager getEntityManager() {
        return entityManager;
    }

//获取事务Session
    public Session getEntityManagerSession() {
        HibernateEntityManager hEntityManager = (HibernateEntityManager)entityManager;
        Session session = hEntityManager.getSession();
        return session;
    }
}

4.使用

SpringBoot实现多数据源



相关标签: JAVA 多数据源