需求是:递归查询资源
1.资源类 EntityBaseResource:
public final class EntityBaseResource { private Long resID = 0l; private String resName = ""; private String urlPath = ""; private String parentResID = ""; private String iconClass = "fa fa-circle-o"; private short level = 1; private String resType = ""; private String permission = ""; private String belongTo = ""; private String isPublished = ""; private short seq = 1; private String isValid = "1"; // 子资源 private List<EntityBaseResource> children; public final Long getResID() { return resID; } public final void setResID(Long resID) { this.resID = resID; } public final String getResName() { return resName; } public final void setResName(String resName) { this.resName = resName == null ? Constant.BLANK : resName.trim(); } public final String getUrlPath() { return urlPath; } public final void setUrlPath(String urlPath) { this.urlPath = urlPath == null ? Constant.BLANK : urlPath.trim(); } public final String getParentResID() { return parentResID; } public final void setParentResID(String parentResID) { this.parentResID = parentResID == null ? Constant.BLANK : parentResID.trim(); } public final String getIconClass() { return iconClass; } public final void setIconClass(String iconClass) { this.iconClass = iconClass == null ? Constant.BLANK : iconClass.trim(); } //省略get/set方法
2.DAO
import java.util.List; import org.apache.ibatis.annotations.Param; import com.csget.entity.base.EntityBaseResource; public interface DaoBaseResource { int deleteByPrimaryKey(Long resID); int insert(EntityBaseResource record); EntityBaseResource selectByPrimaryKey(Long resID); List<EntityBaseResource> selectRecursionRes(@Param("resID") Long resID, @Param("belongTo") String belongTo); int updateByPrimaryKey(EntityBaseResource record); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.csget.dao.base.DaoBaseResource"> <!-- 基础属性 --> <resultMap id="BaseResultMap" type="com.csget.entity.base.EntityBaseResource"> <id column="resID" jdbcType="BIGINT" property="resID" /> <result column="resName" jdbcType="VARCHAR" property="resName" /> <result column="urlPath" jdbcType="VARCHAR" property="urlPath" /> <result column="parentResID" jdbcType="VARCHAR" property="parentResID" /> <result column="iconClass" jdbcType="VARCHAR" property="iconClass" /> <result column="level" jdbcType="TINYINT" property="level" /> <result column="resType" jdbcType="VARCHAR" property="resType" /> <result column="permission" jdbcType="VARCHAR" property="permission" /> <result column="seq" jdbcType="TINYINT" property="seq" /> <result column="isValid" jdbcType="CHAR" property="isValid" /> </resultMap> <!-- 递归资源,继承基础属性 --> <resultMap id="resAll" extends="BaseResultMap" type="com.csget.entity.base.EntityBaseResource"> <collection property="children" ofType="resAll" column="{resID=resID,belongTo=belongTo}" select="selectRecursionRes" /> </resultMap> <!-- 递归资源 --> <select id="selectRecursionRes" resultMap="resAll"> SELECT resID, resName, urlPath, parentResID, iconClass, belongTo FROM t_base_resource WHERE parentResID=#{resID} and belongTo=#{belongTo} and isValid='1' ORDER BY seq ASC </select> </mapper>
递归查询时多个参数参数,会自动调用查询结果中的字段值
同时递归查询,resultMap采用的继承属性 ,这样可以避免一些不必要的查询,例如如果你只需要查询一条记录,不需要查询它下面的子集。