我是 spring-boot 的新手。我在 Angular 8 和 postgres DB 中有一个 Spring Boot 应用程序和前端。我正在使用 MapStruct 来进行实体和 DTO 之间的映射。我有一个 Product
实体,创建新记录和更新工作正常。现在我想将图像包含在实体中并保存在数据库中。我搜索并发现每个人都说使用 MultipartFile 方法,但每个解决方案仅包含图像的保存,而不包含实体数据。有没有办法将图像与实体属性一起保存?当图像需要包含在 DTO 中时,MapStruct 如何与图像一起表现?有什么解决办法吗?
产品
@Entity
@Table(name = "products", indexes = {@Index(name= "part_number_index", columnList = "part_number", unique = true)})
public class Product extends UserDateAudit
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Column(name = "part_number", nullable = false)
@Size(max = 20)
private String partNumber;
@NotBlank
@Size(max = 255)
private String description;
@OneToMany(
mappedBy = "product",
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true
)
@Fetch(FetchMode.SELECT)
private List<ReplaceNumber> replaceNumbers = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "product_manufacturer_id", referencedColumnName = "id")
private ProductManufacturer manufacturer;
@ManyToOne
@JoinColumn(name = "product_model_id", referencedColumnName = "id")
private ProductModel model;
@ManyToOne
@JoinColumn(name = "product_category_id", referencedColumnName = "id")
private ProductCategory category;
@Column(name = "cost", nullable = false)
@DecimalMin(message = "Cost should be greater than 1", value = "1")
private float cost;
@Column(name = "price", nullable = false)
@DecimalMin(message = "Price should be greater than 0", value = "0")
private float price;
@Lob
private byte[] image;
}
最佳答案
Mapstruct 知道更新映射的概念。查看MapStruct Documentation 。您可以通过 @InheritConfiguration
重用当前映射。
所以
@Mapper
public interface MyMapper {
// create method
@Mapping( target = "field1", source ="fieldA" )
@Mapping( target = "field2", source ="fieldB" )
Entity map( Dto in );
// update method
@InheritConfiguraton
void map( Dto in, @MappingTarget Entity out );
}
关于java - 在 Spring Boot/Angular 中存储/更新图像以及实体数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60999147/