热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

树的增删改模块

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) {
//如果parentId0,则代表是根节点,存入到集合中 person.setSpread(true); 自动展开
if (person.getParentId().equals("0")) {
//虚拟字段spreadtrue时表示默认展开
person.setSpread("true");
//虚拟字段checkedtrue时表示默认选中
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)) {
//存入子节点集合
//虚拟字段checkedtrue时表示默认选中
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);
//递归查出所有的子节点,personListnull是递归结束条件,如果当前子节点还存在子节点,则再次执行当前方法,只到找到不存子节点的那项结束
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() {
//查询所有的数据
List allList = tbBasetreeMapper.getTreeState();
//创建一个list集合,用于存放根节点
List parent = new ArrayList<>();
for (TbBasetree person : allList) {
//如果parentId0,则代表是根节点,存入到集合中 person.setSpread(true); 自动展开
if (person.getParentId().equals("0")) {
//虚拟字段spreadtrue时表示默认展开
person.setSpread("true");
//虚拟字段checkedtrue时表示默认选中
person.setChecked("true");
parent.add(person);
}
}
//查找根节点下的子类,因为根节点的id就是子节点的parentId;
for (TbBasetree person : parent) {
List children = getChild2(person.getId(), allList);
person.setChildren(children);
}
return parent;
}
//查找子节点的方法
private List getChild2(String id, List allList) {
//存放子节点的集合
List children = new ArrayList<>();
for (TbBasetree person : allList) {
//如果根节点的id等于集合内parentId,说明是根节点的子节点
if (person.getParentId().equals(id)) {
//存入子节点集合
//虚拟字段checkedtrue时表示默认选中
person.setChecked("true");
children.add(person);
}
}
for (TbBasetree person : children) {
//递归调用,如果子节点存在根节点,则再次调用,往后依次推
List persOnChildren= getChild2(person.getId(), allList);
person.setChildren(personChildren);
}
return children;
}
}

===============================================================================================================
4.Mapper接口:

package com.cars.ict.rbpsems.mapper;

import com.cars.ict.rbpsems.entity.TbBasetree;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
*


* Mapper 接口
*


*
* @author testjava
* @since 2022-01-09
*/
public interface TbBasetreeMapper extends BaseMapper {
List getPersonList();

boolean save(TbBasetree tbBasetree);

boolean update(TbBasetree tbBasetree);

boolean delete(String id);

TbBasetree findById(Integer id);

List findByParentId(String parentId);

List getTreeState();

}

===============================================================================================================
5.Mapper.xml:
xml version="1.0" encoding="UTF-8"?>
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="com.cars.ict.rbpsems.mapper.TbBasetreeMapper">



id="save">
insert into tb_basetree (id, parent_id, title, field, info_mark, info_value, info_type, tree_state, create_time, update_time)
values (#{id}, #{parentId}, #{title}, #{field}, #{infoMark}, #{infoValue}, #{infoType}, #{treeState}, #{createTime}, #{updateTime})


id="update">
update tb_basetree set title=#{title}, field=#{field}, info_mark=#{infoMark}, info_value=#{infoValue}, info_type=#{infoType},
tree_state=#{treeState}, create_time=#{createTime}, update_time=#{updateTime}
where id = #{id}


id="delete">
delete from tb_basetree where id = #{id}














===============================================================================================================
6.entity:

package com.cars.ict.rbpsems.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
*


*
*


*
* @author testjava
* @since 2022-01-09
*/
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="TbBasetree对象", description="")
public class TbBasetree implements Serializable {
private static final long serialVersiOnUID= 1L;

@TableId(value = "id", type = IdType.ID_WORKER)
private String id;

@ApiModelProperty(value = "id")
private String parentId;

@ApiModelProperty(value = "单位名称")
// @TableField("inf_name")
private String title;

@ApiModelProperty(value = "单位编码")
// @TableField("info_code")
private String field;

@ApiModelProperty(value = "标识")
private String infoMark;

@ApiModelProperty(value = "")
private Integer infoValue;

@ApiModelProperty(value = "类型(1国铁集团,2路局,3车务段,4中心站,5代管站)")
private Integer infoType;

@ApiModelProperty(value = "状态")
private Integer treeState;

@ApiModelProperty(value = "创建时间")
@Temporal(TemporalType.TIMESTAMP)
@TableField(fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezOne= "GMT+8")
private Date createTime;

@ApiModelProperty(value = "修改时间")
@Temporal(TemporalType.TIMESTAMP)
@TableField(fill = FieldFill.INSERT_UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezOne= "GMT+8")
private Date updateTime;

private List children;

@ApiModelProperty(value = "true时是默认展开")
@Transient
private String spread;

@ApiModelProperty(value = "true时是默认选中")
@Transient
private String checked;

}



 

推荐阅读
author-avatar
漂浪男孩2010_218
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有