Mybatis自查询递归查找子
先看一下数据库
主键id,名称product_code,父parent,和kind
设计菜单类
setter,getter
dao
public interface productmapper {
list<tproductkindrelationdto> getproductkindrelationdto();
}
mapper.xml
<mapper namespace="com.adv.biz.dao.productmapper" >
<resultmap id="baseresultmap" type="com.adv.biz.dto.tproductkindrelationdto" >
<id column="id" property="id" jdbctype="integer" />
<result column="product_code" property="productcode" jdbctype="varchar" />
<result column="parent" property="parent" jdbctype="varchar" />
<result column="kind" property="kind" jdbctype="varchar" />
<collection property="childlist" oftype="com.adv.biz.entity.tproductkindrelation" column="product_code" select="findbyparent">
</collection>
</resultmap>
<select id="getproductkindrelationdto" resultmap="baseresultmap" >
select t.id,t.kind,t.parent,t.product_code from t_product_kind_relation t where t.parent is null or t.parent = ''
</select>
<select id="findbyparent" resulttype="com.adv.biz.dto.tproductkindrelationdto">
select t.id,t.kind,t.parent,t.product_code from t_product_kind_relation t where t.parent=#{product_code}
</select>
</mapper>
其中findbyparent会查询到所有的父菜单,
而在getproductkindrelationdto中我们定义了如何去查找子菜单,调用findbyparent