Spring 引导中列表的获取器和设置器
Getters and setters for List in Spring boot
我正在 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);
}
}
我的问题:
在控制器 class 中,我应该将更新方法更改为如下代码,product.addProduct(myComponent) 还是应该我有它如下:
product.setProductComponents((分量)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);
});
}
或者有更好的解决方案吗?
我建议
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);
}
我更喜欢第一个。原因
- 我可以通过传递组件列表轻松创建初始对象
product.setProductComponents((Component) myProduct.getProductComponents())
getProductComponents
嗯 return 我假设的列表。这样做不行
更好的办法是保持 getter 和 setter 最愚蠢。优点是您将来可以使用 lombok
等库省略它们
并且您可以随时添加实用方法
我正在 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);
}
}
我的问题:
在控制器 class 中,我应该将更新方法更改为如下代码,product.addProduct(myComponent) 还是应该我有它如下: product.setProductComponents((分量)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);
});
}
或者有更好的解决方案吗?
我建议
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);
}
我更喜欢第一个。原因
- 我可以通过传递组件列表轻松创建初始对象
product.setProductComponents((Component) myProduct.getProductComponents())
getProductComponents
嗯 return 我假设的列表。这样做不行
更好的办法是保持 getter 和 setter 最愚蠢。优点是您将来可以使用 lombok
等库省略它们并且您可以随时添加实用方法