表设计是这样,主要通过parent_id来实现,可以给parent_id加一个dept_id字段的外键约束,但我这里实现增加操作时会依赖主键自增如果设置外键会影响,不能设置dept_id自增,所以我没有加外键约束
实体类定义
@Data @TableName("zyj_dept") @ApiModel(value = "部门层级显示表") public class ZyjDept extends BaseEntity { /** * 部门表ID */ private Integer deptId; /** * 部门名称 */ private String deptName; /** * 父级id */ private int parentId; /** * 子级 */ private List<ZyjDept> children; }
在里面添加了一个children字段,用来接收构建子部门时的数据
mapper层
/** * 查询 */ List<ZyjDept> selectZyjDept(ZyjDept zyjDept);
关于mapper.xml层的查询语句直接使用平常查询所有的就可以,也可以按照自己的情况设定条件
<resultMap id="ResultMap" type="com.future.cloud.business.entity.ZyjDept"> <id column="dept_id" property="deptId"/> <result column="dept_name" property="deptName"/> <result column="parent_id" property="parentId"/> </resultMap><!-- 查询方法 --> <select id="selectZyjDept" parameterType="com.future.cloud.business.entity.ZyjDept" resultMap="ResultMap"> SELECT zyj_dept.dept_id, zyj_dept.dept_name, zyj_dept.parent_id FROM zyj_dept <where> <if test="deptId != null and deptId != ''">and parent_id = #{deptId} </if> <if test="deptName != null and deptName != ''">and dept_name like concat('%', #{deptName}, '%')</if> </where> </select>
/** * Service接口层 */ public interface ZyjDeptService { /** * 查询 * * @param zyjDept * @return */ List<ZyjDept> selectZyjDept(ZyjDept zyjDept);/** * Service层实现类 */ @Service public class ZyjDeptServiceImpl implements ZyjDeptService { /** * mapper层实例化 */ @Autowired private ZyjDeptMapper zyjDeptMapper; /** * 查询方法 * * @param zyjDept * @return */ @Override public List<ZyjDept> selectZyjDept(ZyjDept zyjDept) { List<ZyjDept> zyjDeptList = zyjDeptMapper.selectZyjDept(zyjDept); Integer parentId = zyjDept.getDeptId(); // 获取传入的 deptId if (parentId == null) { return buildZyjDeptTree(zyjDeptList, 0);//从根部门开始构建树形结构 } return buildZyjDeptTree(zyjDeptList, parentId); } /** * 构建树形结构,查询结果按层级显示 * * @param zyjDepts * @param parentId * @return */ private List<ZyjDept> buildZyjDeptTree(List<ZyjDept> zyjDepts, Integer parentId) { List<ZyjDept> buildTree = new ArrayList<>(); for (ZyjDept zyjDept : zyjDepts) { /** * 判断每个部门的父级ID是否与传入的parentId相同 */ if (Objects.equals(zyjDept.getParentId(), parentId)) { List<ZyjDept> children = buildZyjDeptTree(zyjDepts, zyjDept.getDeptId()); zyjDept.setChildren(children); buildTree.add(zyjDept); } } return buildTree; }
controller层
/** *部门表树级展示 测试接口 */ @RestController @RequestMapping("/zyjDept") @Api(value = "部门表层级展示 测试接口",tags ="部门表层级展示 测试接口") public class ZyjDeptController { /** * service层实例化 */ @Autowired private ZyjDeptService zyjDeptService; /** * 查询接口 * @param zyjDept * @return */ @PostMapping(value = "/treeSelect") @ApiOperation("查询接口") public WebDataAbstract<ZyjDept> selectZyjDept(@RequestBody ZyjDept zyjDept){ try { List<ZyjDept> zyjDeptList=this.zyjDeptService.selectZyjDept(zyjDept); return new WebDataResult<ZyjDept>().getWebDataAbstract(WebDataAbstract.NORMAL_STATUS, WebDataAbstract.NORMAL, "查询成功", zyjDeptList); }catch (Exception e){ e.printStackTrace(); return new WebDataResult<ZyjDept>().getWebDataAbstract(WebDataAbstract.EXCEPTION_STATUS, WebDataAbstract.EXCEPTION, e.getMessage()); } }
出来的效果就是
{
"ifPagination": null,
"limit": null,
"page": null,
"fisused": null,
"fcreateTime": "2023-02-06 13:50:48",
"fcreateUserid": "1",
"fcreateUsername": "superman",
"feditTime": "2023-06-26 09:07:56",
"feditUserid": null,
"feditUsername": null,
"forderId": null,
"fdataDeptId": null,
"params": {},
"deptId": 1,
"deptName": "阿里巴巴",
"parentId": 0,
"children": [
{
"ifPagination": null,
"limit": null,
"page": null,
"fisused": null,
"fcreateTime": "2023-04-13 13:51:26",
"fcreateUserid": "1",
"fcreateUsername": "superman",
"feditTime": "2023-06-26 09:07:56",
"feditUserid": null,
"feditUsername": null,
"forderId": null,
"fdataDeptId": null,
"params": {},
"deptId": 6,
"deptName": "饿了么",
"parentId": 1,
"children": []
},
{
"ifPagination": null,
"limit": null,
"page": null,
"fisused": null,
"fcreateTime": "2023-04-24 13:51:19",
"fcreateUserid": "1",
"fcreateUsername": "superman",
"feditTime": "2023-06-26 09:07:56",
"feditUserid": null,
"feditUsername": null,
"forderId": null,
"fdataDeptId": null,
"params": {},
"deptId": 5,
"deptName": "天猫",
"parentId": 1,
"children": []
},
{
"ifPagination": null,
"limit": null,
"page": null,
"fisused": null,
"fcreateTime": "2023-05-02 13:51:02",
"fcreateUserid": "1",
"fcreateUsername": "superman",
"feditTime": "2023-06-26 09:07:56",
"feditUserid": null,
"feditUsername": null,
"forderId": null,
"fdataDeptId": null,
"params": {},
"deptId": 2,
"deptName": "淘宝",
"parentId": 1,
"children": [
{
"ifPagination": null,
"limit": null,
"page": null,
"fisused": null,
"fcreateTime": "2023-04-24 13:51:35",
"fcreateUserid": "1",
"fcreateUsername": "superman",
"feditTime": "2023-06-26 09:07:56",
"feditUserid": null,
"feditUsername": null,
"forderId": null,
"fdataDeptId": null,
"params": {},
"deptId": 8,
"deptName": "淘菜菜",
"parentId": 2,
"children": []
}
]
},
展示一部分,前台效果基本上就是按照嵌套来一层层展示