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

标签 java angular spring-boot mapstruct

我是 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/

相关文章:

java - 尝试调用抛出异常的方法时,增强的 for 循环不会被执行

angular - 在 ag-grid 中隐藏列名?

javascript - ionic 2 : hide and display div with ngClass

java - 如何拦截Spring拦截器对Node Service的请求

spring-boot - 如何使用嵌入式 Tomcat 将 JNDI 数据库连接与 Spring Boot 和 Spring Data 连接?

java - 应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常

java - 如何同时使用 Id 和 Value 属性来单击元素?

java - 从另一个kubernetes作业触发Kubernetes

node.js - 在 Visual Studio 2015 中右键单击时缺少“还原包”选项

java - 如何提高Spring Boot应用程序的性能