第一种方式:
mybatis-plus依赖要用高版本,低版本没有convert方法(测的时候用3.0.5发现没有convert方法)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
mapstruct依赖
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
</dependency>
实体类和List转换
@Mapper
public interface EmpConverter {
EmpConverter INSTANCE = Mappers.getMapper(EmpConverter.class);
void entityToDTO(Emp entity, @MappingTarget EmpDTO dto);
void entityListToDTOList(List<Emp> entity, @MappingTarget List<EmpDTO> dto);
}
//这里直接调用就好了
List<Emp> emps = baseMapper.selectList(lambda);
ArrayList<EmpDTO> empDTOs = new ArrayList<>();
EmpConverter.INSTANCE.entityListToDTOList(emps, empDTOs);
IPage对象转换
public IPage<Emp> queryPage(IPage<Emp> iPage,String code) {
IPage<Emp> empIPage = baseMapper.queryPage(iPage, code);
IPage<EmpDTO> convert = empIPage.convert(this::getEmpDTO);
}
public EmpDTO getEmpDTO(Emp emp){
EmpDTO empDTO = new EmpDTO();
EmpConverter.INSTANCE.entityToDTO(emp, empDTO);
return empDTO;
}
第二种方式:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source> <!-- depending on your project -->
<target>1.8</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
public interface BaseObjectConverter {
@Mapper(componentModel = "spring")
public interface HaircutStoreEntityToVo extends BaseObjectMapper<HaircutStore, HaircutStoreVo>{
}
}
//page转换
public IPage<HaircutStoreVo> queryPage(HaircutStoreDTO haircutStoreDTO, Page page) {
IPage<HaircutStore> haircutStorePage = baseMapper.queryPage(haircutStoreDTO, page);
IPage<HaircutStoreVo> haircutStoreVoPage = haircutStorePage.convert(this::getHaircutStoreVo);
}
//实体类转换
@Override
public List<HaircutStoreVo> queryList(HaircutStoreDTO haircutStoreDTO) {
List<HaircutStore> haircutStores = baseMapper.queryList(haircutStoreDTO);
List<HaircutStoreVo> haircutStoreVoList = haircutStoreEntityToVo.to(haircutStores);
return haircutStoreVoList;
}
//list转换
public HaircutStoreVo getHaircutStoreVo(HaircutStore entity){
HaircutStoreVo sysTenantVo = haircutStoreEntityToVo.to(entity);
return sysTenantVo;
}
第二种方式有个缺点,每次改动实体类都需要编译,否则报错