feat(dto): Enrich data conversion interface and add list copy methods

- Added copyList method to BaseMapper interface to convert object list in batch
- Added partialUpdate method in the ConversionMapper interface to partially update entity objects
- Optimized interface document annotations, clarifying the function and parameter meaning of the method
- Adjusted the order of generic parameters to improve code readability
This commit is contained in:
gewuyou 2025-06-03 15:11:18 +08:00
parent 799ae091c9
commit bf63580581
2 changed files with 54 additions and 18 deletions

View File

@ -5,25 +5,45 @@ import org.mapstruct.MappingTarget
import org.mapstruct.NullValuePropertyMappingStrategy import org.mapstruct.NullValuePropertyMappingStrategy
/** /**
*Base Mapper 基础映射器 * Base Mapper基础映射器
* 提供基础的对象映射操作定义包含合并覆盖合并单个对象拷贝及列表拷贝的方法
* *
* @since 2025-05-30 22:50:18 * @since 2025-05-30 22:50:18
* @author gewuyou * @author gewuyou
*/ */
interface BaseMapper<T,S> { interface BaseMapper<S, T> {
/** /**
* 合并 source target忽略 null * source 对象中的非 null 属性合并到 target 对象中
* 注意null 值的属性不会覆盖 target 中已有的值
*
* @param target 目标对象将被更新
* @param source 源对象提供需要合并的数据
*/ */
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
fun mergeIgnoreNull(@MappingTarget target: T, source: S) fun mergeIgnoreNull(@MappingTarget target: T, source: S)
/** /**
* 全量覆盖合并source 字段即使为 null 也覆盖 target * 全量覆盖合并 source target
* 注意即使 source 中的字段为 null也会覆盖 target 中对应的字段
*
* @param target 目标对象将被更新
* @param source 源对象提供需要合并的数据
*/ */
fun overwriteMerge(@MappingTarget target: T, source: S) fun overwriteMerge(@MappingTarget target: T, source: S)
/** /**
* 拷贝 source 到新对象 * source 对象的内容拷贝到一个新的 T 类型对象中
*
* @param source 源对象提供数据
* @return 返回一个新的目标类型对象
*/ */
fun copy(source: S): T fun copy(source: S): T
/**
* 将源对象列表中的每个元素拷贝为新的目标类型对象生成一个目标对象列表
*
* @param sources 源对象列表
* @return 返回目标类型对象的列表
*/
fun copyList(sources: List<S>): List<T>
} }

View File

@ -1,10 +1,15 @@
package com.gewuyou.forgeboot.webmvc.dto.dto.mapper package com.gewuyou.forgeboot.webmvc.dto.mapper
import org.mapstruct.BeanMapping
import org.mapstruct.MappingTarget
import org.mapstruct.NullValuePropertyMappingStrategy
/** /**
* 转换 映射器 * 转换映射器接口
* *
* 定义了一个转换映射器接口用于在实体类和数据传输对象DTO之间进行转换 * 提供通用的实体类(Entity)与数据传输对象(DTO)之间的双向转换能力
* 这个接口定义了四个基本转换方法实体到DTODTO到实体实体列表到DTO列表和DTO列表到实体列表 * 该接口定义了基础的数据转换方法包括单个对象和集合对象的转换
* 并支持部分更新操作忽略空值属性
* *
* @param <Entity> 实体类类型 * @param <Entity> 实体类类型
* @param <Dto> 数据传输对象类型 * @param <Dto> 数据传输对象类型
@ -12,36 +17,47 @@ package com.gewuyou.forgeboot.webmvc.dto.dto.mapper
* @since 2025-05-30 22:53:35 * @since 2025-05-30 22:53:35
* @author gewuyou * @author gewuyou
*/ */
interface ConversionMapper<Entity, Dto>{ interface ConversionMapper<Entity, Dto> {
/** /**
* 将实体对象转换为DTO对象 * 将实体对象转换为对应的DTO对象
* *
* @param entity 实体对象 * @param entity 需要转换的实体对象
* @return 转换后的DTO对象 * @return 转换后的DTO对象
*/ */
fun toDto(entity: Entity): Dto fun toDto(entity: Entity): Dto
/** /**
* 将DTO对象转换为实体对象 * 将DTO对象转换为对应的实体对象
* *
* @param dto DTO对象 * @param dto 需要转换的DTO对象
* @return 转换后的实体对象 * @return 转换后的实体对象
*/ */
fun toEntity(dto: Dto): Entity fun toEntity(dto: Dto): Entity
/** /**
* 将实体对象列表转换为DTO对象列表 * 将实体对象列表转换为对应的DTO对象列表
* *
* @param entityList 实体对象列表 * @param entityList 需要转换的实体对象列表
* @return 转换后的DTO对象列表 * @return 转换后的DTO对象列表
*/ */
fun toDtoList(entityList: List<Entity>): List<Dto> fun toDtoList(entityList: List<Entity>): List<Dto>
/** /**
* 将DTO对象列表转换为实体对象列表 * 将DTO对象列表转换为对应的实体对象列表
* *
* @param dtoList DTO对象列表 * @param dtoList 需要转换的DTO对象列表
* @return 转换后的实体对象列表 * @return 转换后的实体对象列表
*/ */
fun toEntityList(dtoList: List<Dto>): List<Entity> fun toEntityList(dtoList: List<Dto>): List<Entity>
/**
* 使用非空属性对实体进行部分更新
*
* 注意此操作不会覆盖实体中为空的属性
*
* @param dto 需要用于更新的DTO对象
* @param entity 需要被更新的实体对象
*/
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
fun partialUpdate(dto: Dto, @MappingTarget entity: Entity)
} }