官网: https://mapstruct.org/documentation/installation/

 


<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct</artifactId>
      <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mapstruct</groupId>
      <artifactId>mapstruct-processor</artifactId>
      <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.66</version>
    </dependency>

    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.10.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.10</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>
  </dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

 

dto

@Data
@ToString
public class PersonDto implements Serializable {

    private Integer id;

    private String nickName;

    private String address;

    private List<Bag> bagList;

    private String cardNo;

    private String birthdayStr;

    private Date registerDate;

    private Integer sex;

    private List<String> foods;

    private String phone;

    private String buyStatus;

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

entity

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bag implements Serializable {

    private String title;
    
    private BigDecimal price;

    private Date buyDate;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

 

@Data
@ToString
public class Person implements Serializable {

    private Integer id;

    private String nickName;

    private String address;

    private List<Bag> bagList;

    private String cardNum;

    private Date birthdayDate;

    private String registerStr;

    private Integer gender;

    private List<String> foods;

    private Integer buyed;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

utils

@Slf4j
public class DateUtils {
    public static final String TIME_ZONE = "GMT+8";
    public static final String LUNA_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * String(yyyy-MM-dd HH:mm:ss)格式日期转为Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parseDateTime(String str) throws ParseException {
        return org.apache.commons.lang3.time.DateUtils.parseDate(str, new String[]{LUNA_DATE_TIME});
    }


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

 

@Mapper
public interface PersonConvert {

    PersonConvert PERSON_CONVERT = Mappers.getMapper(PersonConvert.class);

    @Mappings({
            //映射
            @Mapping(source = "cardNum", target = "cardNo"),
            //Date 格式化为  String
            @Mapping(source = "birthdayDate", target = "birthdayStr", dateFormat = "yyyy-MM-dd"),
            //String  格式化为 Date
            @Mapping(target = "registerDate", expression = "java(com.luna.mapstruct.util.DateUtils.parseDateTime(person.getRegisterStr()))"),
            //映射默认值
            @Mapping(source = "gender", target = "sex", defaultValue = "1"),
            //默认表达式
            @Mapping(target = "foods", defaultExpression = "java(new java.util.ArrayList<>())"),
            //映射常量值
            @Mapping(target = "phone", constant = "123@qq.com")

    })
    PersonDto convertDto(Person person) throws Exception;

    /**
     * 将在转换执行
     * @param person
     * @param personDto
     */
    @AfterMapping
    default void convertBuyed(Person person, @MappingTarget PersonDto personDto) {
        if (person.getBuyed() != null && person.getBuyed() == 1) {
            personDto.setBuyStatus("已购");
        }
    }

    /**
     * 列表转换
     * @param peopleList
     * @return
     */
    List<PersonDto> convertListDto(List<Person> peopleList);



//    @InheritConfiguration
    //取消默认转换,仅指定哪些字段映射
    @BeanMapping(ignoreByDefault = true)
    @Mappings({
            @Mapping(source = "id",target = "id")

    })
    PersonDto update(Person person, @MappingTarget PersonDto personDTO)throws Exception;


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

 

Test

public class AppTest {


    @Test
    public void testMapstruct() throws Exception {
//        testDto();
//        testListDto();
        testCustomDto();
    }

    public void testDto() throws Exception {
        Person person = new Person();
        person.setId(1);
        person.setNickName("张三");
        person.setAddress("杭州");
        person.setCardNum("123");
        person.setBirthdayDate(new Date());
        person.setRegisterStr("2022-01-01 12:00:00");
        List<Bag> list = new ArrayList<>();
        list.add(new Bag("红色", BigDecimal.valueOf(1), new Date()));
        list.add(new Bag("黑色", BigDecimal.valueOf(2), new Date()));
        person.setBagList(list);
        person.setBuyed(1);
        PersonDto personDto =
                PersonConvert.PERSON_CONVERT.convertDto(person);
        System.out.println(JSON.toJSONString(personDto));


        person.setNickName("李四");
        PersonDto update = PersonConvert.PERSON_CONVERT.update(person, personDto);
        System.out.println(JSON.toJSONString(update));
    }

    /**
     * 自定义转换
     * @throws Exception
     */
    public void testCustomDto() throws Exception {
        Person person = new Person();
        person.setId(1);
//        person.setNickName("张三");
//        person.setAddress("杭州");
//        person.setCardNum("123");
        person.setBirthdayDate(new Date());
        person.setRegisterStr("2022-01-01 12:00:00");
        PersonDto personDto =
                PersonConvert.PERSON_CONVERT.update(person,new PersonDto());
        System.out.println(JSON.toJSONString(personDto));
    }


    public void testListDto() {
        Person person = new Person();
        person.setId(1);
        person.setNickName("张三");
        person.setAddress("杭州");
        person.setCardNum("123");
        person.setBirthdayDate(new Date());
        person.setRegisterStr("2022-01-01 12:00:00");
        List<Bag> list = new ArrayList<>();
        list.add(new Bag("红色", BigDecimal.valueOf(1), new Date()));
        list.add(new Bag("黑色", BigDecimal.valueOf(2), new Date()));
        person.setBagList(list);
        List<PersonDto> personDtoList =
                PersonConvert.PERSON_CONVERT.convertListDto(Arrays.asList(person));
        System.out.println(JSON.toJSONString(personDtoList));
    }


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.