jpa多数据源配置
Posted 星辰大海
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jpa多数据源配置相关的知识,希望对你有一定的参考价值。
package com.ym.zhproservice.config; import org.springframework.beans.factory.annotation.Qualifier; 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 DataSourceConfig @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource masterDataSource() return DataSourceBuilder.create().build(); @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() return DataSourceBuilder.create().build();
package com.ym.zhproservice.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = "com.ym.zhproservice.dao.primary" //设置dao所在位置 ) public class PrimarySourceConfig @Autowired @Qualifier("primaryDataSource") private DataSource primaryDataSource; @Primary @Bean(name = "entityManagerPrimary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); @Resource private Properties jpaProperties; @Primary @Bean(name = "entityManagerFactoryPrimary") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) LocalContainerEntityManagerFactoryBean entityManagerFactory = builder .dataSource(primaryDataSource) .packages("com.ym.zhproservice.bean.primary") //设置实体类所在位置 .persistenceUnit("primaryPersistenceUnit") .build(); entityManagerFactory.setJpaProperties(jpaProperties); return entityManagerFactory; @Primary @Bean(name = "transactionManagerPrimary") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
package com.ym.zhproservice.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackages= "com.ym.zhproservice.dao.secondary" ) //设置dao所在位置 public class SecondarySourceConfig @Autowired @Qualifier("secondaryDataSource") private DataSource secondaryDataSource; @Bean(name = "entityManagerSecondary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) return entityManagerFactorySecondary(builder).getObject().createEntityManager(); @Resource private Properties jpaProperties; @Bean(name = "entityManagerFactorySecondary") public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) LocalContainerEntityManagerFactoryBean entityManagerFactory = builder .dataSource(secondaryDataSource) .packages("com.ym.zhproservice.bean.secondary") //设置实体类所在位置 .persistenceUnit("secondaryPersistenceUnit") .build(); entityManagerFactory.setJpaProperties(jpaProperties); return entityManagerFactory; @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
以上是关于jpa多数据源配置的主要内容,如果未能解决你的问题,请参考以下文章