gpt4 book ai didi

java - 如何查找学生Bydate (findByDate)

转载 作者:行者123 更新时间:2023-12-01 22:47:35 25 4
gpt4 key购买 nike

我开发了一个应用程序来管理大学的学生,我正在寻找如何查找与输入的日期相关的学生,我开发了以下代码行:

1-模型student.java包 com.avatar.model;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.JoinColumn;

@Entity
@Table(name = "Students")
public class Student{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String nom;
private String prenom;
private String numTel;
private String mail;

@Temporal(TemporalType.DATE)
private Date dateCurrent;

@ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})

@JoinTable(name = "student_techno",
joinColumns = { @JoinColumn(name = "student_id") },
inverseJoinColumns = { @JoinColumn(name = "techno_id") })

private Set<Techno> techno = new HashSet<>();

public Student() {

}

@SuppressWarnings("unchecked")
public Student(String nom, String prenom,String numTel, String mail, Date dateCurrent,
) {
super();
this.nom = nom;
this.prenom = prenom;
this.numTel = numTel;
this.mail = mail;
this.dateCurrent = dateCurrent;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public String getPrenom() {
return prenom;
}

public void setPrenom(String prenom) {
this.prenom = prenom;
}

public String getNumTel() {
return numTel;
}

public void setNumTel(String numTel) {
this.numTel = numTel;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Date getdateCurrent() {
return dateCurrent;
}

public void setdateCurrent(Date dateCurrent) {
this.dateCurrent = dateCurrent;
}

@Override
public String toString() {
return "Student[nom=" + nom + ", prenom=" + prenom + ", numTel=" + numTel + ", mail="
+ mail + ", dateCurrent=" + dateCurrent+ "]";
}
}

2- Controller

package com.avatar.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.avatar.dao.StudentDao;
import com.avatar.model.Student;

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/avatar")
public class StudentController {

@Autowired
StudentDao studentdao;
@GetMapping(value = "/all-students")
public List<Student> listeDesStudent() {

List<Student> students= studentdao.findAll();

if (students.isEmpty())
throw new ProductNotFoundException("No student is registered in the database");

return students;
}

@GetMapping(value = "/all-students/dateCurrent/{dateCurrent}")
public List<Student> viewStudent(@PathVariable("dateCurrent") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateCurrent) {
Calendar c = Calendar.getInstance();
c.setTime(dateCurrent);
c.add(Calendar.DATE, 1);
dateCurrent = c.getTime();
return studentdao.findByDate(dateCurrent);
}

@GetMapping(value = "/all-students /techno/{nomTechno}")
List<Student > viewStudent(@PathVariable("nomTechno") String nomTechno) {

return studentdao.findDistinctByTechnoNomTechno(nomTechno);
}

@PostMapping(value = "/add-student")
public Student addStudent(@RequestBody Student student) {

Student studentAdded= studentdao.save(Student);
return studentAdded;

}
}

3-DAO

package com.avatar.dao;

import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.avatar.model.Student;

@Repository
public interface StudentDao extends JpaRepository<Student, String> {

List<Student> findByDate(Date dateCurrent);

List<> findDistinctByTechnoNomTechno(String nomTechno);

}

4-applications.properties

server.port= 8080

# MySQL Properties

spring.jpa.show-sql = true
spring.datasource.url= jdbc:mysql://localhost:3306/avatar?serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=*****
spring.datasource.password=*****

# Hibernate Properties
spring.jpa.hibernate.ddl-auto=update

在我的控制台中我有:

Failed to create query for method public abstract java.util.List com.avatar.dao.StudentDao.findByDate(java.util.Date)! No property date found for type Student!

最佳答案

请根据以下提到的更改更新 DAO如果字段名称为 dateCurrent,则 findByDateCurrent。

package com.avatar.dao;
import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.avatar.model.Student;

@Repository
public interface StudentDao extends JpaRepository<Student, String> {
List<Student> findByDateCurrent(Date dateCurrent);
List<> findDistinctByTechnoNomTechno(String nomTechno);
}

关于java - 如何查找学生Bydate (findByDate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58468254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com