目的:MapStruct可以实现对象之间的转换
一、引入依赖
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mapstruct-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
</dependency>
二、创建两个实体类
import lombok.*;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人实体类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Long id;
private String name;
private Integer age;
}
import lombok.*;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的dto类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class PersonDto {
private Long id;
private String name;
private Integer age;
}
三、创建MapStruct对象转换接口
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.dto.PersonDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的对象mapstruct转换类
*/
@Mapper
public interface PersonConverter {
// 使用工厂方法获取对象实例
PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);
PersonDto personToPersonDto(Person person);
Person personDtoToPerson(PersonDto personDto);
List<PersonDto> personToPersonDto(List<Person> personList);
List<Person> personDtoToPerson(List<PersonDto> personDtoList);
}
四、测试
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.convert.PersonConverter;
import com.example.quartzdemo.dto.PersonDto;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class QuartzDemoApplicationTests {
/**
* person转PersonDto
*/
@Test
void personToPersonDto() {
Person person = new Person(1L, "张三", 25);
PersonDto personDto = PersonConverter.INSTANCE.personToPersonDto(person);
// 输出PersonDto(id=1, name=张三, age=25)
System.out.println(personDto);
}
/**
* personDto转Person
*/
@Test
void personDtoToPerson() {
PersonDto personDto = new PersonDto(2L, "李四", 26);
Person person = PersonConverter.INSTANCE.personDtoToPerson(personDto);
// 输出Person(id=2, name=李四, age=26)
System.out.println(person);
}
/**
* personList转PersonDtoList
*/
@Test
void personListToPersonDtoList() {
List<Person> personList = new ArrayList<>();
personList.add(new Person(1L, "张三", 25));
personList.add(new Person(2L, "李四", 26));
List<PersonDto> personDtoList = PersonConverter.INSTANCE.personToPersonDto(personList);
// 输出[PersonDto(id=1, name=张三, age=25), PersonDto(id=2, name=李四, age=26)]
System.out.println(personDtoList);
}
/**
* PersonDtoList转personList
*/
@Test
void prsonDtoListToPersonList() {
List<PersonDto> personDtoList = new ArrayList<>();
personDtoList.add(new PersonDto(1L, "张三", 25));
personDtoList.add(new PersonDto(2L, "李四", 26));
List<Person> personList = PersonConverter.INSTANCE.personDtoToPerson(personDtoList);
// 输出[Person(id=1, name=张三, age=25), Person(id=2, name=李四, age=26)]
System.out.println(personList);
}
}
五、如果遇到两个实体类属性名不相同的情况,我们需要对转换器接口进行处理
import java.io.Serializable;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人实体类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
private Long id;
private String name;
private Integer age;
private String address;
}
import lombok.*;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的dto类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class PersonDto {
private Long id;
private String name;
private Integer age;
private String addr;
}
两个实体中的address和addr代表的都是地址,但是表现形式不一样。这种情况我们需要对抓获接口进行配置
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.dto.PersonDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的对象mapstruct转换类
*/
@Mapper
public interface PersonConverter {
// 使用工厂方法获取对象实例
PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);
// source:需要转换的属性 target:转换后的属性
@Mappings({
@Mapping(source = "address", target = "addr")
})
PersonDto personToPersonDto(Person person);
@Mappings({
@Mapping(source = "addr", target = "address")
})
Person personDtoToPerson(PersonDto personDto);
@Mappings({
@Mapping(source = "address", target = "addr")
})
List<PersonDto> personToPersonDto(List<Person> personList);
@Mappings({
@Mapping(source = "addr", target = "address")
})
List<Person> personDtoToPerson(List<PersonDto> personDtoList);
}
测试
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.convert.PersonConverter;
import com.example.quartzdemo.dto.PersonDto;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class QuartzDemoApplicationTests {
/**
* person转PersonDto
*/
@Test
void personToPersonDto() {
Person person = new Person(1L, "张三", 25, "广州");
PersonDto personDto = PersonConverter.INSTANCE.personToPersonDto(person);
// 输出PersonDto(id=1, name=张三, age=25, addr=广州)
System.out.println(personDto);
}
/**
* personDto转Person
*/
@Test
void personDtoToPerson() {
PersonDto personDto = new PersonDto(2L, "李四", 26, "广州");
Person person = PersonConverter.INSTANCE.personDtoToPerson(personDto);
// 输出Person(id=2, name=李四, age=26, address=广州)
System.out.println(person);
}
/**
* personList转PersonDtoList
*/
@Test
void personListToPersonDtoList() {
List<Person> personList = new ArrayList<>();
personList.add(new Person(1L, "张三", 25, "广州"));
personList.add(new Person(2L, "李四", 26, "广州"));
List<PersonDto> personDtoList = PersonConverter.INSTANCE.personToPersonDto(personList);
// 输出[PersonDto(id=1, name=张三, age=25, addr=广州), PersonDto(id=2, name=李四, age=26, addr=广州)]
System.out.println(personDtoList);
}
/**
* PersonDtoList转personList
*/
@Test
void prsonDtoListToPersonList() {
List<PersonDto> personDtoList = new ArrayList<>();
personDtoList.add(new PersonDto(1L, "张三", 25, "广州"));
personDtoList.add(new PersonDto(2L, "李四", 26, "广州"));
List<Person> personList = PersonConverter.INSTANCE.personDtoToPerson(personDtoList);
// 输出[Person(id=1, name=张三, age=25, address=广州), Person(id=2, name=李四, age=26, address=广州)]
System.out.println(personList);
}
}
六、自定义转换 对于某些类型,没有办法通过代码生成器来进行处理,就需要自定义方法来进行转换。
import java.io.Serializable;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人实体类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person implements Serializable {
private Long id;
private String name;
private Integer age;
private String address;
private SubPerson subPerson;
}
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class SubPerson {
private Integer deleted;
private String name;
}
import lombok.*;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的dto类
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class PersonDto {
private Long id;
private String name;
private Integer age;
private String addr;
private SubPersonDto subPersonDto;
}
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class SubPersonDto {
private Boolean result;
private String name;
}
我们需要在转换接口中添加自定义的转换方法
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.bean.SubPerson;
import com.example.quartzdemo.dto.PersonDto;
import com.example.quartzdemo.dto.SubPersonDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* @author qinxun
* @date 2023-06-05
* @Descripion: 人的对象mapstruct转换类
*/
@Mapper
public interface PersonConverter {
// 使用工厂方法获取对象实例
PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);
// source:需要转换的属性 target:转换后的属性
@Mappings({
@Mapping(source = "address", target = "addr"),
@Mapping(source = "subPerson", target = "subPersonDto")
})
PersonDto personToPersonDto(Person person);
@Mappings({
@Mapping(source = "addr", target = "address"),
@Mapping(source = "subPersonDto", target = "subPerson")
})
Person personDtoToPerson(PersonDto personDto);
@Mappings({
@Mapping(source = "address", target = "addr"),
@Mapping(source = "subPerson", target = "subPersonDto")
})
List<PersonDto> personToPersonDto(List<Person> personList);
@Mappings({
@Mapping(source = "addr", target = "address"),
@Mapping(source = "subPersonDto", target = "subPerson")
})
List<Person> personDtoToPerson(List<PersonDto> personDtoList);
/**
* 属性的自定义转换
*/
default SubPersonDto subPersonToSubPersonDto(SubPerson subPerson) {
if (subPerson == null) {
return null;
}
SubPersonDto subPersonDto = new SubPersonDto();
subPersonDto.setResult(!subPerson.getDeleted().equals(0));
subPersonDto.setName(subPerson.getName());
return subPersonDto;
}
/**
* 属性的自定义转换
*/
default SubPerson subPersonDtoToSubPerson(SubPersonDto subPersonDto) {
if (subPersonDto == null) {
return null;
}
SubPerson subPerson = new SubPerson();
subPerson.setDeleted(subPersonDto.getResult() ? 1 : 0);
subPerson.setName(subPersonDto.getName());
return subPerson;
}
}
测试
import com.example.quartzdemo.bean.Person;
import com.example.quartzdemo.bean.SubPerson;
import com.example.quartzdemo.convert.PersonConverter;
import com.example.quartzdemo.dto.PersonDto;
import com.example.quartzdemo.dto.SubPersonDto;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class QuartzDemoApplicationTests {
/**
* person转PersonDto
*/
@Test
void personToPersonDto() {
SubPerson subPerson = new SubPerson(0, "zhangsan");
Person person = new Person(1L, "张三", 25, "广州", subPerson);
PersonDto personDto = PersonConverter.INSTANCE.personToPersonDto(person);
// 输出PersonDto(id=1, name=张三, age=25, addr=广州, subPersonDto=SubPersonDto(result=false, name=zhangsan))
System.out.println(personDto);
}
/**
* personDto转Person
*/
@Test
void personDtoToPerson() {
SubPersonDto subPersonDto = new SubPersonDto(false, "lisi");
PersonDto personDto = new PersonDto(2L, "李四", 26, "广州", subPersonDto);
Person person = PersonConverter.INSTANCE.personDtoToPerson(personDto);
// 输出Person(id=2, name=李四, age=26, address=广州, subPerson=SubPerson(deleted=0, name=lisi))
System.out.println(person);
}
/**
* personList转PersonDtoList
*/
@Test
void personListToPersonDtoList() {
List<Person> personList = new ArrayList<>();
personList.add(new Person(1L, "张三", 25, "广州", new SubPerson(0, "zhangsan")));
personList.add(new Person(2L, "李四", 26, "广州", new SubPerson(1, "lisi")));
List<PersonDto> personDtoList = PersonConverter.INSTANCE.personToPersonDto(personList);
// 输出[PersonDto(id=1, name=张三, age=25, addr=广州, subPersonDto=SubPersonDto(result=false, name=zhangsan)), PersonDto(id=2, name=李四, age=26, addr=广州, subPersonDto=SubPersonDto(result=true, name=lisi))]
System.out.println(personDtoList);
}
/**
* PersonDtoList转personList
*/
@Test
void prsonDtoListToPersonList() {
List<PersonDto> personDtoList = new ArrayList<>();
personDtoList.add(new PersonDto(1L, "张三", 25, "广州", new SubPersonDto(false, "zhangsan")));
personDtoList.add(new PersonDto(2L, "李四", 26, "广州", new SubPersonDto(true, "lisi")));
List<Person> personList = PersonConverter.INSTANCE.personDtoToPerson(personDtoList);
// 输出[Person(id=1, name=张三, age=25, address=广州, subPerson=SubPerson(deleted=0, name=zhangsan)), Person(id=2, name=李四, age=26, address=广州, subPerson=SubPerson(deleted=1, name=lisi))]
System.out.println(personList);
}
}