Java 如何使用@patch使用jpa存储库更新单个字段

Java 如何使用@patch使用jpa存储库更新单个字段,java,spring,spring-boot,jpa,patch,Java,Spring,Spring Boot,Jpa,Patch,我正在用spring boot开发一个简单的应用程序。我需要限制用户只能更新名称,而不是所有与用户数据相关的字段,但不幸的是,我的代码存在一个问题,即如果有人发送Json格式的数据并更改年龄或任何其他字段,它将被更新,但正如我告诉我的,我需要用户能够更改唯一的名称,而不是任何其他字段。我必须提到我正在使用JPA存储库和spring数据 我的控制器 RestController @RequestMapping("/student") public class StudentCo

我正在用spring boot开发一个简单的应用程序。我需要限制用户只能更新名称,而不是所有与用户数据相关的字段,但不幸的是,我的代码存在一个问题,即如果有人发送Json格式的数据并更改年龄或任何其他字段,它将被更新,但正如我告诉我的,我需要用户能够更改唯一的名称,而不是任何其他字段。我必须提到我正在使用JPA存储库和spring数据

我的控制器

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存储库


1.未来的最佳解决方案: 将Dto层添加到应用程序中,然后映射到对象。请参见下面的示例:

public class StudentDto {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}
然后可以通过mapstruct将其映射到模型

@Mapper
public abstract class StudentMapper {
public static final StudentMapper INSTANCE =
  Mappers.getMapper(StudentMapper.class);
      
    @Mapping
    Student studentDtoToStudent(StudentDto studentDto); 
}
这是指向mapstruct的链接:

要使用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 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}
    
    public ResponseEntity partialUpdateName(
    @RequestBody学生partialUpdate,@PathVariable(“id”)字符串id){
    可选选项student=repository.findById(id);
    if(optionalStudent.isPresent()&&partialUpdate!=null){
    学生当前=可选。get();
    current.setName(partialUpdate.getName());
    返回ResponseEntity.ok(repository.save(当前));
    }  
    //返回一个错误}
    

  • @Mapper很好,但我在映射上有一个错误。不知道为什么?我用一个快速的解决方案更新了我的答案。你可以看看哪一个更适合你。如果您使用第二个选项不添加模型,只需更改您的代码以添加我在快速解决方案部分中提到的代码片段,但如果您正在为生产构建代码,我建议使用第一个选项。谢谢您的回答。这是一个错误“@Mapping”不适用于映射注释的方法。请添加我在对pom.xml的回答中提到的两个依赖项。我在我的Gradle上添加了依赖项,但仍然存在类似“target”这样的错误
     <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)
    
     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}