一、本主的应用场景
部门里面有一个属性是当前部门的上级部门,而当前部门又会有下级部门,下级部门还有下级部门,这就形成了一个向下无限循环,呈现出树状结构。
二、认识JSONObject
JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。
postman测试效果图
建表(主键id自增长)
Entity实体类
package com.example.unicom.entity;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class RoleTree {
private Integer id;
private String role_name;
private Integer parent_role_id;
private List<RoleTree>trees =new ArrayList<>();
}
Controller (GeneralResponse为自己添加的公共返回实体类,正式开发加,做练习可以不加)
package com.example.unicom.controller;
import com.example.unicom.entity.RoleTree;
import com.example.unicom.entity.base.GeneralResponse;
import com.example.unicom.service.RoleTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 孙翊轩
* @since 2021-09-02
*/
@RestController
@RequestMapping("/isp/unicom/roletree")
public class RoleTreeController {
@Autowired
private RoleTreeService roleTreeService;
@RequestMapping(value ="selectRoleTree",method = RequestMethod.GET)
public GeneralResponse selectRoleTree(){
try{
List<RoleTree> roleTreeList=roleTreeService.selectRoleTree();
return new GeneralResponse("SUCCESS","查询成功",roleTreeList);
}catch (Exception e){
e.printStackTrace();
}
return new GeneralResponse("FAIL","查询失败",null);
}
}
Service接口
package com.example.unicom.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.unicom.entity.RoleTree;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface RoleTreeMapper extends BaseMapper<RoleTree> {
List<RoleTree> selectRoleTree();
}
@Service 业务层
package com.example.unicom.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.unicom.entity.RoleTree;
import com.example.unicom.entity.UserRole;
import com.example.unicom.mapper.RoleTreeMapper;
import com.example.unicom.mapper.UserRoleMapper;
import com.example.unicom.service.RoleTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class RoleTreeServiceImpl extends ServiceImpl<RoleTreeMapper,RoleTree>implements RoleTreeService{
@Autowired
private RoleTreeMapper roleTreeMapper;
@Autowired
private UserRoleMapper userRoleMapper;
/**
* 获取所有分类
* @return
*/
@Override
public List<RoleTree> selectRoleTree() {
List<RoleTree>roleTreeList=roleTreeMapper.selectRoleTree();
List<UserRole>userRoleList=userRoleMapper.selectAll();
//定义一个新的List
List<RoleTree>treeList=new ArrayList<>();
//找到所有的一级分类
for(RoleTree roleTree :roleTreeList){
//一级菜单的parent_role_id是0
if(roleTree.getParent_role_id()==0){
treeList.add(roleTree);
}
}
//为1级菜单设置子菜单
for (RoleTree roleTree :treeList){
roleTree.setTrees(getchilde(roleTree.getId(),roleTreeList));
}
return treeList;
}
/**
* 递归查找子菜单
* @param id 当前菜单id
* @param rootList 要查找的列表
*/
private List<RoleTree>getchilde(Integer id,List<RoleTree>rootList){
//子菜单的子菜单
List<RoleTree>childList =new ArrayList<>();
for (RoleTree roleTree :rootList){
//遍历所有节点,将父菜单id与传过来的id比较
if(roleTree.getParent_role_id().equals(id)){
childList.add(roleTree);
}
}
//将子菜单的子菜单再做循环
for(RoleTree roleTree :childList){
roleTree.setTrees(getchilde(roleTree.getId(),rootList));
}
//退出递归
if (childList.size()==0){
return null;
}
return childList;
}
}
Mapper接口
package com.example.unicom.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.unicom.entity.RoleTree;
import java.util.List;
public interface RoleTreeService extends IService<RoleTree> {
List<RoleTree> selectRoleTree();
}
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.example.unicom.mapper.UserRoleMapper">
<select id="selectRoleTree" parameterType="com.example.unicom.entity.UserRole" resultType="com.example.unicom.entity.UserRole">
SELECT user_role.user_id,user_role.user_name,user_role.jg_name
FROM user_role
</select>
</mapper>