如
Department {
Department parentDepartment;
}
如果设置查询映射
<!--查看指定部门-->
<select id="selectDepartmentByPrimaryKey" resultMap="departmentResult" parameterClass="integer">
select DEPARTMENTID, SCHOOLID, PDEPARTMENTID,DEPARTMENTNAME, DEPARTMENTDESCRIBE, DEPARTMENTCODE
from S_DEPARTMENT
where DEPARTMENTID = #departmentId#
</select>
<resultMap id="departmentResult" class="org.dreamfly.core.model.Department">
<result property="departmentId" column="DEPARTMENTID"/>
<result property="school" column="SCHOOLID" select="selectSchoolByPrimaryKey"/>
<result property="pdepartment" column="PDEPARTMENTID" select="[color=red]selectDepartmentByPrimaryKey[/color]"/>
<result property="departmentName" column="DEPARTMENTNAME"/>
<result property="departmentDescribe" column="DEPARTMENTDESCRIBE"/>
<result property="departmentCode" column="DEPARTMENTCODE" />
</resultMap>
这样会导致查询进入死循环.查询会一直查找他的父部门类
解决方案
<resultMap id="departmentResult" class="org.dreamfly.core.model.Department">
<result property="departmentId" column="DEPARTMENTID"/>
<result property="school" column="SCHOOLID" select="selectSchoolByPrimaryKey"/>
<result property="pdepartment" column="PDEPARTMENTID" select="[color=red]selectParentDepartmentByPrimaryKey[/color]"/>
<result property="departmentName" column="DEPARTMENTNAME"/>
<result property="departmentDescribe" column="DEPARTMENTDESCRIBE"/>
<result property="departmentCode" column="DEPARTMENTCODE" />
</resultMap>
加上一个
<!--查询上级部门,不查询上级部门的上级部门-->
<select id="selectParentDepartmentByPrimaryKey" resultClass="org.dreamfly.core.model.Department" parameterClass="integer"
cacheModel="otherCodeCache">
select DEPARTMENTID, DEPARTMENTNAME, DEPARTMENTDESCRIBE, DEPARTMENTCODE
from S_DEPARTMENT
where DEPARTMENTID = #departmentId#
</select>
Department {
Department parentDepartment;
}
如果设置查询映射
<!--查看指定部门-->
<select id="selectDepartmentByPrimaryKey" resultMap="departmentResult" parameterClass="integer">
select DEPARTMENTID, SCHOOLID, PDEPARTMENTID,DEPARTMENTNAME, DEPARTMENTDESCRIBE, DEPARTMENTCODE
from S_DEPARTMENT
where DEPARTMENTID = #departmentId#
</select>
<resultMap id="departmentResult" class="org.dreamfly.core.model.Department">
<result property="departmentId" column="DEPARTMENTID"/>
<result property="school" column="SCHOOLID" select="selectSchoolByPrimaryKey"/>
<result property="pdepartment" column="PDEPARTMENTID" select="[color=red]selectDepartmentByPrimaryKey[/color]"/>
<result property="departmentName" column="DEPARTMENTNAME"/>
<result property="departmentDescribe" column="DEPARTMENTDESCRIBE"/>
<result property="departmentCode" column="DEPARTMENTCODE" />
</resultMap>
这样会导致查询进入死循环.查询会一直查找他的父部门类
解决方案
<resultMap id="departmentResult" class="org.dreamfly.core.model.Department">
<result property="departmentId" column="DEPARTMENTID"/>
<result property="school" column="SCHOOLID" select="selectSchoolByPrimaryKey"/>
<result property="pdepartment" column="PDEPARTMENTID" select="[color=red]selectParentDepartmentByPrimaryKey[/color]"/>
<result property="departmentName" column="DEPARTMENTNAME"/>
<result property="departmentDescribe" column="DEPARTMENTDESCRIBE"/>
<result property="departmentCode" column="DEPARTMENTCODE" />
</resultMap>
加上一个
<!--查询上级部门,不查询上级部门的上级部门-->
<select id="selectParentDepartmentByPrimaryKey" resultClass="org.dreamfly.core.model.Department" parameterClass="integer"
cacheModel="otherCodeCache">
select DEPARTMENTID, DEPARTMENTNAME, DEPARTMENTDESCRIBE, DEPARTMENTCODE
from S_DEPARTMENT
where DEPARTMENTID = #departmentId#
</select>
本文介绍在使用MyBatis进行关联查询时如何避免死循环的问题,通过调整查询映射配置实现正确获取部门及其父部门信息而不陷入无限递归。
3733

被折叠的 条评论
为什么被折叠?



