微服务持久化与API扩展实践
1. 实体删除方法与Java Bean映射器
实体删除方法使用仓库中的 findByProductId() 方法和 Optional 类的 ifPresent() 方法,仅在实体存在时进行删除,且该实现是幂等的,若未找到实体不会报告失败。
Java Bean映射器使用MapStruct声明,以产品微服务的映射器类为例:
@Mapper(componentModel = "spring")
public interface ProductMapper {
@Mappings({
@Mapping(target = "serviceAddress", ignore = true)
})
Product entityToApi(ProductEntity entity);
@Mappings({
@Mapping(target = "id", ignore = true),
@Mapping(target = "version", ignore = true)
})
ProductEntity apiToEntity(Product api);
}
-
entityToApi()方法将实体对象映射到API模型对象,忽略serviceAddress字段。 -
apiToEn
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



