在Spring Boot / Angular中存储/更新映像以及实体数据

问题描述 投票:0回答:1

我是spring-boot的新手。我在Angular 8和postgres DB中有一个spring boot应用程序和前端。我正在使用MapStruct在实体和DTO之间进行映射。我有一个Product实体,创建新记录和更新效果很好。现在,我想在实体中包含图像,并将其保存在DB中。我已经搜索过,发现每个人都在说要使用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;
}
java angular spring-boot mapstruct
1个回答
0
投票

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 );

}
© www.soinside.com 2019 - 2024. All rights reserved.