Spring CRUD不将实体保存到数据库

aamkag61  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(163)

我的spring应用程序没有向连接的H2数据库添加任何数据。它是我的Patient实体:

  1. package medical.Model;
  2. import jakarta.persistence.*;
  3. import jakarta.validation.constraints.NotBlank;
  4. import jakarta.validation.constraints.NotNull;
  5. @Entity
  6. @Table(name = "PATIENTS")
  7. public class Patient {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.IDENTITY)
  10. private Long id;
  11. @Column(name = "name")
  12. @NotBlank(message = "Imię pacjenta jest wymagane")
  13. private String name;
  14. @Column(name = "surname")
  15. @NotBlank(message = "Nazwisko pacjenta jest wymagane")
  16. private String surname;
  17. @Column(name = "age")
  18. @NotNull(message = "Wiek pacjenta jest wymagany")
  19. private int age;
  20. @Column(name = "description")
  21. @NotBlank(message = "Opis zdrowotny pacjenta jest wymagany")
  22. private String description;
  23. public Patient(String name, String surname, int age, String description) {
  24. this.name = name;
  25. this.surname = surname;
  26. this.age = age;
  27. this.description = description;
  28. }
  29. public Patient() {
  30. }
  31. public Long getId() {
  32. return id;
  33. }
  34. public void setId(long id) {
  35. this.id = id;
  36. }
  37. public String getName() {
  38. return name;
  39. }
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43. public String getSurname() {
  44. return surname;
  45. }
  46. public void setSurname(String surname) {
  47. this.surname = surname;
  48. }
  49. public int getAge() {
  50. return age;
  51. }
  52. public void setAge(int age) {
  53. this.age = age;
  54. }
  55. public String getDescription() {
  56. return description;
  57. }
  58. public void setDescription(String description) {
  59. this.description = description;
  60. }
  61. @Override
  62. public String toString() {
  63. return "Patient{" +
  64. "id=" + id + name = '" + name + '\'' + surname = '" + surname + '\
  65. '' + age = " + age + description='" + description + '\'' +
  66. '}';
  67. }
  68. }

字符串
我的控制器:

  1. package medical.Controller;
  2. import medical.Model.Patient;
  3. import jakarta.validation.Valid;
  4. import medical.Service.UserManageService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.validation.BindingResult;
  8. import org.springframework.web.bind.annotation.*;
  9. import org.springframework.ui.Model;
  10. import java.util.List;
  11. import java.util.Optional;
  12. import medical.exception.RecordNotFoundException;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. @Controller
  16. @RequestMapping("/")
  17. public class UserManageController {
  18. @Autowired
  19. UserManageService service;
  20. @RequestMapping
  21. public String getAllEmployees(Model model) {
  22. System.out.println("getAllEmployees");
  23. List<Patient> list = service.getAllPatients();
  24. model.addAttribute("employees", list);
  25. return "list-employees";
  26. }
  27. @RequestMapping(path = {"/edit", "/edit/{id}"})
  28. public String editPatientById(Model model, @PathVariable("id") Optional<Long> id) throws RecordNotFoundException {
  29. System.out.println("editPatientById" + id);
  30. if (id.isPresent()) {
  31. Patient patient = service.getPatientById(id.get());
  32. model.addAttribute("patient", patient); else{
  33. model.addAttribute("patient", new Patient());
  34. }
  35. return "add-edit-patient";
  36. }
  37. @RequestMapping(path = "/delete/{id}") public String deleteEmployeeById (Model model, @PathVariable("id") Long
  38. id) throws RecordNotFoundException
  39. {
  40. System.out.println("deleteEmployeeById" + id);
  41. service.deletePatientById(id);
  42. return "redirect:/";
  43. }
  44. @PostMapping("/createOrUpdatePatient") public String createOrUpdatePatient (@Valid Patient
  45. patient, BindingResult bindingResult, Model model){
  46. if (bindingResult.hasErrors()) {
  47. In
  48. case of validation
  49. errors,return to the form with errors return "add-edit-patient";
  50. } System.out.println("Create or update patient");
  51. service.createOrUpdatePatient(patient);
  52. return "redirect:/list-employees";
  53. }
  54. }


服务

  1. package medical.Service;
  2. import medical.Model.Patient;
  3. import medical.Repository.UserManageRepo;
  4. import medical.exception.RecordNotFoundException;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Optional;
  10. @Service
  11. public class UserManageService {
  12. @Autowired
  13. UserManageRepo repository;
  14. public List<Patient> getAllPatients() {
  15. System.out.println("getAllPatients");
  16. List<Patient> result = (List<Patient>) repository.findAll();
  17. if (result.size() > 0) {
  18. return result; else{
  19. return new ArrayList<>();
  20. }
  21. }
  22. public Patient getPatientById (Long id) throws RecordNotFoundException {
  23. System.out.println("getPatientById");
  24. Optional<Patient> patient = repository.findById(id);
  25. if (patient.isPresent()) {
  26. return patient.get(); else{
  27. throw new RecordNotFoundException("No patient record exist for given id");
  28. }
  29. }
  30. public Patient createOrUpdatePatient (Patient patient){
  31. System.out.println("createOrUpdatePatient");
  32. Create new entry if (patient.getId() == null) {
  33. patient = repository.save(patient);
  34. return patient; else{
  35. update existing entry Optional<Patient > patientOptional = repository.findById(patient.getId());
  36. if (patientOptional.isPresent()) {
  37. Patient existingPatient = patientOptional.get();
  38. existingPatient.setName(patient.getName());
  39. existingPatient.setSurname(patient.getSurname());
  40. existingPatient.setAge(patient.getAge());
  41. existingPatient.setDescription(patient.getDescription());
  42. existingPatient = repository.save(existingPatient);
  43. return existingPatient; else{
  44. patient = repository.save(patient);
  45. return patient;
  46. }
  47. }
  48. }
  49. public void deletePatientById (Long id) throws RecordNotFoundException {
  50. System.out.println("deletePatientById");
  51. Optional<Patient> patient = repository.findById(id);
  52. if (patient.isPresent()) {
  53. repository.deleteById(id); else{
  54. throw new RecordNotFoundException("No patient record exist for given id");
  55. }
  56. }
  57. }


最后是百里香叶的表达

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="x-ua-compatible" content="ie=edge">
  6. <title>Add Employee</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  9. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css"> </head>
  10. <body> Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
  11. <div class="container">
  12. <a class="navbar-brand" href="/">Springboot H2 DB @OKAYCOMPUTING</a>
  13. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
  14. <span class="navbar-toggler-icon"></span>
  15. </button>
  16. <div class="collapse navbar-collapse" id="navbarResponsive">
  17. <ul class="navbar-nav ml-auto">
  18. <li class="nav-item active">
  19. <a class="nav-link" href="#">Home
  20. <span class="sr-only">(current)</span>
  21. </a>
  22. </li>
  23. <li class="nav-item">
  24. <a class="nav-link" href="#">About</a>
  25. </li>
  26. <li class="nav-item">
  27. <a class="nav-link" href="#">Services</a>
  28. </li>
  29. <li class="nav-item">
  30. <a class="nav-link" href="#">Contact</a>
  31. </li>
  32. </ul>
  33. </div>
  34. </div> </nav>
  35. <div class="container my-5" style="padding-top: 30px;">
  36. <h3> Add Employee</h3>
  37. <div class="card">
  38. <div class="card-body">
  39. <div class="col-md-10">
  40. <!--/*@thymesVar id="patient" type="medical.Model.Patient"*/-->
  41. <form th:action="@{/createOrUpdatePatient}" th:object="${patient}" method="post">
  42. <div class="row">
  43. <div class="form-group col-md-8">
  44. <label for="name" class="col-form-label">Name</label>
  45. <input type="text" th:field="*{name}" class="form-control" id="name" placeholder="name" />
  46. </div>
  47. <div class="form-group col-md-8">
  48. <label for="surname" class="col-form-label">Surname</label>
  49. <input type="text" th:field="*{surname}" class="form-control" id="surname" placeholder="surname" />
  50. </div>
  51. <div class="form-group col-md-8">
  52. <label for="age" class="col-form-label">Age</label>
  53. <input type="number" th:field="*{age}" class="form-control" id="age" placeholder="Age" />
  54. </div>
  55. <div class="form-group col-md-8">
  56. <label for="description" class="col-form-label">Description</label>
  57. <input type="text" th:field="*{description}" class="form-control" id="description" placeholder="Description" />
  58. </div>
  59. <div class="col-md-6">
  60. <input type="submit" class="btn btn-primary" value=" Submit ">
  61. </div>
  62. <input type="hidden" id="id" th:field="*{id}">
  63. </div>
  64. </form>
  65. </div>
  66. </div>
  67. </div>
  68. </div> </body>
  69. </html>


正如我注意到的那样,患者控制器方法甚至没有记录Sout消息
我试图改变控制器和服务,但它没有帮助很多

carvr3hs

carvr3hs1#

我终于设法解决了这个问题。可能是因为这一行的原因,“ID”中存在冲突:

  1. <input type="hidden" id="id" th:if="${patient.id}" th:field="*{id}">

字符串

相关问题