【发布时间】:2019-09-03 16:55:29
【问题描述】:
我有一堆汽车对象。我能够呈现所有汽车对象的列表,每个字段都显示在list.html 上。如果我只想在list.html 上显示make 值的列表,我将如何使用查询来解决这个问题?查看 Spring Data JPA Docs 第 5.3.2 节,我可以做类似findByAllMake(); 的事情吗?
我试过findByAllMake();...
以下代码显示了我如何成功获取所有对象的完整列表。只想获取所有makes 的列表。
汽车.java
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Car {
@Id
private String id;
private String make;
private String model;
private String year;
public Car() {
}
public Car(String id, String make, String model, String year) {
this.id = id;
this.make = make;
this.model = model;
this.year = year;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
汽车控制器.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class CarController {
@Autowired
private CarRepository carRepository;
@GetMapping("/list")
public String carMakeList(Model model){
model.addAttribute("list", carRepository.getAllMakes());
return "list";
}
}
CarRepository.java
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CarRepository extends JpaRepository<Car, String> {
@Query(value="SELECT * FROM car;", nativeQuery=true)
List<Car> getAllMakes();
}
我已经尝试过findByAllMake();,但我得到了一个 Unsatisfactory Dependency 错误。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carController': Unsatisfied dependency expressed through field 'carService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carService': Unsatisfied dependency expressed through field 'carRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.demo.CarRepository.findAllByMake()! No parameter available for part make SIMPLE_PROPERTY (1): [Is, Equals] NEVER.
【问题讨论】:
-
什么是“make”类型?是
Car吗?我对此表示怀疑。因此,您的查询不应返回 List。它也不应该使用 SQL,而应该使用 JPQL。品牌与汽车有什么关系?为了编写正确的查询,了解这一点很重要吗?贴出 Car 实体的代码。 -
刚刚发布了 Car 实体。
make是String。谢谢。 -
不...确定您为什么要在此处创建本机查询。您可以在没有本机查询的情况下使用
findAll;您遇到了一些 Spring 语法糖,其中生成您的方法的进程看到“By”并且因为您没有为该方法提供参数而吓坏了。 -
为什么
car.make是一个字符串?不是汽车品牌吗?如果是这样,为什么不把它也作为一个实体呢?
标签: java spring-boot jpa