应用程序.yml
spring:
security:
user:
name: test
password: admin
datasource:
platform: postgres
jdbc-url: jdbc:postgresql://localhost:5432/ktnb
username: xxxx
password: xxxx
driverClassName: org.postgresql.Driver
sqlserver-datasource:
jdbc-url: jdbc:sqlserver://192.168.0.10;databaseName=backup1
username: xxx
password: xxx
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
primarydbconfig.java文件
package com.ktnb.keahlian.config;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 = "entityManagerFactory",
transactionManagerRef = "transactionManager",
basePackages = { "com.ktnb.keahlian.repository" }
)
public class PrimaryDBConfig {
@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
) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("spring.jpa.database", "postgresql");
properties.put("spring.jpa.show-sql", "true");
properties.put("spring.jpa.hibernate.ddl-auto", "create");
properties.put("spring.jpa.properties.hibernate.default_schema", "keahlian");
properties.put("spring.jpa.org.hibernate.envers.default_schema", "keahlian_envers");
properties.put("spring.jpa.org.hibernate.envers.audit_strategy", "org.hibernate.envers.strategy.ValidityAuditStrategy");
return builder
.dataSource(dataSource)
.packages("com.ktnb.keahlian.entity")
.persistenceUnit("primaryDB")
.properties(properties)
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
但它找不到正确的表/模式,也无法将sql显示到控制台中。
开发数据库的主要数据源。
生产数据库的辅助数据源。
[INFO ] 2020-08-21 09:38:30.116 [http-nio-8080-exec-1] SessionListenerImpl - ==== Session is created ====
[INFO ] 2020-08-21 09:38:30.116 [http-nio-8080-exec-1] SessionListenerImpl - Total active session are 1
[WARN ] 2020-08-21 09:38:32.480 [http-nio-8080-exec-6] SqlExceptionHelper - SQL Error: 0, SQLState: 42P01
[ERROR] 2020-08-21 09:38:32.480 [http-nio-8080-exec-6] SqlExceptionHelper - ERROR: relation "pengguna" does not exist
Position: 467
1条答案
按热度按时间dddzy1tm1#
你能试试这个吗?
应用程序属性
创建配置bean
接下来,创建jdbctemplatebean,我们将使用它访问数据访问层中的数据源。
尝试访问以下属性: