1,三级分类树形结构查询
/*** DDD(Domain-Driven Design): 领域驱动设计** 三级分类树形结构;* 支持无限层级;* 当前项目只有三级*/
@Data
public class CategoryTreeTo {private Long categoryId; //1private String categoryName;private List<CategoryTreeTo> categoryChild;//子分类
}
<!-- 定义Category树形封装规则; 三级固定封装--><resultMap id="CategoryTreeRM" type="com.azxc.rapid.modules.main.dto.CategoryTreeTo"><!-- 一级分类的规则 --><id column="id" property="categoryId"></id><result column="name" property="categoryName"></result><collection property="categoryChild"ofType="com.azxc.rapid.modules.main.dto.CategoryTreeTo"><!-- 二级分类封装规则 --><id column="c2_id" property="categoryId"></id><result column="c2_name" property="categoryName"></result><collection property="categoryChild"ofType="com.azxc.rapid.modules.main.dto.CategoryTreeTo"><!-- 三级分类封装规则 --><id column="c3_id" property="categoryId"></id><result column="c3_name" property="categoryName"></result></collection></collection></resultMap><select id="getTest" resultMap="CategoryTreeRM">select bc1.*,bc2.id c2_id,bc2.name c2_name,bc2.category1_id,bc3.id c3_id,bc3.name c3_name,bc3.category2_idfrom base_category1 bc1left join base_category2 bc2 on bc1.id = bc2.category1_idleft join base_category3 bc3 on bc2.id = bc3.category2_id</select>
执行结果展示
2,树形结构(递归遍历)
@Overridepublic List<Permission> getTest1() {//查询所有菜单List<Permission> res = baseMapper.getTest1();return build(res);}/*** 根据权限列表构建父子关系** @param permissionList* @return*/public static List<Permission> build(List<Permission> permissionList) {List<Permission> menu = new ArrayList<>();for (Permission permission : permissionList) {//判断当前菜单是否是以及菜单if (permission.getParentId() == 0) {//一级菜单//2.1 设置一级菜单的子菜单列表permission.setChildren(getChildren(permission, permissionList));//2.2 将一级菜单添加到菜单列表中menu.add(permission);}}return menu;}/*** 从原始菜单中获取某个权限的子菜单列表** @param permission* @param originPermissionList* @return*/private static List<Permission> getChildren(Permission permission, List<Permission> originPermissionList) {//1. 创建一个新的集合,用来存储子菜单List<Permission> children = new ArrayList<>();//1. 遍历出原始菜单中的每一个权限for (Permission child : originPermissionList) {//1.1 如果originPermission的父id等于permission的idif (permission.getId().equals(child.getParentId())) {//originPermission是permission的子菜单,则将originPermission添加到children中// 子菜单还有没有子菜单呢?child.setChildren(getChildren(child, originPermissionList));children.add(child);}}return children;}
结果 :
表结构:树形结构
3,数据字典
需求效果图
表结构:树形结构
1.xml
<select id="findListByParentId" resultType="com.atguigu.entity.Dict"><include refid="columns"></include>from hse_dictwhere parent_id = #{parentId} and is_deleted = 0</select>
2.业务层
@Overridepublic List<Map<String, Object>> findZnodes(Long id) {//根据id查询对应的子分类List<Dict> dictList = dictMapper.findListByParentId(id);//使用Stream API将dictList转换为Map集合List<Map<String, Object>> zNodes = dictList.stream().map(dict -> {Map<String, Object> responseMap = new HashMap<>();//表示当前节点是否还有子节点,以当前节点的id到hse_dict表中查询子节点的数量,如果大于0,则表示当前节点还有子节点Integer count = dictMapper.countIsParent(dict.getId());responseMap.put("isParent", count > 0);//表示当前节点的名称responseMap.put("name", dict.getName());responseMap.put("id", dict.getId());return responseMap;}).collect(Collectors.toList());return zNodes;}