Spring Boot,Spring Data JPA多数据源支持配置
2024-09-02 00:42:24
1 配置文件
wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.primary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1521\:xe
wisely.primary.datasource.username=gis
wisely.primary.datasource.password=gis wisely.secondary.datasource.driverClassName=oracle.jdbc.OracleDriver
wisely.secondary.datasource.url=jdbc\:oracle\:thin\:@192.168.1.103\:1522\:xe
wisely.secondary.datasource.username=gis
wisely.secondary.datasource.password=gis spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.auto-ddl=update
2 datasource配置
第一个数据源
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; @Configuration
public class DataSourcePrimaryConfig { @Bean(name = "primaryDS") @Qualifier("primaryDS")
@Primary
@ConfigurationProperties(prefix="wisely.primary.datasource")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
} }
第二个数据源
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; @Configuration
public class DataSourceSecondaryConfig {
@Bean(name = "secondaryDS") @Qualifier("secondaryDS")
@ConfigurationProperties(prefix="wisely.secondary.datasource")
public DataSource secondaryDataSource(){
return DataSourceBuilder.create().build();
}
}
3 实体管理器及事务管理器配置
第一个数据源
import java.util.Map; import javax.persistence.EntityManager;
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",transactionManagerRef="transactionManagerPrimary",basePackages= { "com.wisely.demo.dao.one" })/ /设置dao(repo)所在位置
public class RepositoryPrimaryConfig {
@Autowired
private JpaProperties jpaProperties; @Autowired @Qualifier("primaryDS")
private DataSource primaryDS; @Bean(name = "entityManagerPrimary")
@Primary
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
} @Bean(name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDS)
.properties(getVendorProperties(primaryDS))
.packages("com.wisely.demo.domain.one") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
} private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
} @Bean(name = "transactionManagerPrimary")
@Primary
PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
} }
第二个数据源
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; @Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",transactionManagerRef="transactionManagerSecondary",basePackages= { "com.wisely.demo.dao.two" })
public class RepositorySecondaryConfig {
@Autowired
private JpaProperties jpaProperties; @Autowired @Qualifier("secondaryDS")
private DataSource secondaryDS; @Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
} @Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDS)
.properties(getVendorProperties(secondaryDS))
.packages("com.wisely.demo.domain.two")
.persistenceUnit("secondaryPersistenceUnit")
.build();
} private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
} @Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
} }
4 使用
此时来自不同数据库的dao(repo)可以任意在其它的bean里注入
@Controller
public class TestController {
@Autowired
SysRoleRepo1 sysRoleRepo1;
@Autowired
SysRoleRepo2 sysRoleRepo2;
@RequestMapping("/test")
public @ResponseBody String test(){
System.out.println(Lists.newArrayList(sysRoleRepo1.findAll()).size());
System.out.println(Lists.newArrayList(sysRoleRepo2.findAll()).size());
return "ok";
}
}
5 ENTITY
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.ID; @Entity
@Table(name = "employee")
public class Employee { private @Id @GeneratedValue Long id;
private String firstName, lastName, description; private Employee() {} public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
}
6.Respority
import org.springframework.data.repository.CrudRepository; public interface EmployeeRepository extends CrudRepository<Employee, Long> { Employee findByFirstName(String firstName); List<Employee> findByLastName(String lastName);
}
最新文章
- ACM 找点
- 收集的github的东西
- respond.js
- AS3下如何来判断XML属性的是否存在
- 破解入门【OllyDebug爆破程序】
- hdu 3018 Ant Trip 欧拉回路+并查集
- POJ 1077 &;&; HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
- c语言面试题之sizeof
- jmeter压测app
- Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld
- Oracle入门学习笔记
- was性能调优前期准备
- Jmeter(二)_基础元件
- 页面加载完之前显示Loading
- Chrome - JavaScript调试技巧总结(浏览器调试JS)
- 自动化脚本测试,postman使用沉淀
- debug apk logCat
- 上网爱快?EasyRadius FOR 爱快V2接口测试版正式推出,欢迎广大爱迷们测试噢
- python 标准输入输出sys.stdout. sys.stdin
- testNG断言