封装一个resultMap,映射实体类public class EquipmentVo extends RegionalEquipment { private List<EquipmentDataVo> equipmentDataVoList; private List<Map<String,Object>> maps; public List<Map<String, Object>> getMaps() { return maps; } public void setMaps(List<Map<String, Object>> maps) { this.maps = maps; } public List<EquipmentDataVo> getEquipmentDataVoList() { return equipmentDataVoList; } public void setEquipmentDataVoList(List<EquipmentDataVo> equipmentDataVoList) { this.equipmentDataVoList = equipmentDataVoList; } },其中map中的键不固定,随着不同的SQL语句而改变,数据源是两个表,存在一对多关系
时间: 2024-03-02 09:48:45 浏览: 69
js解析json读取List中的实体对象示例
您可以封装一个resultMap来实现对EquipmentVo实体类的映射,具体实现可以参考以下代码:
```xml
<resultMap id="equipmentVoMap" type="EquipmentVo">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="model" column="model"/>
<result property="regionId" column="region_id"/>
<!-- 一对多关系映射 -->
<collection property="equipmentDataVoList" ofType="EquipmentDataVo">
<result property="id" column="e_id"/>
<result property="name" column="e_name"/>
<result property="value" column="e_value"/>
<result property="equipmentId" column="equipment_id"/>
</collection>
<!-- 动态的map映射 -->
<collection property="maps" ofType="java.util.HashMap" resultMap="dynamicMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="model" property="model"/>
<result column="region_id" property="regionId"/>
<result column="e_id" property="equipmentId"/>
<result column="e_name" property="equipmentName"/>
<result column="e_value" property="equipmentValue"/>
</collection>
</resultMap>
<resultMap id="dynamicMap" type="java.util.HashMap">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="model" property="model"/>
<result column="region_id" property="regionId"/>
<result column="e_id" property="equipmentId"/>
<result column="e_name" property="equipmentName"/>
<result column="e_value" property="equipmentValue"/>
</resultMap>
```
其中,equipmentDataVoList是对应的一对多关系,maps是动态的map映射,ofType属性指定集合中元素的类型,resultMap属性指定使用的resultMap。动态的map映射需要额外定义一个resultMap,其中的property对应的是map中的键,column对应的是数据库表中的列名。在SQL语句中,需要使用AS关键字来指定map中的键名,例如:
```sql
SELECT e.*, d.id AS e_id, d.name AS e_name, d.value AS e_value
FROM equipment e
LEFT JOIN equipment_data d ON e.id = d.equipment_id
```
注意,当使用动态的map映射时,需要将实体类中对应的属性置为null,否则会出现重复映射的问题。
阅读全文