作者:漂浪男孩2010_218 | 来源:互联网 | 2023-10-10 13:44
1.Controller:packagecom.cars.ict.rbpsems.controller;importcom.cars.ict.rbpsems.entity.TbBa
1.Controller:
package com.cars.ict.rbpsems.controller;
import com.cars.ict.rbpsems.entity.TbBasetree;
import com.cars.ict.rbpsems.service.TbBasetreeService;
import com.cars.ict.rbpsems.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author testjava
* @since 2022-01-09
*/
@RestController
@RequestMapping("/tree")
public class TbBasetreeController {
// @Resource
@Autowired
private TbBasetreeService tbBasetreeService;
//查询树
@PostMapping("/list")
public List list() {
List persOnList= tbBasetreeService.getPersonList();
return personList;
}
//查询树2(只展示baseTree表里tree_state字段的值为1的数据)
@PostMapping("/listState")
public List listState() {
List persOnList= tbBasetreeService.getTreeState();
return personList;
}
//新增树
@PostMapping("/save")
public Result save(@RequestBody TbBasetree tbBasetree) {
boolean save = tbBasetreeService.save(tbBasetree);
if (save) {
return Result.ok();
}else {
return Result.error();
}
}
//修改树
@PostMapping("/edit")
public Result edit(@RequestBody TbBasetree tbBasetree) {
boolean update = tbBasetreeService.update(tbBasetree);
if (update) {
return Result.ok();
}else {
return Result.error();
}
}
//删除树
@PostMapping("/delete")
public Result delete(@RequestParam String id) {
boolean delete = tbBasetreeService.delete(id);
if (delete) {
return Result.ok();
}else {
return Result.error();
}
}
}
===============================================================================================================
2.Service:
package com.cars.ict.rbpsems.service;
import com.cars.ict.rbpsems.entity.TbBasetree;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
*
* 服务类
*
*
* @author testjava
* @since 2022-01-09
*/
public interface TbBasetreeService extends IService {
//查询树
List getPersonList();
//查询树2(只展示baseTree表里tree_state字段的值为1的数据)
List getTreeState();
//新增树
boolean save(TbBasetree tbBasetree);
//修改树
boolean update(TbBasetree tbBasetree);
//删除树
boolean delete(String id);
}
===============================================================================================================
3.ServiceImpl:
package com.cars.ict.rbpsems.service.impl;
import com.cars.ict.rbpsems.entity.TbBasetree;
import com.cars.ict.rbpsems.mapper.TbBasetreeMapper;
import com.cars.ict.rbpsems.service.TbBasetreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author testjava
* @since 2022-01-09
*/
@Service
public class TbBasetreeServiceImpl extends ServiceImpl, TbBasetree> implements TbBasetreeService {
@Resource
private TbBasetreeMapper tbBasetreeMapper;
//查询树
@Override
public List getPersonList() {
//查询所有的数据
List allList = tbBasetreeMapper.getPersonList();
//创建一个list集合,用于存放根节点
List parent = new ArrayList<>();
for (TbBasetree person : allList) {
//如果parentId为0,则代表是根节点,存入到集合中 person.setSpread(true); 自动展开
if (person.getParentId().equals("0")) {
//虚拟字段spread为true时表示默认展开
person.setSpread("true");
//虚拟字段checked为true时表示默认选中
person.setChecked("true");
parent.add(person);
}
}
//查找根节点下的子类,因为根节点的id就是子节点的parentId;
for (TbBasetree person : parent) {
List children = getChild(person.getId(), allList);
person.setChildren(children);
}
return parent;
}
//查找子节点的方法
private List getChild(String id, List allList) {
//存放子节点的集合
List children = new ArrayList<>();
for (TbBasetree person : allList) {
//如果根节点的id等于集合内parentId,说明是根节点的子节点
if (person.getParentId().equals(id)) {
//存入子节点集合
//虚拟字段checked为true时表示默认选中
person.setChecked("true");
children.add(person);
}
}
for (TbBasetree person : children) {
//递归调用,如果子节点存在根节点,则再次调用,往后依次推
List persOnChildren= getChild(person.getId(), allList);
person.setChildren(personChildren);
}
//递归结束条件,如果子节点不存在子节点,则递归结束
// if (children == null) {
// return null;
// }
return children;
}
//新增树
@Override
public boolean save(TbBasetree tbBasetree) {
return tbBasetreeMapper.save(tbBasetree);
}
//修改树
@Override
public boolean update(TbBasetree tbBasetree) {
return tbBasetreeMapper.update(tbBasetree);
}
//删除树
@Override
public boolean delete(String id) {
//首先查出当前节点是否存在子节点,如果存在,则遍历删除子节点
List persOnList= tbBasetreeMapper.findByParentId(id);
System.out.println(personList);
//递归查出所有的子节点,personList为null是递归结束条件,如果当前子节点还存在子节点,则再次执行当前方法,只到找到不存子节点的那项结束
if (personList != null && personList.size() > 0) {
for (TbBasetree person : personList) {
delete(person.getId());
}
}
return tbBasetreeMapper.delete(id);
}
//查询树2(只展示baseTree表里tree_state字段的值为1的数据)
@Override
public List getTreeState() {