mapstruct+lombok实现DO到DTO映射

1.配置

1.3.0.Final

org.mapstruct mapstruct ${org.mapstruct.version} org.mapstruct mapstruct-processor ${org.mapstruct.version} provided org.projectlombok lombok

2.编写代码 【mapstruct+lombok实现DO到DTO映射】DO:
@Data @AllArgsConstructor public class User { private String userName; private Integer age; private String address; private Integer sex; private Date birthday; }

DTO:
@Data public class UserDto { private String name; private Integer age; private String sex; private String addr; private String birthday; }

MAPPER:
@Mapper public interface UserMapper {UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); @Mappings({ @Mapping(source = "userName",target = "name"), @Mapping(source = "address",target = "addr"), @Mapping(source = "birthday",target = "birthday",dateFormat = "yyyy-MM-dd HH:mm:ss") }) UserDto userToUserDto(User user); }

测试:
@Test public void test() { User user = new User("xm",20,"sz",1,new Date()); UserDto userDto = UserMapper.INSTANCE.userToUserDto(user); System.out.println(userDto); }

    推荐阅读