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");
}
}
启动后控制台输入