<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--组件版本管理器--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>org.example</groupId><artifactId>mapstruct-demo</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>1.5.5.Final</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><annotationProcessorPaths><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.5.5.Final</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok-mapstruct-binding</artifactId><version>0.2.0</version></path></annotationProcessorPaths></configuration></plugin></plugins></build></project>
package com.ldj.model;import lombok.Getter;
import lombok.Setter;import java.util.Date;
import java.util.List;/*** User: ldj* Date: 2024/10/24* Time: 23:38* Description: No Description*/
@Getter
@Setter
public class UserDo {private String name;private String gender;private Integer age;private String hobbies;private List<Integer> data;private UserDTO.Child children;private String createTime;
}-------------package com.ldj.model;import lombok.Getter;
import lombok.Setter;import java.util.Date;
import java.util.List;/*** User: ldj* Date: 2024/10/24* Time: 23:37* Description: No Description*/
@Getter
@Setter
public class UserDTO {private String username;private String gender;private Integer age;private List<Integer> data;private Child child;private Date createTime;@Getter@Setterpublic static class Child {private String color;private List<String> msg;}
}
package com.ldj;import com.ldj.model.UserDTO;
import com.ldj.model.UserDo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;import java.text.SimpleDateFormat;
import java.util.Date;/*** User: ldj* Date: 2024/10/24* Time: 22:58* Description: No Description*/
@Mapper
public interface CarMapper {CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);@Mappings({@Mapping(source = "username", target = "name"),@Mapping(source = "child", target = "children"),@Mapping(target = "gender", ignore = true),@Mapping(target = "hobbies", constant = "篮球,羽毛球,乒乓球"),//@Mapping(target = "createTime", expression ="java(com.ldj.utils.DateMapper.date2Str(userDTO.getCreateTime()))")@Mapping(target = "createTime", qualifiedByName ="date2Str")})UserDo dto2do(UserDTO userDTO);// 接收source传过来的new Date()@Named("date2Str")default String date2Str(Date date) {return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);}
}
package com.ldj;import com.alibaba.fastjson.JSON;
import com.ldj.model.UserDTO;
import com.ldj.model.UserDo;import java.util.Arrays;
import java.util.Date;/*** User: ldj* Date: 2024/10/24* Time: 22:56* Description: No Description*/
public class Case1 {public static void main(String[] args) {UserDTO userDTO = new UserDTO();userDTO.setUsername("ldj");userDTO.setGender("男");userDTO.setAge(19);userDTO.setData(Arrays.asList(1, 2, 3, 4, 5));UserDTO.Child child = new UserDTO.Child();child.setColor("红色");child.setMsg(Arrays.asList("a","b"));userDTO.setChild(child);userDTO.setCreateTime(new Date());UserDo userDo = CarMapper.INSTANCE.dto2do(userDTO);System.out.println(JSON.toJSONString(userDo));}
}