【问题标题】:Storing/updating Image along with entity data in spring boot/Angular在 Spring Boot/Angular 中存储/更新图像以及实体数据
【发布时间】:2020-04-02 19:02:26
【问题描述】:

我是弹簧靴的新手。我在 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;
}

【问题讨论】:

    标签: java angular spring-boot mapstruct


    【解决方案1】:

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

    【讨论】:

    • 谢谢@Sjaak。我的映射工作正常。我正在寻找将图像文件存储在 db 中的实体示例。
    • 如果问题得到解决,请将问题标记为已回答,以便其他人也能从中受益
    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 2019-07-04
    • 2015-12-22
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2018-07-22
    相关资源
    最近更新 更多