【发布时间】:2018-10-02 21:42:13
【问题描述】:
我使用 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 Controller\n";
}
@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);
但是,当我访问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);
}
【问题讨论】:
-
@TwiN,通过在线访问控制台。好吧,我确实有
schema.sql和data.sql但由于某种原因它们没有运行(我猜)。 -
@TwiN,我没有尝试过,因为无论如何我都无法得到任何结果!
-
@TwiN,您能否确认我的查询方式是否正确?我不确定何时调用构造函数以及该构造函数是否会按预期工作(因为我在表中没有
task列)。 -
@TwiN,没关系。谢谢你。 :)
标签: java spring-boot jpa h2