You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用MapStruct实现HotelInfo/List<HotelInfo>到GeneralModel的映射

使用MapStruct实现HotelInfo到GeneralModel的映射

步骤1:引入MapStruct依赖

在项目构建文件中添加MapStruct核心库与处理器依赖(以Maven为例):

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.5.Final</version> <!-- 建议使用最新稳定版 -->
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source> <!-- 匹配你的项目JDK版本 -->
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.5.5.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

步骤2:编写MapStruct映射器接口

创建映射器接口,定义所需转换方法,MapStruct会自动生成实现类:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.List;
import java.util.Collections;

@Mapper
public interface HotelMapper {

    // 生成映射器实例,用于调用转换方法
    HotelMapper INSTANCE = Mappers.getMapper(HotelMapper.class);

    // 自动映射HotelInfo到HotelDomain(属性名、类型完全匹配,无需额外配置)
    HotelDomain hotelInfoToHotelDomain(HotelInfo hotelInfo);

    // 单个HotelInfo转GeneralModel:将单个HotelDomain包装为列表赋值给hotelDomains
    @Mapping(target = "hotelDomains", expression = "java(Collections.singletonList(hotelInfoToHotelDomain(hotelInfo)))")
    GeneralModel hotelInfoToGeneralModel(HotelInfo hotelInfo);

    // 自动映射List<HotelInfo>到List<HotelDomain>
    List<HotelDomain> hotelInfoListToHotelDomainList(List<HotelInfo> hotelInfos);

    // List<HotelInfo>转GeneralModel:直接映射集合到hotelDomains字段
    @Mapping(target = "hotelDomains", source = "hotelInfos")
    GeneralModel listOfHotelInfoToGeneralModel(List<HotelInfo> hotelInfos);
}

步骤3:自定义字段映射(可选)

如果需要给GeneralModel的identificationNo赋值,比如从HotelInfo的id获取,只需在对应方法上添加@Mapping注解:

// 单个转换时,将HotelInfo的id赋值给identificationNo
@Mapping(target = "identificationNo", source = "id")
@Mapping(target = "hotelDomains", expression = "java(Collections.singletonList(hotelInfoToHotelDomain(hotelInfo)))")
GeneralModel hotelInfoToGeneralModel(HotelInfo hotelInfo);

// 集合转换时,给identificationNo设置固定值
@Mapping(target = "identificationNo", constant = "DEFAULT_COLLECTION_ID")
@Mapping(target = "hotelDomains", source = "hotelInfos")
GeneralModel listOfHotelInfoToGeneralModel(List<HotelInfo> hotelInfos);

步骤4:调用转换方法

在业务代码中直接通过映射器实例调用转换逻辑:

import java.util.Arrays;
import java.util.List;

public class HotelConversionDemo {
    public static void main(String[] args) {
        // 单个HotelInfo转换
        HotelInfo singleHotel = new HotelInfo("H001", "阳光酒店");
        GeneralModel singleModel = HotelMapper.INSTANCE.hotelInfoToGeneralModel(singleHotel);

        // List<HotelInfo>转换
        List<HotelInfo> hotelList = Arrays.asList(
            new HotelInfo("H002", "星辰酒店"),
            new HotelInfo("H003", "云端酒店")
        );
        GeneralModel listModel = HotelMapper.INSTANCE.listOfHotelInfoToGeneralModel(hotelList);
    }
}

内容的提问来源于stack exchange,提问作者Abdullah Imran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 17:35:15