簡體   English   中英

如何使用 jpa 存儲庫使用 @patch 更新單個字段

[英]How to update a single field using jpa repository using @patch

我正在通過 spring 引導開發一個簡單的應用程序。 我需要限制用戶只能更新名稱,而不是所有與用戶數據相關的文件,但不幸的是,我的代碼有一個問題,如果有人以 Json 格式發送數據並更改年齡或任何其他字段將被更新,但正如我所說,我需要用戶能夠更改唯一的名稱,而不是任何其他字段。 我不得不提到我正在使用 JPA 存儲庫和 spring 數據

我的 controller

RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentRepository repository;
// method i user to only update the name field
@PatchMapping("/pattt/{id}")

    public ResponseEntity partialUpdateName(
            @RequestBody Student partialUpdate, @PathVariable("id") String id) {

        Student.sav(partialUpdate, id);
        return ResponseEntity.ok(repository.save(partialUpdate));

JPA 存儲庫

@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
}

學生class

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

    @Id
    @GeneratedValue
    private int id;
    private String name;
    private int age;
    private String emailAddress;
    public Student() {  }
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public Student(int id, String name, int age, String emailAddress) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.emailAddress = emailAddress;
    }
    public static void sav(Student partialUpdate, String id) {
        partialUpdate.setName(id);
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
}

1.未來的最佳解決方案:將 Dto 層添加到您的應用程序,然后映射到您的 object。 請參見下面的示例:

public class StudentDto {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

然后你可以 map 這個到你的 model 通過 mapstruct

@Mapper
public abstract class StudentMapper {
public static final StudentMapper INSTANCE =
  Mappers.getMapper(StudentMapper.class);
      
    @Mapping
    Student studentDtoToStudent(StudentDto studentDto); 
}

這是 mapstruct 的鏈接: https://mapstruct.org/

為了使用 mapstruct,您將需要兩個依賴項:

 <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>1.3.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.3.0.Final</version>
    </dependency>

您將能夠向外界隱藏您的內部結構。 在你的控制器中:

public ResponseEntity partialUpdateName(
            @RequestBody StudentDto partialUpdate, @PathVariable("id") String id){
Student student = StudentMapper.INSTANCE.studentDtoToStudent(partialUpdate)

最后一行會給你一個安全的學生 model 然后你可以保存

  1. 快速解決方案 在您的 controller

     public ResponseEntity partialUpdateName( @RequestBody Student partialUpdate, @PathVariable("id") String id){ Optional<Student> optionalStudent= repository.findById(id); if(optionalStudent.isPresent() && partialUpdate.=null){ Student current=optional;get(). current.setName(partialUpdate;getName()). return ResponseEntity.ok(repository;save(current)); } //return an error}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM