我使用 H2 数据库和一些 JPA 查询创建了一个非常小而简单的 Spring Boot 应用程序。
我的控制器如下:
package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;
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 Controllern";
}
@GetMapping("/todo/{name}")
public ThingsToDo getThingsToDo(@PathVariable String name) {
ThingsToDo thingToDo=repository.findByNameIgnoreCaseContaining(name);
return thingToDo;
}
}
我的豆子如下:
package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;
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;
}
@Override
public String toString() {
return "ThingsToDo{" +
"id=" + id +
", name='" + name + ''' +
", verificationStatus='" + verificationStatus + ''' +
'}';
}
}
H2 数据库架构为:
create table things_to_do
(
id int,
name varchar(500),
verified boolean
);
我运行以将值插入数据库的查询是:
insert into things_to_do (id, name, verified) values (1, 'Hello', 1);
insert into things_to_do (id, name, verified) values (2, 'Bye', 0);
但是,当我执行 get to http://localhost:8080/todo/Hello
时,它不会向我返回任何值;而如果我只是 ping http://localhost:8080
,它确实会输出Hello from the ToDo Controller
。 有人可以指出我错过了什么步骤吗? 谢谢!
编辑:从日志中(不过我不太了解(:
select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
Hibernate: select thingstodo0_.id as id1_0_, thingstodo0_.name as name2_0_, thingstodo0_.task as task3_0_, thingstodo0_.verified as verified4_0_ from things_to_do thingstodo0_ where upper(thingstodo0_.name) like upper(?)
编辑:
ThingsToDoRepository如下:
package me.abhishek.springboot.microservice.example.todo.springbootmicroservicetodoservice;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ThingsToDoRepository extends JpaRepository<ThingsToDo, Long> {
ThingsToDo findByNameIgnoreCaseContaining(String name);
}
尝试将属性spring.jpa.hibernate.ddl-auto
设置为application.properties
文件中的update
:
spring.jpa.hibernate.ddl-auto=update
由于H2
是一个嵌入式数据库,因此 Spring 会自动将默认值 spring.jpa.hibernate.ddl-auto
设置为 create-drop
,这会覆盖您的schema.sql
和data.sql
。
Spring 引导会根据以下条件为您选择一个默认值 它是否认为您的数据库是嵌入式的。它默认为 如果未检测到架构管理器或全部未检测到架构管理器,则创建-删除 其他情况。
参考
此外,您应该在ThingsToDoRepository
类的顶部添加@Repository
。