针对Mybatis一对多查询,创建空对象的问题
提示:Mybatis针对一对多查询,创建空对象的问题
前言
大概内容:
Mybatis在一对多查询的时候遇到的问题,即使子节点无数据,还是创建了一个空对象,这是找到的解决方法,很管用。
提示:在所有的子节点加入 notNullColumn标签,指定的参数有一个为null就不创建子对象
样例代码
<!-- 监测成果左侧菜单树 一级节点 根-->
<resultMap id="MonitorGraphTreeResultMap" type="com.tjgeo.tpp.monitor.vo.MonitorTreeVo">
<id property="id" column="aId"></id>
<result property="text" column="aName"></result>
<result property="treeType" column="aType"></result>
<!--二级节点 分支线路-->
<collection property="children" ofType="com.tjgeo.tpp.monitor.vo.MonitorTreeVo" notNullColumn="bId">
<id property="id" column="bId"></id>
<result property="text" column="bName"></result>
<result property="treeType" column="bType"></result>
<!--一三级节点 服务周期-->
<collection property="children" ofType="com.tjgeo.tpp.monitor.vo.MonitorTreeVo" notNullColumn="cId">
<id property="id" column="cId"></id>
<result property="text" column="cName"></result>
<result property="treeType" column="cType"></result>
</collection>
</collection>
</resultMap>
<select id="getMonitorGraphTree" resultMap="MonitorGraphTreeResultMap">
select
a.id as aId,
a.line_full_name as aName,
'aType' as aType,
b.id as bId,
b.line_full_name as bName,
'bType' as bType,
c.id as cId,
concat_ws("~", date_format(c.service_period_start, '%Y.%m'),date_format(c.service_period_end, '%Y.%m')) as cName,
'cType' as cType
from base_line a
left join base_line b on a.id = b.parent_id
left join monitor_perpetual_monitor_item c on c.item_line_id = b.id
</select>