Java Spring boot中列表的getter和setter
我正在SpringBoot中创建API。 我认为产品由以下组件组成: 这是我的密码: (实体层)Java Spring boot中列表的getter和setter,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我正在SpringBoot中创建API。 我认为产品由以下组件组成: 这是我的密码: (实体层) 还是有更好的解决方案?我建议 public List<Component> getProductComponents(){ return this.productComponents; } public void setProductComponents(List<Component> productComponents){ this.productComp
还是有更好的解决方案?我建议
public List<Component> getProductComponents(){
return this.productComponents;
}
public void setProductComponents(List<Component> productComponents){
this.productComponents = productComponents;
}
我建议你
public List<Component> getProductComponents(){
return this.productComponents;
}
public void setProductComponents(List<Component> productComponents){
this.productComponents = productComponents;
}
我喜欢第一个。理性存在
product.setProductComponents((组件)myProduct.getProductComponents())
getProductComponents
我假设的井返回列表。这样做是行不通的
更好的办法是让能手和二传手保持最愚蠢。优点是您可以在将来使用像lombok这样的库省略它们
您可以随时添加实用方法,我更喜欢第一种。理性存在
product.setProductComponents((组件)myProduct.getProductComponents())
getProductComponents
我假设的井返回列表。这样做是行不通的
更好的办法是让能手和二传手保持最愚蠢。优点是您可以在将来使用像lombok这样的库省略它们
您可以随时添加实用方法这两种方法都应该有。尽管添加组件的不应称为setter。将其命名为
addComponent
或类似的名称。它通常被称为辅助方法,可能会派上用场。两者都应该有。尽管添加组件的不应称为setter。将其命名为addComponent
或类似的名称。它通常被称为辅助方法,可能会派上用场。您现在的问题是什么?在将addProductComponent()方法添加到实体类后,我更新了我的控制器方法,我只想知道我的控制器方法在将组件添加到列表中时是否正确?还是有更好的办法?谢谢。添加许多setProductComponents是正确的方法。对于添加,您应该使用addProductComponentGot。谢谢@Simon您现在的问题是什么?在将addProductComponent()方法添加到实体类后,我已经更新了我的控制器方法,我只想知道我的控制器方法在将组件添加到列表中时是否正确?还是有更好的办法?谢谢。添加许多setProductComponents是正确的方法。对于添加,您应该使用addProductComponentGot。谢谢你,西蒙
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);
}