Storing/updating 图片和实体数据 spring boot/Angular

Storing/updating Image along with entity data in spring boot/Angular

我是 spring-boot 的新手。我在 Angular 8 和 postgres 数据库中有一个 spring 引导应用程序和前端。我正在使用 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 );

}