1. 方法一: List的stream()方法
public ResultData queryMenuList() { // 获取所有数据List List<MenuVo> list = MenuDao.queryMenuList(); // 通过list.stream()将List结构转成Tree结构并返回 List<MenuVo> collection = list.stream() .filter(o -> StrUtil.equals("-1", o.getPIdString())) .peek(o -> o.setChildList(getChildList(o, list))) .collect(Collectors.toList()); return ResultData.success(Constants.QUERY_SUCCESS, collection, collection.size()); } // 递归方法 private List<MenuVo> getChildList(MenuVo MenuVo, List<MenuVo> list) { return list.stream() .filter(o -> StrUtil.equals(String.valueOf(MenuVo.getIdString()), o.getPIdString())) .peek(o -> o.setChildList(getChildList(o, list))) .collect(Collectors.toList()); }
2. 方法二: mybatis映射
//实体类 @Data public class Menu{ // id private int id; // 菜单名称 private String name; // 上级菜单id private int parentId; // 子集菜单列表 private List<Menu> childList; } // XML <resultMap id="MenuMap" type="com.demo.study.entity.Menu"> <result column="id" property="id"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <collection column="id" property="childList" select="queryMenuList"/> </resultMap> <select id="queryMenuList" resultMap="MenuMap"> select * from t_menu where parent_id = #{id} </select>
标签:Java,stream,父级,list,Tree,private,MenuVo,List,id From: https://www.cnblogs.com/Troy0912/p/16938665.html