我有一个像这样的结果集…
+--------------+--------------+----------+--------+
| LocationCode | MaterialCode | ItemCode | Vendor |
+--------------+--------------+----------+--------+
| 1 | 11 | 111 | 1111 |
| 1 | 11 | 111 | 1112 |
| 1 | 11 | 112 | 1121 |
| 1 | 12 | 121 | 1211 |
+--------------+--------------+----------+--------+我需要一个对象(最终要转换为LocationCode )如下:List<Location>,其中Location类中嵌套对象的层次结构是..
Location.class
LocationCode
List<Material>
Material.class
MaterialCode
List<Item>
Item.class
ItemCode
Vendor这与结果集相对应,其中1个地点有2个物料,1个物料(11)有2个物料,1个物料(111)有2个供应商。
我该如何实现这一点?我以前用过AliasToBeanResultTransformer,但我怀疑它在这种情况下会有帮助。
发布于 2013-06-17 21:29:41
我不认为有一种巧妙的方法来做这种映射。我只是使用嵌套循环和自定义逻辑来决定何时开始构建下一个位置、材料、项目等等。
类似于下面的伪代码:
while (row = resultSet.next()) {
if (row.locationCode != currentLocation.locationCode) {
currentLocation = new Location(row.locationCode)
list.add(currentLocation)
currentMaterial = null
} else if (currentMaterial == null ||
row.materialCode != currentMaterial.materialCode) {
currentMaterial = new Material(row.materialCode)
currentLocation.add(currentMaterial)
} else {
currentMaterial.add(new Item(row.itemCode, row.vendorCode))
}
}https://stackoverflow.com/questions/17148535
复制相似问题