我正在尝试使用 MySQL 数据库和 Hibernate 作为 JPA 创建一个简单的 Spring Boot Web 应用程序。我正在使用 Spring Boot 版本 2.0.3。
我希望自动创建表并自动填充初始数据。为了实现这一点,我做了以下事情: 1. POJO 标记为@Entity 2. spring.jpa.hibernate.ddl-auto=update in application.properties 3. data.sql 在资源文件夹下 4. spring.datasource。初始化模式=始终在 application.properties 中(因为我没有使用嵌入式 H2 数据库。我使用的是 MySQL 数据库)
结果:表已创建,data.sql 中的初始数据也已成功加载。但是,当我再次启动 Spring Boot 应用程序时,出现以下异常:
我的疑问是:为什么 ddl-auto=update 每次应用程序启动时都试图插入 data.sql 中存在的记录,而不是更新相同的记录?
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-03 12:40:49.597 ERROR 26552 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class …Run Code Online (Sandbox Code Playgroud)