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