完成zTree的数据回显
使用技术
springboot+mybatis+mysql
配置
datasource
CREATE TABLE `asset_label` (
`id` int(255) NOT NULL AUTO_INCREMENT COMMENT '菜单Id',
`labelName` varchar(255) DEFAULT NULL COMMENT '菜单名称',
`parentId` varchar(255) DEFAULT NULL COMMENT '父级ID',
`cheaked` varchar(255) DEFAULT 'false' COMMENT '是否勾选',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
数据库数据
yml
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/testmysql?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.xiao.ztree.entity
#showSql
logging:
level:
com:
xiao:
ztree:
mapper : debug
代码
entity
package com.xiao.ztree.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Label implements Serializable {
private String id;
private String labelName;
private String parentId;
private String cheaked;
private List<Label> children=new ArrayList<>();
public Label() {
}
public Label(String id, String labelName, String parentId, String cheaked, List<Label> children) {
this.id = id;
this.labelName = labelName;
this.parentId = parentId;
this.cheaked = cheaked;
this.children = children;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabelName() {
return labelName;
}
public void setLabelName(String labelName) {
this.labelName = labelName;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getCheaked() {
return cheaked;
}
public void setCheaked(String cheaked) {
this.cheaked = cheaked;
}
public List<Label> getChildren() {
return children;
}
public void setChildren(List<Label> children) {
this.children = children;
}
@Override
public String toString() {
return "Label{" +
"id='" + id + '\'' +
", labelName='" + labelName + '\'' +
", parentId='" + parentId + '\'' +
", cheaked='" + cheaked + '\'' +
", children=" + children +
'}';
}
}
controller
package com.xiao.ztree.controller;
import com.xiao.ztree.entity.Label;
import com.xiao.ztree.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("get")
public class LabelController {
@Autowired
private LabelService service;
@RequestMapping("list")
public List<Label> findAll(){
return service.findAll();
}
}
service
package com.xiao.ztree.service;
import com.xiao.ztree.entity.Label;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface LabelService {
List<Label> findAll();
}
serviceImpl
package com.xiao.ztree.service.impl;
import com.xiao.ztree.entity.Label;
import com.xiao.ztree.mapper.LabelMapper;
import com.xiao.ztree.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class LabelServiceImpl implements LabelService {
@Autowired
private LabelMapper labelMapper;
@Override
public List<Label> findAll() {
// 获取所有的数据
List<Label> labels = labelMapper.listAll();
// 进行处理
List<Label> firstLabel = new ArrayList<>();
HashMap<String, Label> labelMap = new HashMap<>();
for (Label label : labels) {
// 将数据放入中间容器
labelMap.put(label.getId(),label);
// 找到父级的id
if (label.getParentId()==null){
firstLabel.add(label);
}
// 找出不是父级菜单的且集合中包含父级菜单的id
if (label.getParentId()!=null && labelMap.containsKey(label.getParentId())){
labelMap.get(label.getParentId()).getChildren().add(label);
}
}
return firstLabel;
}
}
mapper
package com.xiao.ztree.mapper;
import com.xiao.ztree.entity.Label;
import java.util.List;
public interface LabelMapper {
List<Label> listAll();
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiao.ztree.mapper.LabelMapper">
<resultMap id="BaseResultMap" type="com.xiao.ztree.entity.Label">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="labelName" jdbcType="VARCHAR" property="labelName" />
<result column="parentId" jdbcType="VARCHAR" property="parentId" />
<result column="cheaked" jdbcType="VARCHAR" property="cheaked" />
</resultMap>
<select id="listAll" resultMap="BaseResultMap">
select * from asset_label
</select>
</mapper>
回显结果
[
{
"id": "1",
"labelName": "父类哦-一级",
"parentId": null,
"cheaked": "false",
"children": [
{
"id": "2",
"labelName": "测试标签2-二级",
"parentId": "1",
"cheaked": "false",
"children": [
{
"id": "3",
"labelName": "测试标签3-三级",
"parentId": "2",
"cheaked": "false",
"children": [
]
},
{
"id": "5",
"labelName": "测试标签5-三级",
"parentId": "2",
"cheaked": "false",
"children": [
{
"id": "6",
"labelName": "测试标签6-四级",
"parentId": "5",
"cheaked": "false",
"children": [
]
}
]
}
]
},
{
"id": "4",
"labelName": "测试标签4-二级",
"parentId": "1",
"cheaked": "false",
"children": [
]
}
]
}
]