在我要保存到两个数据源的持续努力中,我试图通过给它们两个自定义前缀添加两个数据源到我的application.properties文件中,就像在官方spring文档中所做的那样:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources。
当我这样做并插入数据时,该数据将不会保存到mysql数据库中,最终会出现空数据集和错误(请参见下文)。但是,如果我在数据源上使用原始前缀,而在另一个数据库上使用更改的前缀,则确实会保存,但只能在前缀未更改的数据库中保存。
这是我对用户(db1)的配置:
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
public class UserConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.db1")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource.db1.configuration")
public HikariDataSource firstDataSource() {
return firstDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(firstDataSource())
.packages("User.class")//User.class
.persistenceUnit("users")
.build();
}
}
产品(db2)的配置
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
public class ProductConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.datasource.db2")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.datasource.db2.configuration")
public BasicDataSource secondDataSource() {
return secondDataSourceProperties().initializeDataSourceBuilder()
.type(BasicDataSource.class).build();
}
@Bean
public LocalContainerEntityManagerFactoryBean productEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondDataSource())
.packages("Product.class")//Product.class
.persistenceUnit("products")
.build();
}
}
application.properties文件不起作用
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.db1.url=jdbc:mysql://localhost/db1?autoReconnect=true&useSSL=false
spring.datasource.db1.username=user
spring.datasource.db1.password=pass
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db1.configuration.maximum-pool-size=30
spring.datasource.db2.url=jdbc:mysql://localhost/db2?autoReconnect=true&useSSL=false
spring.datasource.db2.username=user
spring.datasource.db2.password=pass
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db2.max-total=30
错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
application.properties文件起作用一半。仅保存到db 1的数据。
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.url=jdbc:mysql://localhost/db1?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.configuration.maximum-pool-size=30
spring.datasource.db2.url=jdbc:mysql://localhost/db2?autoReconnect=true&useSSL=false
spring.datasource.db2.username=user
spring.datasource.db2.password=pass
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db2.max-total=30
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.multidb</groupId>
<artifactId>multipledb</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</project>
因此,这使我相信我的应用程序不接受更改的前缀。我在这里想念什么? 我尝试使用不同的Maven依赖版本,不同的驱动程序符号,并尝试添加H2数据库依赖项。但是我在一个错误中绊倒。
答案 0 :(得分:0)
确定尝试这样做:
您必须像这样拆分存储库:
src/main/java
- com.foobar
- user
- domain
- repo
- products
- domain
- repo
在仓库中放置您的@Repository接口,并在域中放置您的实体。
application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.url=jdbc:mysql://localhost/db1?autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.configuration.maximum-pool-size=30
db2.datasource.url=jdbc:mysql://localhost/db2?autoReconnect=true&useSSL=false
db2.datasource.username=user
db2.datasource.password=pass
db2.datasource.driver-class-name=com.mysql.jdbc.Driver
db2.datasource.max-total=30
DB1的配置(第一个数据源)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = { "com.foobar.user.repo" }
)
public class UserConfiguration {
@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.user.domain")
.persistenceUnit("userPU")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
DB2的配置(Secondy数据源)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef = "db2TransactionManager",
basePackages = { "com.foobar.products.repo" }
)
public class ProductConfiguration {
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix = "db2.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
db2EntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
return
builder
.dataSource(dataSource)
.packages("com.foobar.products.domain")
.persistenceUnit("productPU")
.build();
}
@Bean(name = "db2TransactionManager")
public PlatformTransactionManager db2TransactionManager(@Qualifier("db2EntityManagerFactory") EntityManagerFactory db2EntityManagerFactory) {
return new JpaTransactionManager(db2EntityManagerFactory);
}
}
希望这会有所帮助!