1    mapStruct

1.1    相关demo

pom.xml引入相关依赖

<dependency>
            <groupId>org.mapstruct</groupId>
            <!-- jdk8以下就使用mapstruct -->
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.2.0.Final</version>
        </dependency>

 

转换前实体:

package com.ddwei.test.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Student {
    private String id;
    private String name;
}

 

转换后实体:

package com.ddwei.test.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class StudentDto implements Serializable {
    private String stuNo;
    private String stuName;

}

 

转换interface

package com.ddwei.test.service;

import com.ddwei.test.dto.Student;
import com.ddwei.test.dto.StudentDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

//@Mapper(componentModel = "spring")
@Mapper
public interface StudentMapper {
    StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);

    @Mappings({
            @Mapping(source = "id",target = "stuNo"),
            @Mapping(source = "name",target = "stuName"),
    })
    StudentDto toDto(Student student);
}

 

测试类:

package com.ddwei.test;

import com.ddwei.test.dto.Student;
import com.ddwei.test.dto.StudentDto;
import com.ddwei.test.service.StudentMapper;
import org.junit.Test;

public class StudentTest {
    @Test
    public void testMapStruct(){
        Student student = new Student("1","小明");
        StudentDto dto = StudentMapper.INSTANCE.toDto(student);
        System.out.println(dto.toString());

    }
}

 

2.2    嵌套子对象转换、

参考:https://zhuanlan.zhihu.com/p/438048513

  • dto 看product类的count怎么转换
@Data
@EqualsAndHashCode(callSuper = false)
public class Order {
    private Long id;
    private String orderNo;//订单号
    private Date createTime;
    private String receiverAddress; //收货地址
    private User user;//订单所属的用户
    private List<Product> productList; //商品集合
}

@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class Product {
    private Long id;
    private String productSn;
    private String name;
    private String subTitle;
    private String brandName;
    private BigDecimal price;
    private Integer count;//商品数量
    private Date createTime;
}

 

  • 转换类
Mapper(uses = {UserMapper.class,ProductMapper.class})
public interface OrderMapper {
    OrderMapper INSTANCE = Mappers.getMapper(OrderMapper.class);

    @Mapping(source = "user",target = "UserVo")
    @Mapping(source = "productList",target = "productVoList")
    OrderVo convertToVo(Order order);
}

@Mapper(imports = {UUID.class})
public interface ProductMapper {
    ProductMapper INSTANCE = Mappers.getMapper(ProductMapper.class);

    @Mapping(target = "id",constant = "-1L")
    @Mapping(source = "count",target = "number",defaultValue = "1")
    @Mapping(target = "productSn",expression = "java(UUID.randomUUID().toString())")
    ProductVo convertToVo(Product product);
}

 

2    BeanUtil

2.1    BeanUtil  Demo

父类:

package com.ddwei.test.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@AllArgsConstructor
@Getter
@Setter
public class Person {
    private String age;
    private String name;
}

 

子类:

package com.ddwei.test.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Teacher extends Person{
    private String school;

    @Builder
    public Teacher(String age, String name, String school) {
        super(age, name);
        this.school = school;
    }
}

 

实现类:

package com.ddwei.test.normal;

import com.ddwei.test.dto.Person;
import com.ddwei.test.dto.Teacher;
import com.github.houbb.bean.mapping.core.util.BeanUtil;
import org.junit.Assert;
import org.junit.Test;

public class BuilderTest2 {
    @Test
    public void testBuilder(){
        Teacher teacher = Teacher.builder().age("33").school("北京一中").build();
        Person person = new Person("32","小王");
        BeanUtil.copyProperties(person,teacher);
        System.out.println(teacher.getSchool()+" "+teacher.getName()+" "+teacher.getAge());
        Assert.assertEquals("32",teacher.getAge());
    }
}

 

posted on 2021-08-31 23:02  菜鸟乙  阅读(83)  评论(0编辑  收藏  举报