- 业务代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.bl.station.serviceimpl.itemcat;
import com.bl.station.Bean.BeanValidator;
import com.bl.station.Bean.TreeNode;
import com.bl.station.entity.Itemcat;
import com.bl.station.entity.ItemcatExample;
import com.bl.station.mapper.ItemcatMapper;
import com.bl.station.param.ItemCatParam;
import com.bl.station.service.ItemCat.ItemCatService;
import com.bl.station.utils.StationResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @ClassName ItemCarServiceImpl
* @Description TODO
* @Date 2018/8/14 11:17
* @Author itastro
* @Version 1.0
**/
@Service
@Slf4j
public class ItemCatServiceImpl implements ItemCatService {
@Autowired
private ItemcatMapper itemcatMapper;
/**
* 通过父节点查询子节点
*
* @param parentId
* @return
*/
@Override
public List<TreeNode> getItemCatList(Integer parentId) {
//创建一个查询类
ItemcatExample example = new ItemcatExample();
//设置查询条件
ItemcatExample.Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
criteria.andStatusNotEqualTo("2");
//查询
List<Itemcat> list = itemcatMapper.selectByExample(example);
ArrayList<TreeNode> resultList = getTreeNodes(list);
return resultList;
}
private ArrayList<TreeNode> getTreeNodes(List<Itemcat> list) {
//转换成TreeNode 列表
ArrayList<TreeNode> resultList = new ArrayList<>();
for (Itemcat itemcat : list) {
TreeNode treeNode = new TreeNode();
treeNode.setId((long) itemcat.getId());
treeNode.setText(itemcat.getName());
treeNode.setState(itemcat.getIsParent() ? "closed" : "open");
resultList.add(treeNode);
}
return resultList;
}
/**
* 添加商品类目
*
* @param itemCatParam
* @return
*/
@Override
public StationResult save(ItemCatParam itemCatParam) {
log.info("itemCatParam:{}", itemCatParam);
BeanValidator.check(itemCatParam);
Itemcat itemcat = getItemcat(itemCatParam);
//刚添加的节点肯定不是父节点
itemcat.setIsParent(false);
//数据库默认的排序都是1
itemcat.setSortorder(1);
//插入数据库并且返回id
itemcatMapper.saveAndGetId(itemcat);
//添加节点的时候 先判断是不是父节点,如果父节点是叶子节点的话要更改节点的状态
Itemcat parent = itemcatMapper.selectByPrimaryKey(itemCatParam.getParentId());
if (!parent.getIsParent()) {
parent.setIsParent(true);
//进行更新操作
itemcatMapper.updateByPrimaryKey(parent);
}
return StationResult.success("产品类目添加成功");
}
private Itemcat getItemcat(ItemCatParam itemCatParam) {
Itemcat itemcat = new Itemcat();
//进行copy
BeanUtils.copyProperties(itemCatParam, itemcat);
itemcat.setCreatetime(new Date());
// 1:正常
2:删除
itemcat.setStatus("1");
return itemcat;
}
@Override
public StationResult update(ItemCatParam itemCatParam) {
//通过id查询节点对象
Itemcat itemcat = itemcatMapper.selectByPrimaryKey(itemCatParam.getId());
//判断name值和原来那么值是否相同
if (itemCatParam.getName().equals(itemcat.getName())){
return StationResult.success("产品类目更新成功");
}
itemcat.setName(itemCatParam.getName());
itemcatMapper.updateByPrimaryKey(itemcat);
//填充属性
itemcat.setParentId(itemCatParam.getId());
return StationResult.success("产品类目更新成功");
}
/**
* 删除节点
*
* @param id
* @return
*/
@Override
public StationResult delete(Integer id) {
//删除分类就是改变节点的状态为2
Itemcat itemcat = itemcatMapper.selectByPrimaryKey(id);
//状态
itemcat.setStatus("2");
//更新
itemcatMapper.updateByPrimaryKey(itemcat);
//我们还需要判断一下要删除的这个节点是否是父节点,如果是父节点,那么就级联
//删除这个父节点下的所有子节点(采用递归的方式删除)
if (itemcat.getIsParent()) {
deleteNode(itemcat.getId());
}
//需要判断父节点当前还有没有子节点,如果有子节点就不用做修改
//如果父节点没有子节点了,那么要修改父节点的isParent属性为false即变为叶子节点
Itemcat parent = itemcatMapper.selectByPrimaryKey(itemcat.getParentId());
List<Itemcat> list = getItemCatListByParentId(parent.getId());
//判断父节点是否有子节点是判断这个父节点下的所有子节点的状态,如果状态都是2就说明
//没有子节点了,否则就是有子节点。
boolean flag = false;
for (Itemcat itemcat1 : list) {
if (!("2").equals(itemcat1.getStatus())) {
flag = true;
break;
}
}
//如果没有子节了
if (!flag) {
parent.setIsParent(false);
itemcatMapper.updateByPrimaryKey(parent);
}
return StationResult.success("删除成功");
}
/**
* 递归删除节点
*
* @param id
*/
private void deleteNode(Integer id) {
List<Itemcat> list = getItemCatListByParentId(id);
for (Itemcat itemcat : list) {
itemcat.setStatus("2");
itemcatMapper.updateByPrimaryKey(itemcat);
if (itemcat.getIsParent()) {
deleteNode(itemcat.getId());
}
}
}
/**
* 通过父亲节点的id来查询所有子节点
*
* @param parentId
* @return
*/
private List<Itemcat> getItemCatListByParentId(Integer parentId) {
ItemcatExample itemcatExample = new ItemcatExample();
ItemcatExample.Criteria criteria = itemcatExample.createCriteria();
criteria.andParentIdEqualTo(parentId);
List<Itemcat> list = itemcatMapper.selectByExample(itemcatExample);
return list;
}
@Override
public List<TreeNode> findAll() {
List<Itemcat> itemcats = itemcatMapper.findAll();
ArrayList<TreeNode> resultList = getTreeNodes(itemcats);
return resultList;
}
}
树结构类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @ClassName TreeNode
* @Description TODO
* @Date 2018/8/14 11:21
* @Author itastro
* @Version 1.0
**/
@ApiModel
public class TreeNode {
@ApiModelProperty(name = "id", value = "分类id")
private Long id;
@ApiModelProperty(name = "text", value = "分类名称")
private String text;
@ApiModelProperty(name = "state", value = "是否打开,close:关闭
open:打开")
private String state;
public Long getId() {
return id;
}
public String getText() {
return text;
}
public String getState() {
return state;
}
public void setId(Long id) {
this.id = id;
}
public void setText(String text) {
this.text = text;
}
public void setState(String state) {
this.state = state;
}
}
数据库表
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DROP TABLE IF EXISTS `itemcat`;
CREATE TABLE `itemcat` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '产品分类id',
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '产品分类名称',
`remark` varchar(255) DEFAULT '' COMMENT '说明',
`parent_id` int(11) DEFAULT NULL COMMENT '父节点id',
`status` varchar(255) DEFAULT NULL COMMENT '状态
1:展示
2:不展示',
`sortorder` int(11) DEFAULT NULL COMMENT '排序',
`is_parent` tinyint(1) DEFAULT NULL COMMENT '是否为父节点
1:是
0:不是',
`createtime` datetime DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
最后
以上就是想人陪大炮最近收集整理的关于java树形菜单后台代码的全部内容,更多相关java树形菜单后台代码内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复