Skip to main content
 首页 » 编程设计

Spring MVC : PUT and DELETE with a composite key

2024年06月20日29lonelyxmas

我正在使用 Spring Boot 创建一个 Web 应用程序,该应用程序使用 JPA 持久保存 FooBar 实体。

我有一些 html 页面在我的 Controller 上执行 AJAX 请求。 请求与此实体有关:

@Entity 
@Table(name = "FOO_TABLE") 
public class FooBar { 
 
  @EmbeddedId 
  private FooBarId id; 
 
  @Column(name = "ADDRESS") 
  private String address; 
 
  @Column(name = "COLOR") 
  private String color;   
} 

它使用复合键:

@Embeddable 
public class FooBarId { 
 
  @NotNull 
  @Column(name = "NAME") 
  private String name; 
 
  @NotNull 
  @Column(name = "TXT_ADR_MAIL") 
  private String email; 
} 

POST 没问题:

@PostMapping 
public ResponseEntity<Void> postFoobar(FooBar fb){ 
  repo.save(fb) 
  return new ResponseEntity<>(HttpStatus.CREATED); 
} 

问题:

如何执行 PUTGETDELETE ?我不知道如何做到这一点,因为我习惯于处理简单的 id 。 那么是否可以使用组合键执行这些操作?

编辑1:

  • 我的表格中没有 id 列。我无法更改表格。

  • 到目前为止我尝试过的操作:对于DELETE,我将整个实体传递给 Controller ​​,然后根据键搜索要删除的实体。 对于PUTGET(单次获取),我不知道从哪里开始。

问候。

请您参考如下方法:

假设您将 DTO 和实体分开。因此,在 PUT/DELETE 中,只需将正文放入您的请求即可。对于 GET,您可以尝试 POST 来获取数据,因为您的 id 与电子邮件和姓名相当复杂。因此将该 id 放入 Body 并执行 Post 来查询数据。这是我尝试过的:

--FooBarDTO

public class FooBarDTO { 
    private String name; 
    private String email; 
    private String address; 
    private String color; 
 
    public FooBarDTO(){} 
    /** 
     * @param name 
     * @param email 
     * @param id 
     * @param address 
     * @param color 
     */ 
    public FooBarDTO(String name, String email, String address, String color) { 
        this.name = name; 
        this.email = email; 
        this.address = address; 
        this.color = color; 
    } 
    /** 
     * @return the name 
     */ 
    public String getName() { 
        return name; 
    } 
    /** 
     * @param name the name to set 
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 
    /** 
     * @return the email 
     */ 
    public String getEmail() { 
        return email; 
    } 
    /** 
     * @param email the email to set 
     */ 
    public void setEmail(String email) { 
        this.email = email; 
    } 
    /** 
     * @return the address 
     */ 
    public String getAddress() { 
        return address; 
    } 
    /** 
     * @param address the address to set 
     */ 
    public void setAddress(String address) { 
        this.address = address; 
    } 
    /** 
     * @return the color 
     */ 
    public String getColor() { 
        return color; 
    } 
    /** 
     * @param color the color to set 
     */ 
    public void setColor(String color) { 
        this.color = color; 
    } 
 
 
} 

--FooBarIdDTO

public class FooBarIdDTO { 
    private String name; 
    private String email; 
    /** 
     * @return the name 
     */ 
    public String getName() { 
        return name; 
    } 
    /** 
     * @param name the name to set 
     */ 
    public void setName(String name) { 
        this.name = name; 
    } 
    /** 
     * @return the email 
     */ 
    public String getEmail() { 
        return email; 
    } 
    /** 
     * @param email the email to set 
     */ 
    public void setEmail(String email) { 
        this.email = email; 
    } 
 
} 

--- 在 Controller 中添加这些方法:

@Autowired 
    private FooBarRepository repo; 
 
    // test FooBar 
    @RequestMapping(value = "/foo", method = RequestMethod.POST) 
    public ResponseEntity<?> postFoo(@RequestBody FooBarDTO body){ 
        FooBarId id = new FooBarId(); 
        id.setEmail(body.getEmail()); 
        id.setName(body.getName()); 
 
        FooBar fooBar = new FooBar(); 
        fooBar.setId(id); 
        fooBar.setAddress(body.getAddress()); 
        fooBar.setColor(body.getColor()); 
        repo.save(fooBar); 
        return new ResponseEntity<>(HttpStatus.OK); 
    } 
 
    //test PUT 
    @RequestMapping(value = "/foo", method = RequestMethod.PUT) 
    public ResponseEntity<?> putFoo(@RequestBody FooBarDTO body){ 
        FooBarId id = new FooBarId(); 
        id.setEmail(body.getEmail()); 
        id.setName(body.getName()); 
 
        FooBar fooBar = new FooBar(); 
        fooBar.setId(id); 
        fooBar.setAddress(body.getAddress()); 
        fooBar.setColor(body.getColor()); 
        repo.save(fooBar); 
        return new ResponseEntity<>(HttpStatus.OK); 
    } 
 
    //test Delete FooBar 
    @RequestMapping(value = "/foo", method = RequestMethod.DELETE) 
    public ResponseEntity<?> deleteFoo(@RequestBody FooBarIdDTO body){ 
        FooBarId id = new FooBarId(); 
        id.setEmail(body.getEmail()); 
        id.setName(body.getName()); 
        repo.delete(id); 
        return new ResponseEntity<>(HttpStatus.OK); 
    } 
 
    // test FooBar 
        @RequestMapping(value = "/getFoo", method = RequestMethod.POST) 
        public ResponseEntity<?> getFoo(@RequestBody FooBarIdDTO body){ 
            FooBarId id = new FooBarId(); 
            id.setEmail(body.getEmail()); 
            id.setName(body.getName()); 
            FooBar result = repo.findOne(id); 
            return ResponseEntity.ok(result); 
        } 

结果如图