目录
前言
1、查询子父类通过树形结构封装起来
一、创建实体类
二、创建mapper类
三、创建service和serviceImpl类
四、创建controller类
2、删除该父类下的所有子类,并且删除自己
controller层
service和serviceImpl层
总结
前言
[超详细]Java子父类树形结构查询和删除[小白] 树形结构遍历,传给前端,今天在和甲方对需求的时候,提到了一个子父类树形结构查询的问题,我觉的这个这个功能还挺常见,所以今天就专门写了一篇文章,来让大家快速上手!
1、查询子父类通过树形结构封装起来
一、创建实体类
package com.atdession.config;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;import java.util.List;
//使用了Lombok依赖,如果没有的话就自己手动创建get set 方法 和有参 无参
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class TreeDate {//自己的Idprivate String id;//名称private String name;//父类Idprivate String pid;//子类private List<TreeDate> childList;
}
二、创建mapper类
package com.atdession.mapper;import com.atdession.config.TreeDate;
import com.atdession.entity.TptDocument;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface TreeDateMapper extends BaseMapper<TreeDate> {
}
三、创建service和serviceImpl类
service:
package com.atdession.service;import com.atdession.config.TreeDate;import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;public interface TreeDateService extends IService<TreeDate> {List<TreeDate> queryTreeDate();
}
serviceImpl:
package com.atdession.service.impl;import com.atdession.config.TreeDate;import com.atdession.mapper.TreeDateMapper;import com.atdession.service.TreeDateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;@Service
public class TreeDateServiceImpl extends ServiceImpl<TreeDateMapper, TreeDate> implements TreeDateService {
@Autowired
TreeDateMapper treeDateMapper;@Overridepublic List<TreeDate> queryTreeDate() {List<TreeDate> treeDates = new ArrayList<>();treeDates.add(new TreeDate("1","wdc","0",null));treeDates.add(new TreeDate("2","小伙纸","1",null));treeDates.add(new TreeDate("3","小猪","1",null));treeDates.add(new TreeDate("4","大笨蛋","2",null));treeDates.add(new TreeDate("5","你在干什么","3",null));treeDates.add(new TreeDate("6","我是大飞机","3",null));treeDates.add(new TreeDate("7","我是大鼠标","4",null));treeDates.add(new TreeDate("8","我是拖鞋","2",null));treeDates.add(new TreeDate("9","我是沈飞","4",null));treeDates.add(new TreeDate("10","我是夏利","3",null));List<TreeDate> treeDateList = queryTreeDate("0", treeDates);return treeDateList;}//递归找到所有子类并封装到childList属性中private List<TreeDate> queryTreeDate(String parentId, List<TreeDate> treeDateList){return treeDateList.stream().filter(item->item.getPid().equals(parentId)).peek(item->item.setChildList(queryTreeDate(item.getId(),treeDateList))).collect(Collectors.toList());}}
这个图片不需要看,我设计封面用的 哈哈哈哈
四、创建controller类
package com.atdession.controller;import com.atdession.config.TreeDate;
import com.atdession.service.TreeDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/wdc")
public class TreeDateController {@Autowiredprivate TreeDateService treeDateService;//查询树形结构@PostMapping("/queryTreeDate")public List<TreeDate> queryTreeDate(){List<TreeDate> treeDateList = treeDateService.queryTreeDate();return treeDateList;}}
好了!以上就是查询树形结构的案例了,接下来,我们来看删除 。
2、删除该父类下的所有子类,并且删除自己
controller层
package com.atdession.controller;import com.atdession.config.TreeDate;
import com.atdession.service.TreeDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/wdc")
public class TreeDateController {@Autowiredprivate TreeDateService treeDateService;@PostMapping("/removeTreeDateById")public boolean removeTreeDateById(@RequestParam("id")String id){boolean isYes = treeDateService.removeTreeDateById(id);return isYes;}}
service和serviceImpl层
service:
package com.atdession.service;import com.atdession.config.TreeDate;import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;public interface TreeDateService extends IService<TreeDate> {boolean removeTreeDateById(String id);
}
serviceImpl:
package com.atdession.service.impl;import com.atdession.config.TreeDate;import com.atdession.mapper.TreeDateMapper;import com.atdession.service.TreeDateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;@Service
public class TreeDateServiceImpl extends ServiceImpl<TreeDateMapper, TreeDate> implements TreeDateService {
@Autowired
TreeDateMapper treeDateMapper;@Overridepublic boolean removeTreeDateById(String id) {//存储索要删除的idList<String> childIdList = new ArrayList<>();//获得所有需要删除的idthis.queryChild(id,childIdList);
// 最后将传过来的Id添加进去childIdList.add(id);//批量删除treeDateMapper.deleteBatchIds(childIdList);return true;}private void queryChild(String id, List<String> childIdList) {/** 查询数据库pid等于id的数据!!!!* 如果你使用的是mybatis-plus则可以通过mybatis-plus查* 如果你用的是其他的,则通过其他的查询。*/List<TreeDate> List = treeDateMapper.selectList(new LambdaQueryWrapper<TreeDate>().eq(TreeDate::getPid, id));//递归查询下一级Id,并且将上一级的Id存入到list中List.forEach(item->{childIdList.add(item.getId());this.queryChild(item.getId(),childIdList);});}}
总结
好了!以上就是对该子父类树形的查询和删除,这一块还是挺重要的,因为在正式项目中遇到的这种需求很多,所以一定要牢牢记住!