SpringBoot -生成Entity和Dto互转的双向枚举类 -使用注解@Mapper(componentModel = "spring")


1.导入pom文件 ,版本号自定

<!--mapStruct依赖-->
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-jdk8</artifactId>
                <version>1.2.0.CR1</version>

            </dependency>
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.2.0.CR1</version>   
                <scope>provided</scope>
            </dependency>

2.举例编写Training的双向枚举类TrainingConverter

import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface TrainingConverter {
    Training dtoToEntity(TrainingDto trainingDto);
    TrainingDto entityToDto(Training training);
    List<TrainingDto> listEntityToDto(List<Training> training);
    //pageEntityToDto也是支持的
    // listEntityToDto 也支持
}

3.在maven工程结构的root 项下,如图操作maven clean和compile 操作

4.已自动生成了相应的TrainingConverterImpl类

5.自动生成的实现类位置在target/generated-sources/annotations 的同级目录下

6.更多的生成的方法,参考下面的博客

@Mapper注解的使用

7. 手动指定@Mappings的对应 , source对应转换源的字段的字段的字段, target对应目标类的字段


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM