参考链接:mapstruct的基本使用
是不是有时候发现明明source和target不是同一个类型,但是却转换成功了,这是因为mapstruct有一套自己的类型转换机制
以下的类型之间是mapstruct自动进行类型转换的。
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
public class Item1 {
Long itemId;
String title;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
public class Item2 {
Long itemId;
String title;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
public class Sku2 {
Long skuId;
String skuCode;
String skuPrice;
List<String> nameList;
Item1 item;
}
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
public class SkuDTO2 {
Long skuId;
String skuCode;
Long skuPrice;
List<String> nameList;
Item2 item;
}
@Mapper
public interface ItemConvert {
ItemConvert INSTANCE = Mappers.getMapper(ItemConvert.class);
SkuDTO2 domain2Dto(Sku2 sku2);
}
// 以下为mapstruct自动生成
public class ItemConvertImpl implements ItemConvert {
@Override
public SkuDTO2 domain2Dto(Sku2 sku2) {
if ( sku2 == null ) {
return null;
}
SkuDTO2 skuDTO2 = new SkuDTO2();
skuDTO2.setSkuId( sku2.getSkuId() );
skuDTO2.setSkuCode( sku2.getSkuCode() );
if ( sku2.getSkuPrice() != null ) {
skuDTO2.setSkuPrice( Long.parseLong( sku2.getSkuPrice() ) );
}
List<String> list = sku2.getNameList();
if ( list != null ) {
skuDTO2.setNameList( new ArrayList<String>( list ) );
}
skuDTO2.setItem( item1ToItem2( sku2.getItem() ) );
return skuDTO2;
}
protected Item2 item1ToItem2(Item1 item1) {
if ( item1 == null ) {
return null;
}
Item2 item2 = new Item2();
item2.setItemId( item1.getItemId() );
item2.setTitle( item1.getTitle() );
return item2;
}
expression="java(。。。)"
@Mapper(imports={DemandSourceEnum.class})
public interface CarDealMapper {
CarDealMapper INSTANCE = Mappers.getMapper(CarDealMapper.class);
@Mappings({
@Mapping(target = "demandSource", expression = "java(DemandSourceEnum.getMeaning(carDealDO.getDemandSource()))"),
SaleDemandVO doToVo(CarDealDO carDealDO);
}
@Mapper
public interface ItemConvert {
ItemConvert INSTANCE = Mappers.getMapper(ItemConvert.class);
SkuDTO2 domain2Dto(Sku2 sku2);
default Item2 item1ToItem2(Item1 item1) {
if (item1 == null) {
return null;
}
Item2 item2 = new Item2();
item2.setItemId(11111L);
item2.setTitle(item1.getTitle());
return item2;
}
}
// mapstruct 的实现类
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-07-30T18:38:11+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_101 (Oracle Corporation)"
)
public class ItemConvertImpl implements ItemConvert {
@Override
public SkuDTO2 domain2Dto(Sku2 sku2) {
if ( sku2 == null ) {
return null;
}
SkuDTO2 skuDTO2 = new SkuDTO2();
skuDTO2.setSkuId( sku2.getSkuId() );
skuDTO2.setSkuCode( sku2.getSkuCode() );
if ( sku2.getSkuPrice() != null ) {
skuDTO2.setSkuPrice( Long.parseLong( sku2.getSkuPrice() ) );
}
List<String> list = sku2.getNameList();
if ( list != null ) {
skuDTO2.setNameList( new ArrayList<String>( list ) );
}
// mapstruct直接调用的是在接口中自定义的实现
skuDTO2.setItem( item1ToItem2( sku2.getItem() ) );
return skuDTO2;
}
}
public class BooleanStrategy {
public String booleanToString(Boolean value) {
if (value == null) {
return "--";
}
return value ? "是" : "否";
}
public Integer booleanToInteger(Boolean value) {
if (value == null) {
return null;
}
return value ? 1 : 0;
}
public Boolean IntegerToBoolean(Integer value) {
if (value == null) {
return null;
}
return value == 0 ? false : true;
}
public String dateFormate(Date date) {
if (date == null) {
return "--";
}
return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
}
}
@Mapper(uses = BooleanStrategy.class)
public interface CarDealMapper {
CarDealMapper INSTANCE = Mappers.getMapper(CarDealMapper.class);
CarDealDO dtoToDo(PlateCityMessageDTO plateCityMessageDTO);
}
方法中target和source 类型满足 BooleanStrategy方法的,会自动进行调用这个方法的类型转换进行处理
若是我们需要给一些属性定义一个固定的值,这个时候可以使用 constant
@Mapping(target = "whetherPass", constant = "true")
CentralizedNewCarShipDTO doToDubboDto(CarDealDO carDealDO);
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss", expression = "java(new Date())")
CarDTO doToDTO(CarDO carDO);
另外还有numberFormat
定义数字的格式转换
defaultValue
/**
* 单个参数的直接使用
*
* @param carDO source do
* @return target dto
*/
@Mapping(target = "userName", constant = "yby")
@Mapping(target = "modelColor", source = "modelColor", defaultValue = "标致")
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss", expression = "java(new Date())")
CarDTO doToDTO(CarDO carDO);
额外的请参考 org.mapstruct.Mapping
原理是遍历source collection然后转换类型,put到target collection中 如果是可以自动转换的则自动转换,同date type conversion;若是无法自动转换的,则会查看是否有可以调用的类型
@Mapper(imports = Date.class)
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
/**
* 基本类型collection的转换
*
* @param integers
* @return
*/
Set<String> integerSetToStringSet(Set<Integer> integers);
/**
* 调用存在已有方法的转换
*
* @param cars
* @return
*/
List<CarDTO> carsToCarDtos(List<CarDO> cars);
/**
* 单个参数的直接使用
*
* @param carDO source do
* @return target dto
*/
@Mapping(target = "userName", constant = "yby")
@Mapping(target = "modelColor", source = "modelColor", defaultValue = "标致")
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss", expression = "java(new Date())")
CarDTO doToDTO(CarDO carDO);
/**
* DTO转为VO
*
* @param warehouseValidDTOList
* @return
*/
List<WarehouseValidPageVO> dtoToVo(List<WarehouseValidDTO> warehouseValidDTOList);
}
// mapstruct实现
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-07-30T19:27:05+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_101 (Oracle Corporation)"
)
public class CarMapperImpl implements CarMapper {
@Override
public Set<String> integerSetToStringSet(Set<Integer> integers) {
if ( integers == null ) {
return null;
}
Set<String> set = new HashSet<String>( Math.max( (int) ( integers.size() / .75f ) + 1, 16 ) );
for ( Integer integer : integers ) {
set.add( String.valueOf( integer ) );
}
return set;
}
@Override
public List<CarDTO> carsToCarDtos(List<CarDO> cars) {
if ( cars == null ) {
return null;
}
List<CarDTO> list = new ArrayList<CarDTO>( cars.size() );
for ( CarDO carDO : cars ) {
list.add( doToDTO( carDO ) );
}
return list;
}
@Override
public CarDTO doToDTO(CarDO carDO) {
if ( carDO == null ) {
return null;
}
CarDTO carDTO = new CarDTO();
if ( carDO.getModelColor() != null ) {
carDTO.setModelColor( carDO.getModelColor() );
}
else {
carDTO.setModelColor( "标致" );
}
if ( carDO.getNewCarGuidePrice() != null ) {
carDTO.setNewCarGuidePrice( BigDecimal.valueOf( carDO.getNewCarGuidePrice() ) );
}
carDTO.setBrandCode( carDO.getBrandCode() );
carDTO.setBrandName( carDO.getBrandName() );
carDTO.setSeriesCode( carDO.getSeriesCode() );
carDTO.setSeriesName( carDO.getSeriesName() );
carDTO.setModelCode( carDO.getModelCode() );
carDTO.setModelName( carDO.getModelName() );
carDTO.setModelColorCode( carDO.getModelColorCode() );
carDTO.setUserName( "yby" );
carDTO.setCreateTime( new Date() );
return carDTO;
}
@Override
public List<WarehouseValidPageVO> dtoToVo(List<WarehouseValidDTO> warehouseValidDTOList) {
if ( warehouseValidDTOList == null ) {
return null;
}
List<WarehouseValidPageVO> list = new ArrayList<WarehouseValidPageVO>( warehouseValidDTOList.size() );
for ( WarehouseValidDTO warehouseValidDTO : warehouseValidDTOList ) {
list.add( warehouseValidDTOToWarehouseValidPageVO( warehouseValidDTO ) );
}
return list;
}
protected WarehouseValidPageVO warehouseValidDTOToWarehouseValidPageVO(WarehouseValidDTO warehouseValidDTO) {
if ( warehouseValidDTO == null ) {
return null;
}
WarehouseValidPageVO warehouseValidPageVO = new WarehouseValidPageVO();
warehouseValidPageVO.setId( warehouseValidDTO.getId() );
warehouseValidPageVO.setWarehouseNo( warehouseValidDTO.getWarehouseNo() );
warehouseValidPageVO.setWarehouseName( warehouseValidDTO.getWarehouseName() );
warehouseValidPageVO.setPlateValid( warehouseValidDTO.getPlateValid() );
return warehouseValidPageVO;
}
}
public interface SourceTargetMapper {
@MapMapping(valueDateFormat = "dd.MM.yyyy")
Map<String, String> longDateMapToStringStringMap(Map<Long, Date> source);
}
// mapstruct自己实现
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-07-30T19:35:22+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_101 (Oracle Corporation)"
)
public class SourceTargetMapperImpl implements SourceTargetMapper {
@Override
public Map<String, String> longDateMapToStringStringMap(Map<Long, Date> source) {
if ( source == null ) {
return null;
}
Map<String, String> map = new HashMap<String, String>( Math.max( (int) ( source.size() / .75f ) + 1, 16 ) );
for ( java.util.Map.Entry<Long, Date> entry : source.entrySet() ) {
String key = new DecimalFormat( "" ).format( entry.getKey() );
String value = new SimpleDateFormat( "dd.MM.yyyy" ).format( entry.getValue() );
map.put( key, value );
}
return map;
}
}