在很多场合,我们需要对表中的数据对递归查询。如以下情况:
菜单分类中,我们往往需要由一级菜单获得对应的子菜单。
| id | name | pid |
|---|---|---|
| 1 | 图书 | 0 |
| 2 | 服装 | 0 |
| 3 | 儿童读物 | 1 |
| 4 | 杂志 | 1 |
| 5 | 卡通 | 3 |
| 6 | 睡前故事 | 3 |
我们希望得到的结果为,由图书可以查到:
{ 图书: [ 杂志,
儿童读物:[卡通,睡前故事]
}
在类似上述具有依赖关系的查询时,我们将父子依赖关系抽象成以下字段:
| col | col_parent |
|---|---|
| value1 | value2 |
| value2 | value3 |
由col中的value1查询到col_parent值为value2;
再由value2作为col查询,查询到value3。
注:父依赖或子依赖只是称呼不同而已。
value1的所有父依赖value1-->value2-->value3
value2的所有父依赖value2-->value3
针对上述问题,本文提出两种解决方案。
使用mybatis中的collection标签
优点:框架已经封装好了,不用深入实现原理。
缺点:返回结果的结构固定,不能灵活处理。对结构的解析也较复杂。扩展性差
只需根据一条查询语句select col_parent from table_name where col=#{col}及自己的代码即可
优点:灵活性高。返回结构可扩展
难点:需要理解实现原理。
demo说明
对于mysql中如下数据表结构:
| id | code | parent_code |
|---|---|---|
| ... | ... | ... |
目标:我们要求通过code找到左右父code(包括祖辈code)
核心代码(其他实现代码不做展示)
dao
@Mapperpublic interface RelationTreeDao { List<RelationTreeDTO> selectAllParentsByCode(String code);
dto
public class RelationTreeDTO { private String code; private String parentCode; private List<RelationTreeDTO> parentCodeList; //List嵌套本身,装父节点信息
// getter and setter}
mapper.xml
<?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.***.dao.RelationTreeDao">
<!---->
<resultMap id="relationTreeMap" type="com.***.dto.RelationTreeDTO">
<result column="task_code" property="taskCode" jdbcType="VARCHAR" javaType="String"/>
<result column="parent_code" property="parentCode" jdbcType="VARCHAR" javaType="String"/>
<collection column="parent_code" property="parentCodeList"
select="selectAllParentsByCode">
</collection>
</resultMap>
<!-- relation表中选择所有的父节点 -->
<select id="selectAllParentsByCode" parameterType="java.lang.String" resultMap="relationTreeMap">
SELECT
`code`,`parent_code`
FROM
`relation`
WHERE
`code` = #{code}
AND
`parent_code` is not NULL </select></mapper>
说明:
RelationTreeDTO作为查询结果的映射对象,其中需要定义自嵌套的List
mapper中的select也为简单查询,但是其映射结果resultMap中有collection标签。会将column="parent_code"再作为参数#{code}循环查询。
结果:
relationTreeDao.selectAllParentsByCode("yourCode");查询结果将会以RelationTreeDTO对象返回,若有多条父依赖,将显示在List的嵌套中。
[
{ "code": ***, "parentCode": ***, "parentCodeList": [
{ "code": ***, "parentCode": ***, "parentCodeList": []
},
...
]
}
]
对于上述结果,我们往往需要进一步获取有用信息。如只需要一个List:
[code, parentCode, parentCode, parentCode,...]
由于RelationTreeDTO是一个树结构,这就涉及到树的遍历。在此,以树的深度优先搜索算法,获得上述list。
/**
* description:深度优先搜索DTO中的所有父节点
* @author wanghongbing whbing1991@gmail.com
* @param treeDTO RelationTreeDTO待解析的树结构对象
* @return list [0]存code, [1]开始存parents
* 一定会有一个父节点,list长度>=2
*/
@Override public List<String> depthFirst(RelationTreeDTO treeDTO) { //list [0]存code, [1]开始存parents
List<String> list = new ArrayList<>(); list.add(treeDTO.getCode()); //list[0]
ArrayDeque<RelationTreeDTO> stack = new ArrayDeque();
stack.push(treeDTO); while (!stack.isEmpty()){
RelationTreeDTO node =stack.pop(); list.add(node.getParentCode()); //获取嵌套节点
List<RelationTreeDTO> parents = node.getParentCodeList(); if(parents !=null && parents.size() >0){ for (int i = parents.size()-1; i >=0 ; i--) {
stack.push(parents.get(i));
}
}
} return list;
}
至此,该方式级联查询结束。
上述实现,collection结果为固定的树结构,在解析时,要使用算法(如DFS)获取树种的节点信息。虽然在mapper查询时,一次性获得了级联结构,后续解析仍然复杂。下面介绍推荐方式。
dao
@Mapperpublic interface RelationDao { List<TaskRelationDTO> selectParentByCode(String code); // 其他表
List<TaskRelationDTO> selectOtherParentByCode(String code);
}
dto(或entity,如数据库对应的话)
public class TaskRelationDTO {
private String code; private String parentCode; // getter and setter}
mapper.xml(假设有relation表和other_relation表,其字段相同。两个表完全为了展示扩展)
<?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" ><!-- recommended not modified but can be added --><mapper namespace="com.***.dao.RelationDao">
<!--tag-->
<resultMap id="relationMap" type="com.***.dto.RelationDTO">
<result column="code" property="code" jdbcType="VARCHAR" javaType="String"/>
<result column="parent_code" property="parentCode" jdbcType="VARCHAR" javaType="String"/>
</resultMap>
<!-- a_relation表中选择当前code的父节点 -->
<select id="selectParentByCode" parameterType="java.lang.String" resultMap="relationMap">
SELECT
`code`,`parent_code`
FROM
`relation`
WHERE
`code` = #{code}
AND
`parent_code` is not NULL </select>
<!-- other_relation表中选择当前code的父节点 -->
<select id="selectOtherParentByCode" parameterType="java.lang.String" resultMap="relationMap">
SELECT
`code`,`parent_code`
FROM
`other_relation`
WHERE
`code` = #{code}
AND
`parent_code` is not NULL </select></mapper>
说明:上述查询仅为最简单的sql查询,我们将递归查询写在业务方法中。
service
/**
*
* @param code 待查找父任务的子任务
* @return 返回的list.size()>=2 list[0]当前code,[1]以后去重后的无序parentsCode
* 如:[tag-depend-2, path-depend-0-p, path-depend-2, tag-depend-0, path-depend-0]
*/
@Override public List<String> getAllParentsByCode(String code) { List<String> list = new ArrayList<>();
Set<String> parentSet = new HashSet<>();
ArrayDeque<String> stack = new ArrayDeque();
int count = 0; final int MAX_LOOP_COUNT = 50; // 初始化stack,将code放入stack
stack.push(code); // 将code加入list。如果最终list.isEmpty(),表明没有父节点,将其清空。故list长度最短为2
list.add(code); while (!stack.isEmpty()){ // 如果入栈次数太多,表明可能出现环形依赖。强行终止
if(count++ > MAX_LOOP_COUNT){
LOGGER.error("code为["+code+"]任务其父任务超过"+MAX_LOOP_COUNT+"个,请检查是否有环形依赖"); list.addAll(parentSet); // 仅有taskCode,无parentCode时,将list清空
if(list.size() == 1){ list.clear();
} return list;
}
String childCode = stack.pop();/**
可能会出现两个表交叉依赖情况,故有otherRelation
*/
List<RelationDTO> relation =relationDao.selectTagParentByCode(childCode); List<TaskRelationDTO> otherRelation =relationDao.selectOtherParentByCode(childCode); // 从relation表中查找pop()元素的父任务,将其加入stack
if(!relation.isEmpty()){ for (int i = 0; i < relation.size(); i++) {
String parent = relation.get(i).getParentCode(); //这个parent是需要的,同时要将其放入stack
parentSet.add(parent);
stack.push(parent);
}
} // 从otherRelation表中查找pop()元素的父任务,将其加入stack
if(!otherRelation.isEmpty()){ for (int i = 0; i < otherRelation.size(); i++) {
String parent = otherRelation.get(i).getParentCode(); //这个parent是需要的,同时要将其放入stack
parentSet.add(parent);
stack.push(parent);
}
}
} list.addAll(parentSet); // 仅有taskCode,无parentCode时,将list清空
if(list.size() == 1){ list.clear();
} return list;
}
原理
说明:上述原理,使用栈(递归亦可,所谓递归,无非进栈出栈)来循环查询。初始化时,将待查询的code入栈,第一次查询时,该code出栈,作为参数查询,如有查询结果(一条或多条),将查询到的结果进栈(放入栈中,下次循环计就可以取出作为参数输入了!)。
如上述进行了两个表的查询,灵活。
作者:王小禾
链接:https://www.jianshu.com/p/0c8393f21e73
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3373/viewspace-2821122/ ,如需转载,请注明出处,否则将追究法律责任。