Spring Boot,Spring Data配置多数据源DataSource

这里已配置Oracle和PostgreSQL为例。

application.properties配置多数据源

# Oracle DB - "foo"
spring.datasource.url=jdbc:oracle:thin:@//db-server-foo:1521/FOO
spring.datasource.username=fooadmin
spring.datasource.password=foo123
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
# PostgreSQL DB - "bar"
bar.datasource.url=jdbc:postgresql://db-server-bar:5432/bar
bar.datasource.username=baradmin
bar.datasource.password=bar123
bar.datasource.driver-class-name=org.postgresql.Driver

foo为oracle数据库,bar为PostgreSQL数据库。

application.properties配置SQL方言为default

spring.jpa.database=default

分离不同数据源的目录

src/main/java
- com.foobar
- foo
- domain
- repo
- bar
- domain
- repo

创建Oracle数据库Foo的配置FooDbConfig.java

package com.foobar;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.foobar.foo.repo" }
)
public class FooDbConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.foobar.foo.domain")
.persistenceUnit("foo")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}

配置需要指定basePackages为foo数据库对应的目录com.foobar.foo.repo

创建PostgreSQL数据库Bar配置BarDbConfig.java

package com.foobar;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager",
basePackages = { "com.foobar.bar.repo" }
)
public class BarDbConfig {
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.foobar.bar.domain")
.persistenceUnit("bar")
.build();
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory
barEntityManagerFactory
) {
return new JpaTransactionManager(barEntityManagerFactory);
}
}

配置需要指定basePackages为bar数据库对应的目录com.foobar.bar.repo

创建对应Oracle数据库foo模型Foo.java

package com.foobar.foo.domain;
@Entity
@Table(name = "FOO")
public class Foo {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "FOO")
private String foo;
Foo(String foo) {
this.foo = foo;
}
Foo() {
}
}

创建FooRepository.java

package com.foobar.foo.repo;
@Repository
public interface FooRepository extends JpaRepository<Foo, Long> {
Foo findById(Long id);
}

创建PostgreSQL数据库bar对应的模型Bar.java

package com.foobar.bar.domain;
@Entity
@Table(name = "BAR")
public class Bar {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "BAR")
private String bar;
Bar(String bar) {
this.bar = bar;
}
Bar() {
}
}

BarRepository.java

package com.foobar.bar.repo;
@Repository
public interface BarRepository extends JpaRepository<Bar, Long> {
Bar findById(Long id);
}

RestController似乎用FooRepository和BarRepository

package com.foobar;
@RestController
public class FooBarController {
private final FooRepository fooRepo;
private final BarRepository barRepo;
@Autowired
FooBarController(FooRepository fooRepo, BarRepository barRepo) {
this.fooRepo = fooRepo;
this.barRepo = barRepo;
}
@RequestMapping("/foobar/{id}")
public String fooBar(@PathVariable("id") Long id) {
Foo foo = fooRepo.findById(id);
Bar bar = barRepo.findById(id);
return foo.getFoo() + " " + bar.getBar();
}
}

创建Spring Boot主类“Application.java”

package com.foobar;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Spring Data会根据不同的数据源自动切换DataSource

参考:https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

版权声明:著作权归作者所有。

相关推荐

Spring Boot集成RabbitMQ发送接收JSON

在Spring Boot 集成RabbitMQ一文中介绍了如何集成RabbitMQ。默认情况下发送的消息是转换为字节码,这里介绍一下如何发送JSON数据。ObjectMapper最简单发送JSON数据的方式是把对象使用ObjectMapper等JSON工具类把对象转换为JSON格式,然后发送。如下:@Autowired private ObjectMapper&nbs

Spring Boot 集成RabbitMQ

此处假设已经安装好了RabbitMQ,主要讲述使用Spring Boot如何集成RabbitMQ。添加依赖在Maven的pom.xml添加rabbitmq的starter依赖,内容如下:<?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo

Spring Boot使用springProfile实现Logback多环境的通用配置

在一个基于Spring boot开发的项目里,常常需要有多套环境的配置:开发,测试以及产品。这里给出一个logback的通用配置。在src/main/resources目录下创建配置文件logback-spring.xml,多环境的通用配置内容如下:<?xml version="1.0" encoding="UTF-8"?

Spring Boot设置服务器的端口

Spring Boot默认内嵌的web服务器为tomcat,端口为8080。如果想修改内嵌tomcat插件的端口有三种方式:在application.properties里修改在java的启动脚本里设置在java代码里设置在application.properties设置server.port=9999 如果是application.yml,设置如下:server:  &nbs

Spring Boot集成kafka

Spring Boot对kafka提供了自动配置(auto configuration)。使用用Spring Boot只需要做很少的配置即可集成对kafka的访问。pom配置1、继承spring-boot-starter-parent<parent>   <groupId>org.springframework.boot&l

Spring Boot:日志集成

Java日志框架Java有好几个日志框架,我们有时在选择Java日志框架时会有点迷惑。下面几个是常用的日志框架Common Logging:Apache最早提供的日志的门面接口。避免和具体的日志方案直接耦合。SLF4J(Simple Logging Facade for JAVA):和Common Logging一样,是一个门面框架,是对