原创

Java-实体转换-实体中的字符串字段转层list-String转BigInteger


1.数组字符串转成list结合
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.getConverterFactory().registerConverter("employeeListConverter", new JsonConfigConvert<List<TsServiceDetailEmployee>>());
mapperFactory.classMap(TsMaintainIntentServiceDetail.class, TsMaintainIntentServiceDetailVo.class) .fieldMap("employeeList", "employeeList").converter("employeeListConverter").add().byDefault().register();

2. String 转 BigInteger
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.getConverterFactory().registerConverter(new ConvertStringToBigIntegerConverter());
return mapperFactory.getMapperFacade().map(intentServiceDetailVo, TsMaintainIntentServiceDetail.class);
private class ConvertStringToBigIntegerConverter extends BidirectionalConverter<String, BigInteger> {

@Override
public BigInteger convertTo(String s, Type<BigInteger> type, MappingContext mappingContext) {
return new BigInteger(s);//String 转 BigInteger
}

@Override
public String convertFrom(BigInteger bigInteger, Type<String> type, MappingContext mappingContext) {
return String.valueOf(bigInteger);
}
}

private class JsonConfigConvert<T> extends BidirectionalConverter<T, String> {
@Override
public String convertTo(T source, Type<String> destinationType, MappingContext mappingContext) {
return JSON.toJSONString(source);
}

@Override
public T convertFrom(String source, Type<T> destinationType, MappingContext mappingContext) {
return JSON.parseObject(source, destinationType.getRawType());
}
}
正文到此结束
本文目录