別再用 BeanUtils 了,這款 PO VO DTO 轉換神器不香么?
閱讀本文大概需要 5?分鐘。
來自:網(wǎng)絡
<properties>
????????<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
????????<maven.compiler.source>1.8maven.compiler.source>
????????<maven.compiler.target>1.8maven.compiler.target>
????????<org.mapstruct.version>1.4.1.Finalorg.mapstruct.version>
????????<org.projectlombok.version>1.18.12org.projectlombok.version>
properties>
<dependencies>
????????<dependency>
????????????<groupId>org.mapstructgroupId>
????????????<artifactId>mapstructartifactId>
????????????<version>${org.mapstruct.version}version>
????????dependency>
????????
????????<dependency>
????????????<groupId>org.projectlombokgroupId>
????????????<artifactId>lombokartifactId>
????????????<version>${org.projectlombok.version}version>
????????????<scope>providedscope>
????????dependency>
????????
?????<dependency>
????????????<groupId>org.mapstructgroupId>
????????????<artifactId>mapstruct-processorartifactId>
????????????<version>${org.mapstruct.version}version>
????????????<scope>providedscope>
????????dependency>
dependencies>
<build>
????????<plugins>
????????????<plugin>
????????????????<groupId>org.apache.maven.pluginsgroupId>
????????????????<artifactId>maven-compiler-pluginartifactId>
????????????????<version>3.8.1version>
????????????????<configuration>
????????????????????<source>1.8source>
????????????????????<target>1.8target>
????????????????????<annotationProcessorPaths>
????????????????????????<path>
????????????????????????????<groupId>org.projectlombokgroupId>
????????????????????????????<artifactId>lombokartifactId>
????????????????????????????<version>${org.projectlombok.version}version>
????????????????????????path>
????????????????????????<path>
????????????????????????????<groupId>org.mapstructgroupId>
????????????????????????????<artifactId>mapstruct-processorartifactId>
????????????????????????????<version>${org.mapstruct.version}version>
????????????????????????path>
????????????????????annotationProcessorPaths>
????????????????configuration>
????????????plugin>
????????plugins>
????build>
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public?class?Student?{
????private?String?name;
????private?int?age;
????private?GenderEnum?gender;
????private?Double?height;
????private?Date?birthday;
}
public?enum?GenderEnum?{
????Male("1",?"男"),
????Female("0",?"女");
????private?String?code;
????private?String?name;
????public?String?getCode()?{
????????return?this.code;
????}
????public?String?getName()?{
????????return?this.name;
????}
????GenderEnum(String?code,?String?name)?{
????????this.code?=?code;
????????this.name?=?name;
????}
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public?class?StudentVO?{
????private?String?name;
????private?int?age;
????private?String?gender;
????private?Double?height;
????private?String?birthday;
}
@Mapper
public?interface?StudentMapper?{
????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);
????@Mapping(source?=?"gender.name",?target?=?"gender")
????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
????StudentVO?student2StudentVO(Student?student);
}
public?class?Test?{
????public?static?void?main(String[]?args)?{
????????Student?student?=?Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new?Date()).build();
????????System.out.println(student);
????????//這行代碼便是實際要用的代碼
????????StudentVO?studentVO?=?StudentMapper.INSTANCE.student2StudentVO(student);
????????System.out.println(studentVO);
????}
}
@Mapper
public?interface?StudentMapper?{
????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);
????@Mapping(source?=?"gender",?target?=?"gender")
????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
????StudentVO?student2StudentVO(Student?student);
????default?String?getGenderName(GenderEnum?gender)?{
????????return?gender.getName();
????}
}
1、List 轉換
@Mapper
public?interface?StudentMapper?{
????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);
????@Mapping(source?=?"gender.name",?target?=?"gender")
????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
????StudentVO?student2StudentVO(Student?student);
????List?students2StudentVOs(List ;?studentList)
}
public?static?void?main(String[]?args)?{
????Student?student?=?Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new?Date()).build();
????List?list?=?new?ArrayList<>();
????list.add(student);
????List?result?=?StudentMapper.INSTANCE.students2StudentVOs(list);
????System.out.println(result);
}
2、多對象轉換到一個對象
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public?class?Student?{
????private?String?name;
????private?int?age;
????private?GenderEnum?gender;
????private?Double?height;
????private?Date?birthday;
}
@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
public?class?Course?{
????private?String?courseName;
????private?int?sortNo;
????private?long?id;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public?class?StudentVO?{
????private?String?name;
????private?int?age;
????private?String?gender;
????private?Double?height;
????private?String?birthday;
????private?String?course;
}
@Mapper
public?interface?StudentMapper?{
????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);
????@Mapping(source?=?"student.gender.name",?target?=?"gender")
????@Mapping(source?=?"student.birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
????@Mapping(source?=?"course.courseName",?target?=?"course")
????StudentVO?studentAndCourse2StudentVO(Student?student,?Course?course);
}
public?class?Test?{
????public?static?void?main(String[]?args)?{
????????Student?student?=?Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new?Date()).build();
????????Course?course?=?Course.builder().id(1L).courseName("語文").build();
????????StudentVO?studentVO?=?StudentMapper.INSTANCE.studentAndCourse2StudentVO(student,?course);
????????System.out.println(studentVO);
????}
}
3、默認值
@Mapper
public?interface?StudentMapper?{
????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);
????@Mapping(source?=?"student.gender.name",?target?=?"gender")
????@Mapping(source?=?"student.birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
????@Mapping(source?=?"course.courseName",?target?=?"course")
????@Mapping(target?=?"name",?source?=?"student.name",?defaultValue?=?"張三")
????StudentVO?studentAndCourse2StudentVO(Student?student,?Course?course);
}
推薦閱讀:
嫌學校 App 太“爛”,極客父母做了開源版本,卻遭官方報警?
最近面試BAT,整理一份面試資料《Java面試BATJ通關手冊》,覆蓋了Java核心技術、JVM、Java并發(fā)、SSM、微服務、數(shù)據(jù)庫、數(shù)據(jù)結構等等。
朕已閱?

