我如何在Spring Boot中在DatabaseConfig中设置属性

时间:2018-07-26 12:42:30

标签: database hibernate spring-boot

在将我的配置放入application.properties之前。但是现在它似乎什么也没做,所以我想我需要将它们设置为conf类。

所有示例和教程似乎只是对LocalContaierEntityManagerFactoryBean的setProperties进行了随意设置,但是这里没有这样的选项,或者我不知道如何...

 @Configuration
 @EnableTransactionManagement
 @EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactory",
    basePackages = "com.xxxx.xxxxx.database.local.repository"
 )
public class LocalDatabaseConfig {

@Bean(name = "localDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource localDataSource() {
    return DataSourceBuilder.create().build();

}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
        entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("localDataSource") DataSource localDataSource) {
    return builder.dataSource(localDataSource)
            .packages("com.xxxx.xxxxx.database.local.model")
            .persistenceUnit("local")
            .build();
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory")EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty(
            "hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    return properties;
}

}

我有方法AdditionalProperties,但无法弄清楚如何在此处的任何对象上设置setProperties。

2 个答案:

答案 0 :(得分:1)

结果证明LocalContainerEntityManagerFactoryBean将覆盖application.properties中的自动创建。

您可以通过将其插入配置中进行设置。

    @Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
        entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("localDataSource") DataSource localDataSource) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", "update");
    return builder.dataSource(localDataSource)
            .packages("com.evli.kickback.database.local.model")
            .persistenceUnit("local")
            .properties(properties)

答案 1 :(得分:0)

一种更干净的方法是将所有这些配置都放在application.properties文件中。 在[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.2.1:wsdl2java (generate-sources-asan) on project AsanImza: Provider for class javax.xml.parsers.DocumentBuilderFacto ry cannot be created: javax.xml.parsers.DocumentBuilderFactory: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found -> [Help 1] 中创建一个名为 application.properties 的文件。 Spring自动将其检测为配置文件并加载所有需要的属性。 例如-

Spring DATASOURCE(数据源自动配置和数据源属性)

/src/main/resources

对于多个数据源,请使用以下配置-

spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

对于多个数据源,可以使用带有前缀字符串的@ConfigurationProperties批注,以标识您的数据源。