一:代码处理
1.多表父子级关系利用流来处理

private List<AddressInfo>  children;
List<AddressInfo> collect1 = addressInfo.stream().map(m1 -> {
List<AddressInfo> collect = addressInfo.stream().filter(
        m2 -> Objects.equals(m1.getId(), m2.getPId())).collect(Collectors.toList());
m1.setChildren(collect);
return m1;
 }).collect(Collectors.toList());
说明:m1 父级信息   m2 子级信息     

2单表父子级关系处理

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    /**
     * 3.循环处理全部数据,转为父子级对象
     */
    for (AddressInfo vo : addressInfo) {
        Map<String, Object> map = new HashMap<String, Object>(8);
        if (vo.getPId() == 0) {
            map.put("children", getChildren(addressInfo, vo.getId()));
            map.put("pId", vo.getPId());
            存放子级数据
            list.add(map);
        }
    }
    PageInfo<AddressInfo> pageInfo = new PageInfo<>(addressInfo);
    return success(list, pageInfo.getTotal());
}

public List<Map<String, Object>> getChildren(List<AddressInfo> data, Integer wzflid) {
    /**
     * 1.对数据校验处理
     */
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    if (data == null || data.size() == 0 || wzflid == null) {
        return list;
    }
    /**
     * 2.遍历匹配父子级关系数据
     */
    for (AddressInfo vo : data) {
        Map<String, Object> map = new HashMap<String, Object>(8);
        if (wzflid.equals(vo.getPId())) {
            map.put("pId", vo.getPId());
            
            list.add(map);
        }
    }
    return list;
}

二:mysql 查询语句处理

1、创建接收类型(实体类有 接受子结果的数据类型)
    <resultMap type="实体类" id="fatherMap">
    <id column="id" property="id"/>
    <collection property="children" ofType="com.wengegroup.entity.UPerm" column="id" select="findMenuByPid"/>
</resultMap>

<!--级联查询父菜单-->
 <select id="selectUPerm" resultMap="fatherMap">
    select *
    from u_perm
    where id = #{id}
</select>

<!--级联查询子菜单-->
<select id="findMenuByPid" resultMap="fatherMap" >
    select * from u_perm where parent_id = #{id}
</select>







真理求知者
4 声望0 粉丝

在黑暗在探索光明,在迷茫中寻找真理!


下一篇 »
stream使用