问题描述
我是 Spring Boot 的新手。
我尝试使用 Spring Boot 作为后端开发一个简单的 CRUD 应用程序。
我对 JPA 存储库中的方法有一些问题。
只有一种方法有效,其他方法返回所有数据,就像 findAll()
一样。
当我尝试 http://localhost:8080/api/professeurs?nom=CHRAYAH
时,它可以工作并返回正确的行。
但是当我尝试 http://localhost:8080/api/professeurs?prenom=Yassine
时,它返回所有行。
因此,只有 profRepository.findByNom()
有效,但 profRepository.findByPrenom()
和其他方法无效。
这是我的代码
ProfRepository
package com.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.model.Professeur;
@Repository
public interface ProfRepository extends JpaRepository<Professeur,Long> {
List<Professeur> findByNom(String name);
@Query("SELECT p FROM Prof p where p.prenom=?1")
List<Professeur> findByPrenom(String prenom);
List<Professeur> findByEmail(String email);
List<Professeur> findByNomContaining(String name);
}
Professeur.java(模型)
package com.model;
import javax.persistence.*;
@Entity(name="Prof")
@Table(name = "professeurs")
public class Professeur {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "nom" )
private String nom;
@Column(name = "prenom")
private String prenom;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "cours")
private String cours;
public Professeur() {
}
public Professeur(String nom,String prenom,String email,String password,String cours) {
this.nom = nom;
this.prenom= prenom;
this.email = email;
this.password = password;
this.cours = cours;
}
public long getId() {
return id;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getpassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCours() {
return this.cours;
}
public void setCours(String cours) {
this.cours = cours;
}
@Override
public String toString() {
return "Professeur [id=" + id + ",nom=" + nom+ ",prenom=" + prenom + ",cours=" + cours + ",email=" + email + "]";
}
}
ProfesseurController
package com.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.model.Professeur;
import com.repository.AppRepository;
import com.repository.ProfRepository;
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class ProfesseurController {
@Autowired
ProfRepository profRepository;
@GetMapping("/professeurs")
public ResponseEntity<List<Professeur>> getAllProfs(@RequestParam(required = false) String nom) {
try {
List<Professeur> professeurs = new ArrayList<Professeur>();
if (nom == null)
profRepository.findAll().forEach(professeurs::add);
else
profRepository.findByNomContaining(nom).forEach(professeurs::add);
if (professeurs.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(professeurs,HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/professeurs/{id}")
public ResponseEntity<Professeur> getProfById(@PathVariable("id") long id) {
Optional<Professeur> profData = profRepository.findById(id);
if (profData.isPresent()) {
return new ResponseEntity<>(profData.get(),HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/professeurs")
public ResponseEntity<Professeur> createProf(@RequestBody Professeur professeur) {
try {
Professeur _professeur = profRepository
.save(new Professeur (professeur.getNom(),professeur.getPrenom(),professeur.getEmail(),professeur.getpassword(),professeur.getCours()));
return new ResponseEntity<>(_professeur,HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/professeurs/{id}")
public ResponseEntity<Professeur> updateProf(@PathVariable("id") long id,@RequestBody Professeur professeur) {
Optional<Professeur> profData = profRepository.findById(id);
if (profData.isPresent()) {
Professeur _professeur = profData.get();
_professeur.setNom(professeur.getNom());
_professeur.setPrenom(professeur.getPrenom());
_professeur.setEmail(professeur.getEmail());
_professeur.setPassword(professeur.getpassword());
_professeur.setCours(professeur.getCours());
return new ResponseEntity<>(profRepository.save(_professeur),HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/professeurs/{id}")
public ResponseEntity<HttpStatus> deleteProf(@PathVariable("id") long id) {
try {
profRepository.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/professeurs")
public ResponseEntity<HttpStatus> deleteallEleves() {
try {
profRepository.deleteall();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/professeurs/nom")
public ResponseEntity<List<Professeur>> findByNom(@PathVariable("nom") String nom) {
try {
List<Professeur> professeurs = profRepository.findByNom(nom);
if (professeurs .isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(professeurs,HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/professeurs/email")
public ResponseEntity<List<Professeur>> findByEmail(@PathVariable("email") String email) {
try {
List<Professeur> professeurs = profRepository.findByEmail(email);
if (professeurs .isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(professeurs,HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/professeurs/prenom")
public ResponseEntity<List<Professeur>> findByPrenom(@PathVariable("prenom") String prenom) {
try {
List<Professeur> professeurs = profRepository.findByPrenom(prenom);
if (professeurs .isEmpty()) {
System.out.println("pas de prof avec ce nom");
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(professeurs,HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
解决方法
我刚刚发现我应该将 @GetMapping("/professeurs/prenom")
改为 @GetMapping("/professeurs/prenom/{prenom}")