字典数据映射
class Flight {
String airportIata;
}
class FlightDto {
// 默认开启缓存并且存活时间为60s,根据业务需要可以灵活配置
@DictMapping(key="IATA", resolver=AirportDictDataResolver.class)
@Mapping(field="airportIata")
String airport;
}
// 此处仅作为示例,实际使用时一般通过依赖数据服务获取数据,然后将结果转换为Map
public class AirportDictDataResolver implements DictDataResolver {
@Override
public Map<Object, Object> resolve(String key) {
if (key.equals("IATA")) {
Map<Object, Object> data = new HashMap<>();
data.put("TAO", "青岛");
data.put("DLC", "大连");
data.put("SHE", "沈阳");
return data;
} else {
Map<Object, Object> data = new HashMap<>();
data.put("ZSQD", "青岛");
data.put("ZYTL", "大连");
data.put("ZYTX", "沈阳");
return data;
}
}
}
FlightDto flightDto = new FlightDto("青岛")
Flight flight = BeanUtils.copyProperties(flightDto, Flight.class);
System.out.println(flight.getAirportIata()) // "TAO"Last updated