【发布时间】:2021-10-06 14:47:50
【问题描述】:
阅读spring boot 5 in action,第3章->使用H2数据库,项目结构:
.
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── tacocloud
│ │ │ ├── controllers
│ │ │ │ ├── DesignTacoController.java
│ │ │ │ └── OrderController.java
│ │ │ ├── models
│ │ │ │ ├── Ingredient.java
│ │ │ │ ├── Order.java
│ │ │ │ └── Taco.java
│ │ │ ├── repositories
│ │ │ │ ├── interfaces
│ │ │ │ │ └── IngredientRepository.java
│ │ │ │ └── JdbcIngredientRepository.java
│ │ │ ├── TacoCloudApplication.java
│ │ │ └── WebConfig.java
│ │ └── resources
│ │ ├── application.properties
│ │ ├── data.sql
│ │ ├── schema.sql
│ │ ├── static
│ │ │ ├── images
│ │ │ │ └── TacoCloud.png
│ │ │ └── style.css
│ │ └── templates
│ │ ├── design.html
│ │ ├── home.html
│ │ └── orderForm.html
│ └── test
│ └── java
│ └── com
│ └── example
│ └── tacocloud
│ └── TacoCloudApplicationTests.java
└── taco-cloud.iml
当我用maven插件mvn spring-boot:run启动项目时:
2021-07-31 12:41:30.065 INFO 4910 --- [ main] c.e.tacocloud.TacoCloudApplication : Starting TacoCloudApplication using Java 11.0.11 on Shepherd with PID 4910 (/home/shepherd/Desktop/spring/taco-cloud/target/classes started by shepherd in /home/shepherd/Desktop/spring/taco-cloud)
2021-07-31 12:41:30.067 INFO 4910 --- [ main] c.e.tacocloud.TacoCloudApplication : No active profile set, falling back to default profiles: default
2021-07-31 12:41:31.195 INFO 4910 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-07-31 12:41:31.205 INFO 4910 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-07-31 12:41:31.206 INFO 4910 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-07-31 12:41:31.281 INFO 4910 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-07-31 12:41:31.281 INFO 4910 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1163 ms
2021-07-31 12:41:31.371 INFO 4910 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-07-31 12:41:31.474 INFO 4910 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-07-31 12:41:31.999 INFO 4910 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-31 12:41:32.013 INFO 4910 --- [ main] c.e.tacocloud.TacoCloudApplication : Started TacoCloudApplication in 2.413 seconds (JVM running for 2.812)
我可以看到没有 sql 执行,因此没有创建来自 main/resources/schema.sql 和 main/resources/data.sql 的初始表。 (即使 sql 文件根据文档位于正确的目录中)。
所以当我尝试从/design (main/java/com/example/tacocloud/controllers/DesignController.java) 获取此页面时:
@GetMapping
public String showDesignForm(Model model){
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepo.findAll().forEach(ingredients::add);
Type[] types = Ingredient.Type.values();
for(Type type: types){
model.addAttribute(type.toString().toLowerCase(),
filterByType(ingredients, type));
}
return "design";
}
调用存储库函数findAll()(main/java/com/example/tacocloud/repositories/JdbcIngredientRepository.java):
@Override
public Iterable<Ingredient> findAll() {
return jdbc.query("SELECT id, name, type FROM Ingredient",
this::mapRowToIngredient);
}
我收到 thymeleaf 错误,因为它无法呈现模板,因为所需的数据不可用,因为带有表的 schema.sql 未执行。如何让 spring 执行schema.sql 以使这些数据可用?
PS:schema.sql:
create table if not exists Ingredient (
id varchar(4) not null,
name varchar(25) not null,
type varchar(10) not null
);
create table if not exists Taco (
id identity,
name varchar(50) not null,
createdAt timestamp not null
);
create table if not exists Taco_Ingredients(
taco bigint not null,
ingredient varchar(4) not null
);
alter table Taco_Ingredients
add foreign key (taco) references Taco(id);
alter table Taco_Ingredients
add foreign key (ingredient) references Ingredient(id);
create table if not exists Taco_Order (
id identity,
deliveryName varchar(50) not null,
deliveryStreet varchar(50) not null,
deliveryCity varchar(50) not null,
deliveryState varchar(2) not null,
deliveryZip varchar(10) not null,
ccNumber varchar(16) not null,
ccExpiration varchar(5) not null,
ccCVV varchar(3) not null,
placedAt timestamp not null
);
create table if not exists Taco_Order_Tacos(
tacoOrder bigint not null,
taco bigint not null
);
alter table Taco_Order_Tacos
add foreign key (tacoOrder) references Taco_Order(id);
alter table Taco_Order_Tacos
add foreign key (taco) references Taco(id);
application.properties:
spring.thymeleaf.cache=false
spring.sql.init.mode=always`
PSS: git 回购: https://github.com/tacocloud/taco-cloud
【问题讨论】:
-
请包含您的
application.properties文件的内容。 -
请在 github 或类似网站上创建一个完整的工作项目,以便有人可以查看它...
-
@khmarbaise 查看编辑
标签: java spring spring-boot maven h2