java - 多数据库设置中未使用的属性
问题描述
出于监管和安全原因,我不得不将 Spring Boot 应用程序的逻辑拆分为两个工具:一个用于管理有限数量的表,另一个用于“真实”用户应用程序。因此,我在服务器版本 5.7 上有两个 MySQL 数据库实例。虽然用户工具只访问一个包含几十个表的数据库,但管理工具需要访问两个数据库中的实体。
这些工具都基于 JavaFX 和 Spring Boot。由于这种架构设置,我有三个 maven 包:一个用于管理工具和所有与管理相关的实体、服务等,一个用于用户工具和所有相关实体、服务等。仅与此用户工具相关,第三个与这两个工具共享的所有实体。
当我运行用户工具时,它会在共享数据库中生成表,并根据其 application.properties 文件中的配置使用休眠改进命名策略。因此,列在适当的地方有一个下划线。
首先,管理工具根本不会使用 spring.jpa.hibernate.ddl-auto 创建任何数据库表,但我必须使用 spring.jpa.generate-ddl。
现在,当我运行管理工具时,我希望它只在管理数据库中创建表,因为这个数据源被注释为@Primary。但它也会在用户数据库中创建混合大小写的列。因此,我在用户数据库中有名为“email_address”和“emailAddress”的列。
我想知道我的方法是否使用了任何属性?任何想法如何正确地做到这一点?
请找到以下一些来源..
应用程序属性:
# Employee database
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbcUrl=jdbc:mysql://127.0.0.1/agiletunesdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8
spring.datasource.username=YYY
spring.datasource.password=XXXXXX
# Account database
security.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
security.datasource.jdbcUrl=jdbc:mysql://127.0.0.1/authenticationdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8
security.datasource.username=YYY
security.datasource.password=XXXXXX
# create db schema, values are none, validate, update, create, and create-drop.
#spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=true
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
数据库配置:
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MultipleDbConfiguration {
/*
* Persistence of admin database
*/
@Bean(name = "securityDB")
@Primary
@ConfigurationProperties(prefix="security.datasource")
public DataSource securityDataSource() {
return DataSourceBuilder.create().build();
}
/*
*
* Persistence of user database
*/
@Bean(name = "organizationDB")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource organizationDataSource() {
return DataSourceBuilder.create().build();
}
}
用户数据库配置
import java.util.HashMap;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
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
@EnableJpaRepositories(
entityManagerFactoryRef = "organizationEntityManagerFactory",
transactionManagerRef = "organizationTransactionManager",
basePackages = "com.agiletunes.domain.organization"
)
@EnableTransactionManagement
@PropertySources({ @PropertySource("classpath:application.properties") })
public class OrganizationConfig {
@Autowired
private Environment env; // Contains Properties Load by @PropertySources
@Bean(name = "organizationEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean organizationEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("organizationDB") DataSource dataSource) {
HashMap<String, Object> properties = new HashMap<>();
properties.put("spring.jpa.properties.hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("spring.jpa.hibernate.ddl-auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
properties.put("spring.jpa.hibernate.naming-strategy", env.getProperty("spring.jpa.hibernate.naming-strategy"));
return builder
.dataSource(dataSource)
.packages("com.agiletunes.domain.organization")
.persistenceUnit("organizationPU")
.properties(properties)
.build();
}
@Bean(name="organizationTransactionManager")
public PlatformTransactionManager secondTransactionManager(@Qualifier("organizationEntityManagerFactory")
EntityManagerFactory organizationEntityManagerFactory) {
return new JpaTransactionManager(organizationEntityManagerFactory);
}
}
解决方案
诀窍是使用具有
@PropertySources({ @PropertySource("classpath:application.properties") })
注解。然后,在创建 LocalContainerEntityManagerFactoryBean 的方法中,您可以拉取和设置在 application.properties 文件中定义的值
properties.put("spring.jpa.hibernate.naming.physical-strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
推荐阅读
- java - Netbeans 无法导入 FileUtils 包?
- android - 点击屏幕,打开被点击的布局
- python - Python - pandas ExcelFile/read_excel - 数据导入在第一个空行处停止
- linux - 波浪号有什么不同“~/
“对~/” " 在 Shell 脚本中 - python - Pandas 数据框填充索引之间的值
- json - 如何在python中的大型JSON文件中产生字典
- c# - 按升序对日志(Serilog)输出进行排序?
- sql-server - SQL:如何获取两个字符串列之一,具体取决于是否填充
- python - 使用 numpy 进行尺寸变换
- javascript - 为什么在 fetch 调用中使用 no-cors?