出于法规和安全方面的原因,我不得不将Spring Boot应用程序的逻辑分为两个工具:一个用于管理表数量有限的工具,另一个用于“实际”用户应用程序。因此,我在服务器版本5.7上有两个MySQL数据库实例。虽然该用户工具只能访问一个带有几十个表的数据库,但管理工具需要访问两个数据库中的实体。
这些工具都是基于JavaFX和Spring Boot的。由于这种体系结构设置,我有了三个Maven软件包:一个用于管理工具和所有与管理相关的实体,服务等,一个用于用户工具以及所有相关的实体,服务等,仅与此用户工具相关,而第三个与这两个工具共享的所有实体。
当我运行用户工具时,它会在共享数据库中生成表,并根据其application.properties文件中的配置使用休眠的EnhancedNamingStrategy。因此,这些列在适当的地方带有下划线。
首先,管理工具根本不会使用spring.jpa.hibernate.ddl-auto创建任何数据库表,但是我不得不使用spring.jpa.generate-ddl。
现在,当我运行管理工具时,我希望它仅在管理数据库中创建表,因为此数据源被注释为@Primary。但是,它还会在大小写混合的情况下在用户数据库中创建列。因此,我有名为例如的列用户数据库中的“ email_address”和“ emailAddress”。
我想知道我的方法是否使用了任何属性?有任何想法如何正确执行吗?
请找到以下消息来源。
application.properties:
# 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);
}
}
答案 0 :(得分:0)
诀窍是使用配置类
@PropertySources({ @PropertySource("classpath:application.properties") })
注释。然后,在创建LocalContainerEntityManagerFactoryBean的方法中,您可以使用
拉出并设置在application.properties文件中定义的值。properties.put("spring.jpa.hibernate.naming.physical-strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));