版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/85254259
这里要注意,是String转Number或Number或String才有效,如果是Number转Number或其他类型的转换均不生效,比如Double转Double(用于格式化小数位)时不生效。
先浏览:mapstruct的用法-qualifiedByName,用的都是这里的类。
1 在Mapper中定义方法如下:Number转Number
@Mapping(source = "pm25", target = "pm25", numberFormat = "0.00")//不生效
AreaVO areaPO2areaVO(AreaPO areaPO);
2 运行后,Impl类如下:
package com.weather.weatherexpert.common.model.mapper;
import com.weather.weatherexpert.common.model.po.AreaPO;
import com.weather.weatherexpert.common.model.vo.AreaVO;
public class ConvertMapperImpl implements ConvertMapper {
public ConvertMapperImpl() {
}
public AreaVO areaPO2areaVO(AreaPO areaPO) {
if (areaPO == null) {
return null;
} else {
AreaVO areaVO = new AreaVO();
areaVO.setPm25(areaPO.getPm25());
areaVO.setCityName(areaPO.getCityName());
areaVO.setHaveAir(areaPO.getHaveAir());
return areaVO;
}
}
}
并没有小数位格式的代码
输出:
JSON.toJSONString(areaVO):{"cityName":"忻州","haveAir":1,"pm25":1.256879}
3 Mapper修改如下:Number转String
@Mapping(source = "pm25", target = "pm25Str", numberFormat = "0.00")//生效,Number转String或String转Number才可以,如果是Number转Number则无效
AreaVO areaPO2areaVO(AreaPO areaPO);//Number转String
4 运行后,Impl类如下:
package com.weather.weatherexpert.common.model.mapper;
import com.weather.weatherexpert.common.model.po.AreaPO;
import com.weather.weatherexpert.common.model.vo.AreaVO;
import java.text.DecimalFormat;
public class ConvertMapperImpl implements ConvertMapper {
public ConvertMapperImpl() {
}
public AreaVO areaPO2areaVO(AreaPO areaPO) {
if (areaPO == null) {
return null;
} else {
AreaVO areaVO = new AreaVO();
if (areaPO.getPm25() != null) {
areaVO.setPm25Str((new DecimalFormat("0.00")).format(areaPO.getPm25()));
}
areaVO.setCityName(areaPO.getCityName());
areaVO.setHaveAir(areaPO.getHaveAir());
areaVO.setPm25(areaPO.getPm25());
return areaVO;
}
}
}
有小数位格式的代码,这里是Number转String
扫描二维码关注公众号,回复:
4795958 查看本文章
输出:
{"cityName":"忻州","haveAir":1,"pm25":1.256879,"pm25Str":"1.26"}
5 Mapper修改如下:String转Number
@Mapping(source = "pm10Str", target = "pm10", numberFormat = "0.00")//String转Number
AreaVO areaPO2areaVO(AreaPO areaPO);
6 测试:
7 运行后,Impl类如下:
package com.weather.weatherexpert.common.model.mapper;
import com.weather.weatherexpert.common.model.po.AreaPO;
import com.weather.weatherexpert.common.model.vo.AreaVO;
import java.text.DecimalFormat;
import java.text.ParseException;
public class ConvertMapperImpl implements ConvertMapper {
public ConvertMapperImpl() {
}
public AreaVO areaPO2areaVO(AreaPO areaPO) {
if (areaPO == null) {
return null;
} else {
AreaVO areaVO = new AreaVO();
try {
if (areaPO.getPm10Str() != null) {
areaVO.setPm10((new DecimalFormat("0.00")).parse(areaPO.getPm10Str()).doubleValue());
}
} catch (ParseException var4) {
throw new RuntimeException(var4);
}
areaVO.setCityName(areaPO.getCityName());
areaVO.setHaveAir(areaPO.getHaveAir());
areaVO.setPm25(areaPO.getPm25());
return areaVO;
}
}
}
输出:
JSON.toJSONString(areaVO2):{"cityName":"忻州","haveAir":1,"pm10":12.12315464,"pm25":1.256879}
发现pm10属性的小数位并没有按照预期格式化!
public static void main(String[] args) {
try {
Double d1 = new DecimalFormat("0.00").parse("12.1321312").doubleValue();
System.out.println("d1:" + d1);//12.1321312
Double d2 = new Double(new DecimalFormat("0.00").format(Double.parseDouble("12.1321312")));
System.out.println("d2:" + d2);//12.13
} catch (ParseException e) {
e.printStackTrace();
}
}
这String转Number时小数位并没有按预期转换,不知是哪里写错了,还是哪里写的不正确!