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);
}

最新文章

  1. ACM 找点
  2. 收集的github的东西
  3. respond.js
  4. AS3下如何来判断XML属性的是否存在
  5. 破解入门【OllyDebug爆破程序】
  6. hdu 3018 Ant Trip 欧拉回路+并查集
  7. POJ 1077 &amp;&amp; HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
  8. c语言面试题之sizeof
  9. jmeter压测app
  10. Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld
  11. Oracle入门学习笔记
  12. was性能调优前期准备
  13. Jmeter(二)_基础元件
  14. 页面加载完之前显示Loading
  15. Chrome - JavaScript调试技巧总结(浏览器调试JS)
  16. 自动化脚本测试,postman使用沉淀
  17. debug apk logCat
  18. 上网爱快?EasyRadius FOR 爱快V2接口测试版正式推出,欢迎广大爱迷们测试噢
  19. python 标准输入输出sys.stdout. sys.stdin
  20. testNG断言

热门文章

  1. Guava 开源工具的简单介绍
  2. Apache之默认配置文件解释
  3. dbvisualizer设置自动补全不显示模式名
  4. JS面向对象——构造函数模型
  5. chrome浏览器屏蔽广告插件小例子
  6. bash date format
  7. Pycharm文档模板变量
  8. 【leetcode】901. Online Stock Span
  9. Springboot与jsp使用404错误
  10. LUOGU P2617 Dynamic Rankings(树状数组套主席树)