【问题标题】:How do i set properties in DatabaseConfig in spring boot如何在 Spring Boot 中的 DatabaseConfig 中设置属性
【发布时间】:2019-01-03 11:11:45
【问题描述】:

在我在 application.properties 中进行配置之前。但现在它似乎什么也没做,所以我想我需要在 conf 类中设置它们。

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

 @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,但不知道如何在此处的任何对象上设置属性。

【问题讨论】:

    标签: database hibernate spring-boot


    【解决方案1】:

    更简洁的方法是将所有这些配置放在 application.properties 文件中。 在/src/main/resources 中创建一个名为application.properties 的文件。 Spring 自动将其检测为配置文件并加载所有需要的属性。 例如-

    Spring DATASOURCE(DataSourceAutoConfiguration 和 DataSourceProperties)

    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
    

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

    #first db
    spring.datasource.url = [url]
    spring.datasource.username = [username]
    spring.datasource.password = [password]
    spring.datasource.driverClassName = oracle.jdbc.OracleDriver
    
    #second db ...
    spring.secondDatasource.url = [url]
    spring.secondDatasource.username = [username]
    spring.secondDatasource.password = [password]
    spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
    
    
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.firstDatasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @ConfigurationProperties(prefix="spring.secondDatasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    

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

    【讨论】:

    • 我的配置似乎不适用于应用程序属性,因为我有两个数据源。我可以以某种方式使用前缀将此属性仅提供给一个来源吗?
    • 刚刚更新了我对多个数据源配置的回答。
    • 如果我只想在第二个数据源上使用 spring.jpa.hibernate.ddl-auto = update 怎么样?
    【解决方案2】:

    原来 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)
    

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 1970-01-01
      • 2020-08-21
      • 2020-12-24
      • 2017-10-28
      • 2019-03-04
      • 2017-01-08
      • 1970-01-01
      • 2021-07-17
      相关资源
      最近更新 更多