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

时间:2022-06-01 16:42:34

这里已配置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