Car*_*rla 4 spring spring-boot
我正在尝试针对在容器中运行的 MySQL 数据库为 Spring Boot 应用程序执行一些数据库初始化。在身份验证过程中,我收到错误“找不到表”。我检查了数据库,确实没有创建任何表。数据库属性中是否缺少某些内容?
spring.datasource.url = jdbc:mysql://172.17.0.2:3306/schema
spring.datasource.username = user
spring.datasource.password = password
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.data.rest.basePath=/
spring.datasource.data = classpath:/data.sql
spring.datasource.schema = classpath:/schema.sql
Run Code Online (Sandbox Code Playgroud)
总而言之,只要我从 mysql 命令行创建 DDL,JDBC 设置就可以正常工作。所以它只是在启动时不执行 data.sql 和 schema.sql 。我需要 mysql 的一些额外属性吗?
小智 5
您可以使用以下属性更新 application.properties。
应用程序属性:
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
spring.datasource.initialization-mode=always
Run Code Online (Sandbox Code Playgroud)
默认情况下spring.jpa.generate-ddl值为false。如果将spring.jpa.generate-ddl=true或spring.jpa.hibernate.ddl-auto设置为任何值validate、update、create、create-drop。Spring Boot 生成架构脚本并根据应用程序中可用的实体创建表。
现在,您应该在 resources 文件夹下创建schema.sql和data.sql文件,并在 application.properties 中设置属性spring.datasource.initialization-mode=always。
您还可以运行基于平台的脚本。假设您要运行hsqldb数据库的脚本,然后在 application.properties 文件中设置spring.datasource.platform=hsqldb,并在 resources 文件夹下创建脚本文件schema-hsqldb.sql和data-hsqldb.sql。您可以找到有关如何在 spring boot 启动时加载 schema.sql 和 data.sql 的更多详细信息。
| 归档时间: |
|
| 查看次数: |
12425 次 |
| 最近记录: |