Spring Data 无法为方法创建查询

Kou*_*k J 3 java spring hibernate spring-mvc spring-boot

错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“appController”的bean时出错:通过字段“service”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'jenkinsService' 的 bean 时出错:通过字段 'repo' 表达的不满意的依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jenkinsRepo”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:无法为方法公共抽象 java.util.List com.example.crud.JenkinsRepo.findByrun_id(java.lang.String) 创建查询!没有找到 Jenkins 类型的属性运行!

这是用于定义表实体的 Jenkins 类:

package com.example.crud;

import javax.persistence.*;

@Entity
@Table(name="test_case_failure")
public class Jenkins {

    @Id
    @Column(name = "failure_id")
    private int failure_id;

    @Column(name="test_case_name")
    private String test_case_name;
    @Column(name="expected_value")
    private String expected_value;
    @Column(name="error_name")
    private String error_name;
    @Column(name="auto_error_type")
    private String auto_error_type;
    @Column(name="run_id")
    private String run_id;

    public Jenkins() {
    }

    public int getFailure_id() {
        return failure_id;
    }

    public void setFailure_id(int failure_id) {
        this.failure_id = failure_id;
    }

    public String getTest_case_name() {
        return test_case_name;
    }

    public void setTest_case_name(String test_case_name) {
        this.test_case_name = test_case_name;
    }

    public String getExpected_value() {
        return expected_value;
    }

    public void setExpected_value(String expected_value) {
        this.expected_value = expected_value;
    }

    public String getError_name() {
        return error_name;
    }

    public void setError_name(String error_name) {
        this.error_name = error_name;
    }

    public String getAuto_error_type() {
        return auto_error_type;
    }

    public void setAuto_error_type(String auto_error_type) {
        this.auto_error_type = auto_error_type;
    }

    public String getRun_id() {
        return run_id;
    }

    public void setRun_id(String run_id) {
        this.run_id = run_id;
    }

}
Run Code Online (Sandbox Code Playgroud)

控制器类:

package com.example.crud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class appController {

    @Autowired
    private jenkinsService service;

    @RequestMapping("/")
    public String viewHomePage(Model model) {
        List<Jenkins> listProducts = service.getbyrun_id();
        model.addAttribute("TestsReports", listProducts);

        return "index";
    }

}
Run Code Online (Sandbox Code Playgroud)

存储库类:

package com.example.crud;

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

import java.util.List;

@Repository
public interface JenkinsRepo extends JpaRepository<Jenkins, Integer> {
    List<Jenkins> findByrun_id(String run_id);
}
Run Code Online (Sandbox Code Playgroud)

服务等级:

package com.example.crud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class jenkinsService {

    @Autowired
    private JenkinsRepo repo;

    List<Jenkins> getbyrun_id() {
        return repo.findByrun_id("test");
    }

}
Run Code Online (Sandbox Code Playgroud)

rob*_*tts 10

当 spring-data 尝试从方法签名注入查询时,它使用下划线作为嵌套字段的分隔符。因此,如果您这样做,findByrun_idSpring 将搜索嵌套字段 Jenkins.run.id。您应该将属性更改run_idrunId,然后将您的方法重命名为findByrunIdfindByRunId