今天突然后个功能,根据id查个多层的树状数据
用到了resultMap
期望返回如下的json示例
{
"id": "1285114537198723074",
"building": 6,
"coveredArea": "2123",
"floorArea": "3000",
"households": 1000,
"totalInvestment": "1000",
"state": 1,
"onStreamTime": "2020-07-20",
"completionTime": "2021-07-20",
"communityProgressList": [
{
"id": "1291205587579387906",
"communityId": "1285114537198723074",
"progressDate": "2000-02-01",
"summary": "测试",
"plan": "test",
"createTime": null,
"creatBy": null,
"updateTime": null,
"updateBy": null,
"communityPictureList": [
{
"id": "1291205574388301825",
"progressId": "1291205587579387906",
"fileName": "1596682372556.png",
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null
},
{
"id": "1291205574862258178",
"progressId": "1291205587579387906",
"fileName": "1596682373126.png",
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null
},
{
"id": "1291205576267350017",
"progressId": "1291205587579387906",
"fileName": "1596682373240.jpg",
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null
}
],
"pictureIds": null
},
一共有三层数据
顶层对外的是一条
xml
<resultMap id="communityMap" type="org.jeecg.modules.gaoLing.model.CommunityVewModel">
<id property="id" column="cid"/>
<result property="address" column="address"/>
<result property="building" column="building"/>
<result property="coveredArea" column="covered_area"/>
<result property="floorArea" column="floor_area"/>
<result property="households" column="households"/>
<result property="totalInvestment" column="total_investment"/>
<result property="state" column="state"/>
<result property="onStreamTime" column="on_stream_time" javaType="java.sql.Date" jdbcType="DATE"/>
<result property="completionTime" column="completion_time" javaType="java.sql.Date" jdbcType="DATE"/>
<collection property="communityProgressList"
ofType="org.jeecg.modules.gaoLing.entity.CommunityProgress">
<id property="id" column="pid" />
<result property="communityId" column="community_id"/>
<result property="progressDate" column="progress_date" javaType="java.sql.Date" jdbcType="DATE"/>
<result property="summary" column="summary"/>
<result property="plan" column="plan"/>
<collection property="communityPictureList"
ofType="org.jeecg.modules.gaoLing.entity.CommunityPicture">
<id property="id" column="pcid" />
<result property="progressId" column="progress_id"/>
<result property="fileName" column="file_name"/>
</collection>
</collection>
</resultMap>
<select id="selectByCommunityId" resultMap="communityMap">
SELECT
c.id cid, address, building, covered_area, floor_area,
households, total_investment, state, on_stream_time, completion_time,
p.id pid, community_id, summary, plan,
pc.id pcid, progress_id, file_name
FROM
wechat_gaoling_community c
LEFT JOIN wechat_gaoling_community_progress p ON c.id = p.community_id
LEFT JOIN wechat_gaoling_community_picture pc ON p.id = pc.progress_id
WHERE c.id = #{id}
ORDER BY pc.create_time DESC
</select>
其中简单删除了一些敏感数据,但是字段对应什么的是没有问题,若有遗漏,大家可以忽略
完事后发现报错
nested exception is org.apache.ibatis.exceptions.TooManyResultsException:
Expected one result (or null) to be returned by selectOne(), but found: 6
就是说resultMap并没正确映射
解决:
后来发现,即便是你能确定该接口仅返回一条顶层数据,使用了resultMap,也必须返回list
也就是将
public interface CommunityMapper extends BaseMapper<Community> {
CommunityVewModel selectByCommunityId(@Param("id") String id);
}
改为
public interface CommunityMapper extends BaseMapper<Community> {
List<CommunityVewModel> selectByCommunityId(@Param("id") String id);
}
如此就正确了