MapStruct是什么
MapStruct是一个代码生成器,用于简化Java实体类型之间转换,使用时只需定义Mapper映射接口,会自动为我们生成转换代码。本质上MapStruct是通过普通方法调用进行字段映射,快速、类型安全、简单、易于理解。
主页: https://mapstruct.org/
GitHub: https://github.com/mapstruct/mapstruct/
Demo: https://github.com/mapstruct/mapstruct-examples
MapStruct使用方式
1.1 Maven坐标
<properties>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
1.2 定义基础转换类
package com.giser.mp.entmp;
import java.util.List;
/**
*
* 基础转换类,提供几个基本方法,直接继承使用
* 更多的用法需自行实现
* @param <DTO> 目标对象,一般为DTO对象
* @param <ENTITY> 源对象,一般为需要转换的对象
* @param <VO> 目标对象,一般为VO对象
*/
public interface BaseMapStructMapper<DTO, ENTITY, VO> {
/**
* 将源对象转换为DTO对象
* @param e
* @return D
*/
DTO entityToDTO(ENTITY e);
/**
* 将源对象集合转换为DTO对象集合
* @param es
* @return List<D>
*/
List<DTO> entitiesToDTOList(List<ENTITY> es);
/**
* 将源对象转换为VO对象
* @param e
* @return V
*/
VO entityToVO(ENTITY e);
/**
* 将DTO对象转换为VO对象
* @param d
* @return V
*/
VO dtoToVO(DTO d);
/**
* 将源对象集合转换为VO对象集合
* @param es
* @return List<D>
*/
List<VO> entitiesToVOList(List<ENTITY> es);
/**
* 将目标对象转换为源对象
* @param d
* @return E
*/
ENTITY dtoToEntity(DTO d);
/**
* 将目标对象集合转换为源对象集合
* @param ds
* @return List<E>
*/
List<ENTITY> dtosToEntityList(List<DTO> ds);
}
13 使用时,继承基础转换类,并标注@Mapper注解(org.mapstruct.Mapper)即可
@Mapper(componentModel = "spring")
public interface EarlyEntityMapper extends BaseMapStructMapper<EarlyDto, EarlyEntity, EarlyVo> {
}
1.4 测试
/**
* @see https://mapstruct.org/
* @see https://github.com/mapstruct/mapstruct/
* @see https://github.com/mapstruct/mapstruct-examples
*/
@SpringBootTest(classes = JavaMpApp.class)
@Slf4j
public class MapStructTest {
@Autowired
private EarlyEntityMapper earlyEntityMapper;
@Test
public void testDto2Ent(){
EarlyDto earlyDto = new EarlyDto(1L,"xiangjiao dto",22,null);
EarlyEntity entity = earlyEntityMapper.dtoToEntity(earlyDto);
System.out.println(entity);
}
}
5.编译后的文件
package BOOT-INF.classes.com.giser.mp.entmp;
import com.giser.mp.entity.EarlyDto;
import com.giser.mp.entity.EarlyEntity;
import com.giser.mp.entity.EarlyVo;
import com.giser.mp.entmp.EarlyEntityMapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class EarlyEntityMapperImpl implements EarlyEntityMapper {
public EarlyDto entityToDTO(EarlyEntity e) {
if (e == null)
return null;
EarlyDto earlyDto = new EarlyDto();
earlyDto.setId(e.getId());
earlyDto.setName(e.getName());
earlyDto.setAge(e.getAge());
earlyDto.setEmail(e.getEmail());
return earlyDto;
}
public List<EarlyDto> entitiesToDTOList(List<EarlyEntity> es) {
if (es == null)
return null;
List<EarlyDto> list = new ArrayList<>(es.size());
for (EarlyEntity earlyEntity : es)
list.add(entityToDTO(earlyEntity));
return list;
}
public EarlyVo entityToVO(EarlyEntity e) {
if (e == null)
return null;
EarlyVo earlyVo = new EarlyVo();
earlyVo.setId(e.getId());
earlyVo.setName(e.getName());
earlyVo.setAge(e.getAge());
earlyVo.setEmail(e.getEmail());
return earlyVo;
}
public EarlyVo dtoToVO(EarlyDto d) {
if (d == null)
return null;
EarlyVo earlyVo = new EarlyVo();
earlyVo.setId(d.getId());
earlyVo.setName(d.getName());
earlyVo.setAge(d.getAge());
earlyVo.setEmail(d.getEmail());
return earlyVo;
}
public List<EarlyVo> entitiesToVOList(List<EarlyEntity> es) {
if (es == null)
return null;
List<EarlyVo> list = new ArrayList<>(es.size());
for (EarlyEntity earlyEntity : es)
list.add(entityToVO(earlyEntity));
return list;
}
public EarlyEntity dtoToEntity(EarlyDto d) {
if (d == null)
return null;
EarlyEntity earlyEntity = new EarlyEntity();
earlyEntity.setId(d.getId());
earlyEntity.setName(d.getName());
earlyEntity.setAge(d.getAge());
earlyEntity.setEmail(d.getEmail());
return earlyEntity;
}
public List<EarlyEntity> dtosToEntityList(List<EarlyDto> ds) {
if (ds == null)
return null;
List<EarlyEntity> list = new ArrayList<>(ds.size());
for (EarlyDto earlyDto : ds)
list.add(dtoToEntity(earlyDto));
return list;
}
}