gpt4 book ai didi

java - 表名是什么?

转载 作者:行者123 更新时间:2023-12-02 11:20:27 25 4
gpt4 key购买 nike

我正在使用 H2 数据库创建 Spring Boot 应用程序。我不断收到以下错误:

Table "THINGS_TO_DO" not found; SQL statement:
insert into things_to_do (id, name, verified) values (1, 'TestUser1', 1) [42102-197]

而且,我觉得这是合乎逻辑的,因为我不知道在应用程序中在哪里传递这个表名称。另外,表名称应该是什么 - 表必须有一些特定的名称吗?

My ThingsToDo.java 如下所示:

package me.hiboy.springboot.microservice.example.todo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="things_to_do")
public class ThingsToDo {

@Id
private Long id;

@Column(name="name")
private String name;

@Column(name="verified")
private int verificationStatus;

private String task;

public ThingsToDo() {

}

public ThingsToDo(Long id, String name, int verificationStatus, String task) {
super();
this.id=id;
this.name=name;
this.verificationStatus=verificationStatus;
this.task=task;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public int getVerificationStatus() {
return verificationStatus;
}

public String getTask() {
return task;
}
}

Controller ThingsToDoController.java如下:

package me.hiboy.springboot.microservice.example.todo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ThingsToDoController {

@Autowired
ThingsToDoRepository repository;

@GetMapping("/")
public String index() {
return "Hello from the ToDo Controller\n";
}

@GetMapping("/todo/{name}")
public ThingsToDo getThingsToDo(@PathVariable String name) {
ThingsToDo thingToDo=repository.findByName(name);

return thingToDo;
}
}

存储库 ThingsToDoRepository 是:

package me.hiboy.springboot.microservice.example.todo;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ThingsToDoRepository extends JpaRepository<ThingsToDo, Long> {
ThingsToDo findByName(String name);
}

Application.properties 是:

spring.application.name=todo-service
server.port=8080
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.platform=h2
spring.datasource.initialize=true

data.sql 是:

insert into things_to_do (id, name, verified) values (1, 'TestUser1', 1);
insert into things_to_do (id, name, verified) values (2, 'TestUser2', 0);

我认为 pom.xml 不是必需的 - 如果是的话,请让我知道,我也会发布它。谢谢。

编辑:带有 main() 方法的那个在这里:

package me.hiboy.springboot.microservice.example.todo.springbootmicroservicetodoservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootMicroserviceTodoServiceApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootMicroserviceTodoServiceApplication.class, args);
}
}

编辑:到目前为止给出的所有答案都没有任何帮助。

最佳答案

按照包名称作为如果您的主类位于 com.example 包中那么你的应用程序中的所有包都将遵循这个包对于新实体,包将是com.example.entity

关于java - 表名是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com