比BeanUtils更好用的Bean转换器 - MapStruct

MapSturct 是一个生成类型安全,高性能且无依赖的 JavaBean 映射代码的注解处理器(annotation processor)。

  • 注解处理器
  • 可以生成 JavaBean 之间那的映射代码
  • 类型安全,高性能,无依赖性

添加依赖

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.5.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.5.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.5.5.Final</version>
        </dependency>

实体类

import lombok.Data;

/**
 * @author 
 */
@Data
public class Student {

    private String name;

    private String age;

}

VO类

import lombok.Data;

/**
 * @author 
 */
@Data
public class StudentVO {

    private String name;

    private String age;

}

MapStruct转换类

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author 
 */
@Mapper
public interface StudentConvert {

    StudentConvert INSTANCE = Mappers.getMapper(StudentConvert.class);

    Student convert(StudentVO bean);

    List<Student> convertList(List<StudentVO> list);

}

启动类测试转换效果

import java.util.ArrayList;
import java.util.List;

/**
 * @author linshuia
 */
public class main {

    public static void main(String[] args) {
        StudentVO studentVO = new StudentVO();
        studentVO.setAge("18");
        studentVO.setName("林");
        Student student = StudentConvert.INSTANCE.convert(studentVO);
        System.out.println(student);

        List<StudentVO> list = new ArrayList<>();
        list.add(studentVO);
        list.add(studentVO);
        System.out.println("-----------------------------------------");
        List<Student> students = StudentConvert.INSTANCE.convertList(list);
        students.stream().forEach(student1 -> System.out.println(student1));

    }

}

运行效果

运行之后在项目的Target的目录下会自动生成Bean转换逻辑

属性名不同时

在需要进行互相转化的时候,则我们可以通过@Mapping 注解来进行转化。

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

import java.util.List;

/**
 * @author
 */
@Mapper
public interface StudentConvert {

    StudentConvert INSTANCE = Mappers.getMapper(StudentConvert.class);

//    @Mapping(source = "name", target = "newName")
    @Mappings({
            @Mapping(source = "pwd",target = "password")
    })
    Student convert(StudentVO bean);

    List<Student> convertList(List<StudentVO> list);

}

  • source: 需要转换的对接,通常是入参
  • target: 转换的对接,通常是出参
  • ignore: 忽略,默认false不忽略,需要忽略设置为true
  • defaultValue: 默认值

和Bean Utils对比

public class main {

    public static void main(String[] args) {

        TestA testA = new TestA();
        testA.setComments("11111");
        testA.setId(1);
        testA.setIp("192.11.11.11");
        testA.setDatasource("mysql");
        testA.setCreateBy("Test");
        testA.setCreateDate(new Date());
        testA.setDatasourceGroup("Test");
        testA.setDbName("Test");
        testA.setJdbcDriverClass("Test");
        testA.setJdbcPassword("Test");
        testA.setPort(8080);
        testA.setJdbcUrl("192.11.11.11");
        testA.setStatus(1);
        testA.setUpdateBy("update");
        testA.setType(1);
        testA.setZkAdress("192.11.11.11");
        long startTime=System.currentTimeMillis ();
        Test test = TestMapstruct.INSTANCE.convert(testA);
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("使用MapStruct转换Bean:程序运行时间: "+(endTime-startTime)+"ms");

        Test tests = new Test();
        long startTimes=System.currentTimeMillis ();
        BeanUtils.copyProperties(tests, testA);
        long endTimes=System.currentTimeMillis(); //获取结束时间
        System.out.println("使用BeanUtils转换Bean:程序运行时间: "+(endTimes-startTimes)+"ms");


    }

}

启动后控制台输入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值