企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
步骤如下: **1. 创建实体类和被映射的类** ```java /** * 实体类 * 必须提供Getter和Setter方法 */ @Data public class User { private Integer id; private String name; private Date createTime; } /** * 被映射的类 * 必须提供Setter和Getter方法 * 与实体类User的属性一致 */ @Data public class UserDto01 { private Integer id; private String name; private Date createTime; } /** * 被映射的类 * 必须提供Setter和Getter方法 * 比实体类User少了一个属性 */ @Data public class UserDto02 { private Integer id; private String name; } ``` **2. 创建映射接口** ```java import com.mapstruct.domain.User; import com.mapstruct.dto.UserDto01; import com.mapstruct.dto.UserDto02; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @Mapper public interface UserMapper { /** 成员变量INSTANCE,目的是让客户端可以访问 Mapper 接口的实现 **/ UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); /** User映射到UserDto01 **/ UserDto01 toDto01(User source); /** UserDto01映射到User **/ User toUser01(UserDto01 source); /** User映射到UserDto02 **/ UserDto02 toDto02(User source); /** UserDto02映射到User **/ User toUser02(UserDto02 source); } ``` **3. 测试** ```java public static void main(String[] args) { User user = new User(); user.setId(1001); user.setName("张三"); user.setCreateTime(new Date()); //User-->UserDto01 UserDto01 userDto01 = UserMapper.INSTANCE.toDto01(user); //User-->UserDto02 UserDto02 userDto02 = UserMapper.INSTANCE.toDto02(user); //UserDto01-->User User user01 = UserMapper.INSTANCE.toUser01(userDto01); //UserDto02-->User User user02 = UserMapper.INSTANCE.toUser02(userDto02); System.out.println("User:" + user.toString()); System.out.println("User-->UserDto01:" + userDto01); System.out.println("User-->UserDto02:" + userDto02); System.out.println("UserDto01-->User:" + user01); System.out.println("UserDto02-->User:" + user02); } ``` 输出如下: ``` User:User(id=1001, name=张三, createTime=Thu Oct 14 17:26:49 CST 2021) User-->UserDto01:UserDto01(id=1001, name=张三, createTime=Thu Oct 14 17:26:49 CST 2021) User-->UserDto02:UserDto02(id=1001, name=张三) UserDto01-->User:User(id=1001, name=张三, createTime=Thu Oct 14 17:26:49 CST 2021) UserDto02-->User:User(id=1001, name=张三, createTime=null) ``` **4. 查看映射接口被Mapstruct编译后的代码** 可以发现MapStruct实现`UserMapper`接口,并重写了接口中的方法进行属性赋值。 ```java public class UserMapperImpl implements UserMapper { public UserMapperImpl() { } public UserDto01 toDto01(User source) { if (source == null) { return null; } else { // 对UserDto01的属性赋值 UserDto01 userDto01 = new UserDto01(); userDto01.setId(source.getId()); userDto01.setName(source.getName()); userDto01.setCreateTime(source.getCreateTime()); return userDto01; } } public User toUser01(UserDto01 source) { if (source == null) { return null; } else { // 对User的属性赋值 User user = new User(); user.setId(source.getId()); user.setName(source.getName()); user.setCreateTime(source.getCreateTime()); return user; } } public UserDto02 toDto02(User source) { if (source == null) { return null; } else { // 对UserDto02的属性赋值 UserDto02 userDto02 = new UserDto02(); userDto02.setId(source.getId()); userDto02.setName(source.getName()); return userDto02; } } public User toUser02(UserDto02 source) { if (source == null) { return null; } else { // 对Use的属性赋值 User user = new User(); user.setId(source.getId()); user.setName(source.getName()); return user; } } } ```