在返回json数据时常常需要返回树形的结构,自己去写递归来构建树的话又太麻烦了。
下面介绍一种使用mybatis来返回树形结构的好方法
表结构展示
表里面需要记录父级Id
MyBatis一对多collection用法
第一次用过org_id找到符合的数据,返回给orgTreeMap,关键就在这个resultMap里面,有一个集合,集合又去调用了getOrgByParentId方法,该方法把查询的到的org_id,当作parent_id来查找,getOrgByParentId又返回要给orgTreeMap这样就形成了递归查询。
<resultMap id="orgTreeMap" type="com.ylz.api.vo.OrgVO"> <result column="org_id" jdbcType="VARCHAR" property="orgId"/> <result column="org_name" jdbcType="VARCHAR" property="orgName"/> <result column="parent_id" jdbcType="VARCHAR" property="parentId"/> <collection column="org_id" property="children" ofType="com.ylz.api.vo.OrgVO" select="getOrgByParentId" > </collection> </resultMap> <select id="getOrgTree" parameterType="java.lang.String" resultMap="orgTreeMap"> select o.org_id, o.org_name, o.parent_id from org_table@allcenter_db o where o.org_id = #{orgId} </select> <select id="getOrgByParentId" parameterType="java.lang.String" resultMap="orgTreeMap"> select o.org_id, o.org_name, o.parent_id from org_table@allcenter_db o where o.parent_id = #{orgId} </select>
返回类
@JsonIgnoreProperties(ignoreUnknown = true) public class OrgVO { private String orgId; private String orgName; private String parentId;//父机构id private List<OrgVO> children; }
调用
public String orgTree(OverviewPojo overviewPojo) { OrgVO orgVO = userMapper.selectOrgInfoByUserId(overviewPojo.getUserId()); List<OrgVO> orgTree = orgMapper.getOrgTree(orgVO.getOrgId()); return ReturnJsonFormatUntil.json(true, AnalysisJson.parseListWithGson(orgTree)); }
返回树形结构的json数据
JDK8返回树形结构
public class 树型关系 { public static void main(String[] args) { //初始化数据 NodeTree NodeTree1 = new NodeTree("xx1",1,-1); NodeTree NodeTree2 = new NodeTree("xx2",2,-1); NodeTree NodeTree3 = new NodeTree("xx3",3,-1); NodeTree NodeTree4 = new NodeTree("xx4",4,1); NodeTree NodeTree5 = new NodeTree("xx5",5,2); NodeTree NodeTree6 = new NodeTree("xx6",6,3); NodeTree NodeTree7 = new NodeTree("xx7",7,4); NodeTree NodeTree8 = new NodeTree("xx8",8,7); NodeTree NodeTree9 = new NodeTree("xx9",9,5); List<NodeTree> list = new ArrayList<>(); list.add(NodeTree1); list.add(NodeTree2); list.add(NodeTree3); list.add(NodeTree4); list.add(NodeTree5); list.add(NodeTree6); list.add(NodeTree7); list.add(NodeTree8); list.add(NodeTree9); List<NodeTree> collect = list.stream() //找到所有最顶级的节点 .filter(e -> e.pid.equals(-1)) //查找顶级节点的所有子节点 .map(e -> covert(e, list)) .collect(Collectors.toList()); System.out.println(collect); } /** * 返回当前节点的以及所有子节点 * @param nodeTree 当前节点 * @param nodeTreeList 所有节点 * @return */ public static NodeTree covert(NodeTree nodeTree, List<NodeTree> nodeTreeList) { List<NodeTree> children = nodeTreeList.stream() //过滤出当前节点的下一级子节点 .filter(subNodeTree -> subNodeTree.getPid().equals(nodeTree.getId())) //递归查找 .map(subNodeTree -> covert(subNodeTree, nodeTreeList)).collect(Collectors.toList()); nodeTree.setChildren(children); return nodeTree; } } @Data @NoArgsConstructor class NodeTree { //名称 String name; //id Integer id; //父id Integer pid; //子节点 List<NodeTree> children; public NodeTree(String name, Integer id, Integer pid) { this.name = name; this.id = id; this.pid = pid; } }