MapStruct-Plus 使用教程
泛域名ssl证书 239元1年送1个月、单域名39元1年,Sectigo(原Comodo证书)全球可信证书,强大的兼容性,高度安全性,如有问题7天内可退、可开发票
加微信VX 18718058521 备注SSL证书
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
前言
提示:你还在使用MapStruct繁琐的配置吗?不妨试试Plus,只做增强,不做修改。
Mapstruct Plus 内嵌 Mapstruct,和 Mapstruct 完全兼容,如果之前已经使用 Mapstruct,可以无缝替换依赖`
官方文档
一、依赖包POM.xml文件
<properties> <java.version>19</java.version> <mapstruct-plus.version>1.3.5</mapstruct-plus.version> <lombok.version>1.18.30</lombok.version> </properties> <dependencies> <dependency> <groupId>io.github.linpeilie</groupId> <artifactId>mapstruct-plus-spring-boot-starter</artifactId> <version>${mapstruct-plus.version}</version> </dependency> <!-- knife4j 版本4.0.0——SpringBoot版本兼容性 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <!-- maven-compiler-plugin作用:指定 Java 版本、设置编码方式 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> <path> <groupId>io.github.linpeilie</groupId> <artifactId>mapstruct-plus-processor</artifactId> <version>${mapstruct-plus.version}</version> </path> <!-- 将 Lombok 注解和 MapStruct 的映射注解进行绑定,使它们能够协同工作 --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
二、实体类
package com.example.mapstructplusdemo.model.entity; import io.github.linpeilie.annotations.AutoMapper; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.Accessors; /** * @Description : 实体类User * @Author : LiYan * @CreateTime : 2023/12/20 15:29 */ @ToString @Data @Accessors(chain = true) @AllArgsConstructor @NoArgsConstructor @AutoMapper(target = UserDto.class) public class User { private String username; private int age; private boolean young; } @ToString @AllArgsConstructor @NoArgsConstructor @Accessors(chain = true) @Data public class UserDto { private String username; // 属性名称不同时. @AutoMapping(target = "age") private int ageB; private boolean young; }
三、测试类
package com.example.mapstructplusdemo; import com.example.mapstructplusdemo.model.entity.User; import com.example.mapstructplusdemo.model.entity.UserDto; import io.github.linpeilie.Converter; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class MapstructPlusDemoApplicationTests { @Autowired private Converter converter; @Test void contextLoads() { User user = new User() .setUsername("jack") .setAge(23) .setYoung(false); UserDto userDto = converter.convert(user, UserDto.class); System.out.println("userDto: "+ userDto); /** * 断言语句作用:验证代码的正确性。当断言失败时,会抛出AssertionError异常, * 提醒我们假设不成立,需修正。 * 上线时,需删除,以提高代码的执行效率。 */ assert user.getUsername().equals(userDto.getUsername()); assert user.getAge() == userDto.getAge(); assert user.isYoung() == userDto.isYoung(); User newUser = converter.convert(userDto, User.class); System.out.println(newUser); assert user.getUsername().equals(newUser.getUsername()); assert user.getAge() == newUser.getAge(); assert user.isYoung() == newUser.isYoung(); }
总结
是真的香啊!