我正在尝试使用Hibernate和SpringBoot进行简单的CRUD应用程序。我想让hibernate自动创建数据库并显示sql语句,因此我在application.properties中添加了一些属性。
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto = create-drop
但是它们似乎都是未使用的属性。我尝试创建配置类,还添加了@PropertySource("classpath:application.properties")
,但没有帮助。
这是我的配置类:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableJpaRepositories
public class DBConfiguration {
}
这是我的主班:
@SpringBootApplication
@Import({DBConfiguration.class})
@PropertySource("classpath:application.properties")
public class SpringboothibernateApplication {
public static void main(String[] args) {
SpringApplication.run(SpringboothibernateApplication.class, args);
}
}
我在做什么错了,为什么根本没有使用application.properties文件。
答案 0 :(得分:2)
删除DbConfiguration。将那些属性写入位于资源文件夹中的application.properties文件。然后在SpringBoothibernateApplication类上使用@EnableAutoConfiguration批注。