繁体   English   中英

查询创建Spring Data JPA

[英]Query Creation Spring Data JPA

我有一堆汽车用品。 我能够呈现所有Car Objects的列表,并且每个字段都显示在list.html 如果我只想在list.html上显示make值列表,该如何使用查询来解决? 查看Spring Data JPA Docs第5.3.2节,我可以做类似findByAllMake();事情findByAllMake();

我尝试过findByAllMake(); ...

以下代码显示了如何成功获取所有对象的完整列表。 希望仅获取所有make的列表。

汽车.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;
    }
}

CarController.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(); 但是我又得到了一个不令人满意的依赖错误。

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列表,则其返回类型应为List<String> ,而不是List<Car>

查询需要选择品牌,而不是汽车:

select distinct car.make from Car car

还没有在我的笔记本电脑上运行它,但是如果您尝试执行以下操作:向CarController添加构造函数:

public CarController(CarRepository carRepository){
this.carRepository = carRepository;
}

然后可能只是尝试:

public List<Car> findAll();

在仓库中?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM