MapStruct 是一款基于注解的类字段映射工具,能够自动生成类字段映射的代码。可以自动或手动实现复杂的字段映射,深嵌套也能自动适配。
业务对外提供Dubbo接口的时候,通常通过DTO与外部进行数据交换。要实现类的深拷贝非常麻烦,基于序列化的耗时,自己写的太麻烦。
@Data
public class AnProjectInfoPo {
private Integer id;
private String projectName;
private Integer type;
private String desc;
private String columns;
private Integer createType;
private Integer state;
private Date createTime;
private Date updateTime;
private Integer createUser;
private Integer updateUser;
private Boolean isDeleted;
}
@Data
public class AnProjectInfoDTO {
private Integer id;
private String projectName;
private Integer type;
private String desc;
private String columns;
private Integer createType;
private Integer state;
private Date createTime;
private Date updateTime;
private Integer createUser;
private Integer updateUser;
private Boolean isDeleted;
}
public interface CommonMS<P,D> {
D toDTO(P p);
P toPo(D d);
List<D> toDTO(List<P> pList);
PageInfo<D> toPage(PageInfo<P> pageInfo);
}
@Mapper(componentModel = "spring")
public interface AnProjectInfoMS extends CommonMS<AnProjectInfoPo,AnProjectInfoDTO> {
AnProjectInfoMS INSTANCE = Mappers.getMapper(AnProjectInfoMS.class);
}
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-03-18T21:44:52+0800",
comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class AnProjectInfoMSImpl implements AnProjectInfoMS {
@Override
public AnProjectInfoDTO toDTO(AnProjectInfoPo p) {
if ( p == null ) {
return null;
}
AnProjectInfoDTO anProjectInfoDTO = new AnProjectInfoDTO();
anProjectInfoDTO.setId( p.getId() );
anProjectInfoDTO.setProjectName( p.getProjectName() );
anProjectInfoDTO.setType( p.getType() );
anProjectInfoDTO.setDesc( p.getDesc() );
anProjectInfoDTO.setColumns( p.getColumns() );
anProjectInfoDTO.setCreateType( p.getCreateType() );
anProjectInfoDTO.setState( p.getState() );
anProjectInfoDTO.setCreateTime( p.getCreateTime() );
anProjectInfoDTO.setUpdateTime( p.getUpdateTime() );
anProjectInfoDTO.setCreateUser( p.getCreateUser() );
anProjectInfoDTO.setUpdateUser( p.getUpdateUser() );
anProjectInfoDTO.setIsDeleted( p.getIsDeleted() );
return anProjectInfoDTO;
}
@Override
public AnProjectInfoPo toPo(AnProjectInfoDTO d) {
if ( d == null ) {
return null;
}
AnProjectInfoPo anProjectInfoPo = new AnProjectInfoPo();
anProjectInfoPo.setId( d.getId() );
anProjectInfoPo.setProjectName( d.getProjectName() );
anProjectInfoPo.setType( d.getType() );
anProjectInfoPo.setDesc( d.getDesc() );
anProjectInfoPo.setColumns( d.getColumns() );
anProjectInfoPo.setCreateType( d.getCreateType() );
anProjectInfoPo.setState( d.getState() );
anProjectInfoPo.setCreateTime( d.getCreateTime() );
anProjectInfoPo.setUpdateTime( d.getUpdateTime() );
anProjectInfoPo.setCreateUser( d.getCreateUser() );
anProjectInfoPo.setUpdateUser( d.getUpdateUser() );
anProjectInfoPo.setIsDeleted( d.getIsDeleted() );
return anProjectInfoPo;
}
@Override
public List<AnProjectInfoDTO> toDTO(List<AnProjectInfoPo> pList) {
if ( pList == null ) {
return null;
}
List<AnProjectInfoDTO> list = new ArrayList<AnProjectInfoDTO>( pList.size() );
for ( AnProjectInfoPo anProjectInfoPo : pList ) {
list.add( toDTO( anProjectInfoPo ) );
}
return list;
}
@Override
public PageInfo<AnProjectInfoDTO> toPage(PageInfo<AnProjectInfoPo> pageInfo) {
if ( pageInfo == null ) {
return null;
}
PageInfo<AnProjectInfoDTO> pageInfo1 = new PageInfo<AnProjectInfoDTO>();
pageInfo1.setPageNum( pageInfo.getPageNum() );
pageInfo1.setPageSize( pageInfo.getPageSize() );
pageInfo1.setSize( pageInfo.getSize() );
pageInfo1.setStartRow( pageInfo.getStartRow() );
pageInfo1.setEndRow( pageInfo.getEndRow() );
pageInfo1.setTotal( pageInfo.getTotal() );
pageInfo1.setPages( pageInfo.getPages() );
pageInfo1.setList( toDTO( pageInfo.getList() ) );
pageInfo1.setFirstPage( pageInfo.getFirstPage() );
pageInfo1.setPrePage( pageInfo.getPrePage() );
pageInfo1.setNextPage( pageInfo.getNextPage() );
pageInfo1.setLastPage( pageInfo.getLastPage() );
pageInfo1.setIsFirstPage( pageInfo.isIsFirstPage() );
pageInfo1.setIsLastPage( pageInfo.isIsLastPage() );
pageInfo1.setHasPreviousPage( pageInfo.isHasPreviousPage() );
pageInfo1.setHasNextPage( pageInfo.isHasNextPage() );
pageInfo1.setNavigatePages( pageInfo.getNavigatePages() );
int[] navigatepageNums = pageInfo.getNavigatepageNums();
if ( navigatepageNums != null ) {
pageInfo1.setNavigatepageNums( Arrays.copyOf( navigatepageNums, navigatepageNums.length ) );
}
pageInfo1.setNavigateFirstPage( pageInfo.getNavigateFirstPage() );
pageInfo1.setNavigateLastPage( pageInfo.getNavigateLastPage() );
return pageInfo1;
}
}
import com.alibaba.dubbo.config.annotation.Service;
@Slf4j
@Service //com.alibaba.dubbo.config.annotation.Service;
@Component
public class ProjectInfoServiceImpl implements ProjectInfoService {
@Autowired
private AnProjectInfoDao anProjectInfoDao;
@Override
public List<AnProjectInfoDTO> selectById(int projectId) {
return AnProjectInfoMS.INSTANCE.toDTO(anProjectInfoDao.selectById(projectId));
}
@Override
public AnProjectInfoDTO selectOne(int projectId) {
return AnProjectInfoMS.INSTANCE.toDTO(anProjectInfoDao.selectOneById(projectId));
}
}