【问题标题】:Getters and setters for List in Spring bootSpring Boot 中 List 的 getter 和 setter
【发布时间】:2021-03-24 14:53:41
【问题描述】:

我正在 Spring Boot 中创建我的 API。 我有一个产品是由组件组成的:

这是我的代码: (实体层)

import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;

@Table
@Entity
public class Product{
   @Id @GeneratedValue
   private Long id;
   private String name;
   @OneToMany
   @JoinColumn
   private List<Component> productComponents = new ArrayList<>();

   //default constructor
   public Product(){

   }
   
   public Product(Long id, String name, List<Component> productComponents){
      this.id = id;
      this.name = name;
      this.productComponents = productComponents;
   }

  //getters
  public Long getId(){
     return this.id;
  }

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

  public List<Component> getProductComponents(){
     return this.productComponents;
  }


  //setters
  public void setId(Long id){
     this.id = id;
  }

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

 public void setProductComponents(List<Component> productComponents){
    this.productComponents = productComponents;
 }

 public void addProductComponent(Component component) {
    this.productComponents.add(component);
 }

public void removeProductComponent(Component component) {
    this.productComponents.remove(component);
}

}

我的问题:

在控制器类中,我应该将更新方法更改为如下,以具有以下代码 product.addProduct(myComponent) 还是应该如下: product.setProductComponents((Component) myProduct.getProductComponents())

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/products/product")
public class ProductController {
    private static ProductRepository myProductRepository;

    @PutMapping("/update/{id}")
    public Product updateProduct(@RequestBody Product myProduct, @RequestBody Component myComponent, @PathVariable Long id){
        return myProductRepository.findById(id).map((product) ->{
           product.setName(myProduct.getName());
           //updated code
           product.setProductComponents(myProduct.getProductComponents());
           product.addProductComponent(myComponent);
           ///////
        return myProductRepository.save(product);
    }).orElseGet(() ->{
       myProduct.setId(id);
       return myProductRepository.save(myProduct);
    });
}

还是有更好的解决方案?

【问题讨论】:

  • 你应该两者都有。尽管不应该将添加组件的那个称为 setter。将其命名为 addComponent 或类似名称。它通常被称为辅助方法,可能会派上用场。

标签: java spring spring-boot spring-mvc


【解决方案1】:

我建议有

public List<Component> getProductComponents(){
    return this.productComponents;
}

public void setProductComponents(List<Component> productComponents){
    this.productComponents = productComponents;
}

为方便起见:

public void addProductComponent(Component component) {
   this.productComponents.add(component);
}

public void removeProductComponent(Component component) {
   this.productComponents.remove(component);
}

【讨论】:

  • 你现在有什么问题?
  • 在将 addProductComponent() 方法添加到我的实体类后,我已经更新了我的控制器方法,我只想知道我的控制器方法对于将组件添加到 List 是否正确?或者,还有更好的方法 ?谢谢。
  • 添加许多 setProductComponents 是正确的方法。要添加,您应该使用 addProductComponent
  • 知道了。谢谢@Simon
【解决方案2】:

我更喜欢第一个。原因是

  1. 我可以通过传递组件列表轻松创建初始对象
product.setProductComponents((Component) myProduct.getProductComponents())

getProductComponents 我假设返回列表。这样做是行不通的

更好的办法是让 getter 和 setter 变得最笨。优点是您将来可以使用 lombok 等库省略它们

而且你总是可以添加实用方法

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2011-10-05
    • 2015-08-05
    相关资源
    最近更新 更多