mapStruct(1) : 入门

参考 : 

    https://segmentfault.com/a/1190000020663215?utm_source=tag-newest

    https://blog.csdn.net/zhige_me/article/details/80699784

    https://blog.csdn.net/qq_38542085/article/details/99416865 

属性复制,包含内部类和不同字段,需要转换的对象:TestEntity,转换后的对象:TestDTO

 

测试结果:

// 注释后两个convert
TestEntity:{"data":[{"age":12,"info":{"height":147.5,"like":"补天","nickname":"女娲"},"name":"爱丽丝"},{"age":11,"info":{"height":144.5,"like":"奔月","nickname":"嫦娥"},"name":"安妮"}]}
TestDTO:{"data":[{"age":12,"info":{"height":147.5,"like":"补天"}},{"age":11,"info":{"height":144.5,"like":"奔月"}}]}

// 注释后最后面的convert
TestEntity:{"data":[{"age":12,"info":{"height":147.5,"like":"补天","nickname":"女娲"},"name":"爱丽丝"},{"age":11,"info":{"height":144.5,"like":"奔月","nickname":"嫦娥"},"name":"安妮"}]}
TestDTO:{"data":[{"age":12,"info":{"height":147.5,"like":"补天"},"mingcheng":"爱丽丝"},{"age":11,"info":{"height":144.5,"like":"奔月"},"mingcheng":"安妮"}]}

// 无注释
TestEntity:{"data":[{"age":12,"info":{"height":147.5,"like":"补天","nickname":"女娲"},"name":"爱丽丝"},{"age":11,"info":{"height":144.5,"like":"奔月","nickname":"嫦娥"},"name":"安妮"}]}
TestDTO:{"data":[{"age":12,"info":{"height":147.5,"like":"补天","wangming":"女娲"},"mingcheng":"爱丽丝"},{"age":11,"info":{"height":144.5,"like":"奔月","wangming":"嫦娥"},"mingcheng":"安妮"}]}

 

TestEntity

import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @Auther: liyue
 * @Date: 2020/9/18 16:41
 * @Description:
 */
@Setter
@Getter
public class TestEntity {

    private Integer total;
    private List<Data> data;

    @Setter
    @Getter
    public static class Data {
        private String name;

        private Integer age;

        private Info info;
    }

    @Setter
    @Getter
    public static class Info {
        private Double height;

        private String nickname;

        private String like;

    }
}

TestDTO

import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @Auther: liyue
 * @Date: 2020/9/18 16:40
 * @Description:
 */
@Getter
@Setter
public class TestDTO {

    private List<Data> data;

    @Setter
    @Getter
    public static class Data {
        private String mingcheng;

        private Integer age;

        private Info info;
    }

    @Setter
    @Getter
    public static class Info {
        private Double height;

        private String wangming;

        private String like;

    }
}

TestStruce


import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

/**
 * @Auther: liyue
 * @Date: 2020/9/18 16:38
 * @Description:
 */
@Mapper(componentModel = "spring")
public interface TestStruce {

    TestDTO convert(TestEntity entity);

    @Mappings({
            @Mapping(target = "mingcheng", source = "name"),
    })
    TestDTO.Data convert(TestEntity.Data data);

    @Mappings({
            @Mapping(target = "wangming", source = "nickname"),
    })
    TestDTO.Info convert(TestEntity.Info info);


}

注 : 其他两个convert方法名定义随意,重要的是注解 

测试类:Test


import cn.nordrassil.SpringbootApplication;
import cn.nordrassil.mapstruce.TestDTO;
import cn.nordrassil.mapstruce.TestEntity;
import cn.nordrassil.mapstruce.TestStruce;
import com.alibaba.fastjson.JSONObject;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.LinkedList;
import java.util.List;


/**
 * @Author zanhonglei
 * @DAte 2020/4/14 9:55 上午
 * @Description
 * @Version V1.0
 * @Modify by
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {

    @Autowired
    private TestStruce testStruce;

    @org.junit.Test
    public void test() {
        TestEntity entity = new TestEntity();
        TestEntity.Data data = new TestEntity.Data();
        TestEntity.Info info = new TestEntity.Info();
        info.setNickname("女娲");
        info.setHeight(147.5);
        info.setLike("补天");
        data.setAge(12);
        data.setName("爱丽丝");
        data.setInfo(info);

        TestEntity.Data data2 = new TestEntity.Data();
        TestEntity.Info info2 = new TestEntity.Info();
        info2.setNickname("嫦娥");
        info2.setHeight(144.5);
        info2.setLike("奔月");
        data2.setAge(11);
        data2.setName("安妮");
        data2.setInfo(info2);
        List<TestEntity.Data> datas = new LinkedList<>();
        datas.add(data);
        datas.add(data2);
        entity.setData(datas);

        TestDTO convert = testStruce.convert(entity);
        System.err.println("TestEntity:" + JSONObject.toJSONString(entity));
        System.err.println("TestDTO:" + JSONObject.toJSONString(convert));

    }

}

maven依赖

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.25</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mapStruct 对象转换 -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <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>

 

 

END。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值