繁体   English   中英

Spring 引导应用程序上无数据显示

[英]No data displaying on Spring Boot application

我正在尝试构建一个连接到 MySQL 的 Java/Spring Boot 应用程序。 该应用程序编译良好,我没有遇到任何错误。 当我 go 到 localhost:8080/employees 时,网页应该显示来自 sql 文件的员工 ID (eid)、员工姓名 (name) 和员工薪水 (salary),该文件已通过终端导入到 Z632A004B47591DCA 文件中。 当我 go 到 localhost:8080/employees 时,网页显示“[]”。

存储库 class

package com.example.demo.repositories;

import java.util.List;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.models.Employee;

public interface EmployeeRespositoryImpl extends CrudRepository <Employee, Integer> {
    
    public List<Employee>findByEidAndNameAndSalary(String eid, String name, Double salary);

}

服务 Class

package com.example.demo.services;

import java.util.List;

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

import com.example.demo.models.Employee;
import com.example.demo.repositories.EmployeeRespositoryImpl;

@Service
public class EmployeeService {
    
    @Autowired
    EmployeeRespositoryImpl er;
    
    public List<Employee> allEmployees(String eid, String name, Double salary){
        return er.findByEidAndNameAndSalary(eid, name, salary);
    }

}

Controller Class

package com.example.demo.controllers;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.models.Employee;
import com.example.demo.services.EmployeeService;
@RestController
public class EmployeeControllers {
    
    @Autowired
    EmployeeService es;
    
    @GetMapping(path="/employees")
    public List<Employee> getEmployees(String eid, String name, Double salary) {
        return es.allEmployees(eid, name, salary);
    }
    
}

//型号Class

package com.example.demo.models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;


@Entity
public class Employee {
    
    @Id     
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(unique = true)
    private String eid;
    private String name;
    private double salary;
    @ManyToOne
    @JoinColumn
    private Department dept;
    @ManyToMany
    private List<Project> projects;
    public String getEid() {
        return eid;
    }
    public void setEid(String eid) {
        this.eid = eid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public Department getDept() {
        return dept;
    }
    public void setDept(Department dept) {
        this.dept = dept;
    }
    public List<Project> getProjects() {
        return projects;
    }
    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }

}

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/import?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Adal135792!
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.initialization-mode=always

logging.level.org.hibernate.SQL=off
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

server.error.include-message=always
server.error.include-binding-errors=always

SQL文件

INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (1, "E001", "Thomas", 55000, 1);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (2, "E002", "Mary", 59000, 1);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (3, "E003", "John", 72401, 3);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (4, "E004", "Sean", 65432, 3);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (5, "E005", "Ann", 62003, 3);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (6, "E006", "Alan", 47333, 2);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (7, "E007", "Sara", 55323, 4);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (8, "E008", "Bill", 52353, 4);
INSERT INTO employee (id, eid, name, salary, dept_id) VALUES (9, "E009", "Rob", 57000, 5);

根据您的端点“/employees”,您需要返回所有员工。 那么,为什么您不使用存储库中的.findAll()方法,而不是使用 3 个不同参数寻找员工呢?

暂无
暂无

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

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