数据库字段以及测试数据
Maven所需依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
工具类
import com.computer.entity.Tree;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class MenuTreeUtil {
public List<Tree> menuCommon;
public List<Object> list = new ArrayList<>();
public List<Object> menuList(List<Tree> menu) {//controller层调用的方法,并将数据以list的形式返回
this.menuCommon = menu;
for (Tree t : menu) {
LinkedHashMap<String, Object> mapArr = new LinkedHashMap<>();
if (t.getIsparent().equals("true") && t.getParent_code().equals("0")) {//判断是否为父级
mapArr.put("id",t.getId());
mapArr.put("tree_name",t.getTree_name());
mapArr.put("parent_code",t.getParent_code());
mapArr.put("hasChildren",t.getIsparent());
mapArr.put("tree_img",t.getTree_img());
mapArr.put("child",menuChild(t.getId()));//去子集查找遍历
list.add(mapArr);
}
}
return list;
}
public List<?> menuChild(String id){//子集查找遍历
List<Object> lists = new ArrayList<>();
for (Tree a : menuCommon) {
Map<String, Object> childArray = new LinkedHashMap<>();
if (a.getParent_code().equals(id)){
childArray.put("id",a.getId());
childArray.put("tree_name",a.getTree_name());
childArray.put("parent_code",a.getParent_code());
childArray.put("tree_img",a.getTree_img());
childArray.put("hasChildren",a.getIsparent());
childArray.put("child",menuChild(a.getId()));
lists.add(childArray);
}
}
return lists;
}
}
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PageData {
private String id;//主键
private String parentCode;//父级编码
private String treeName;//树结构的名字
private String isParent;//是否是父级
private String treeImg;//树结构的图片
}
package com.computer.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Tree {
private String id;//主键
private String parent_code;//父级编码
private String tree_name;//树结构的名字
private String isparent;//是否是父级
private String tree_img;//树结构的图片
}
Dao
import com.computer.entity.PageData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface MenuTreeDao {
@Select("select * from pageData")
List<PageData> selectMenuTree();
}
Service
import com.computer.entity.PageData;
import java.util.List;
public interface RegionTreeService {
List<PageData> findReagionTreePage();
}
import com.computer.dao.MenuTreeDao;
import com.computer.entity.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RegionTreeServiceImpl implements RegionTreeService {
@Autowired
MenuTreeDao menuTreeDao;
@Override
public List<PageData> findReagionTreePage() {
return menuTreeDao.selectMenuTree();
}
}
Controller
import com.computer.entity.PageData;
import com.computer.entity.Tree;
import com.computer.service.RegionTreeServiceImpl;
import com.computer.utils.MenuTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class MenuTreeController {
@Autowired
RegionTreeServiceImpl regionTreeService;
@GetMapping(value = "/queryTree")
public Object findPage(){
Map<String, Object> returnMap = new HashMap<>();
List<PageData> lists = regionTreeService.findReagionTreePage();//所有数据未转换成树结构
MenuTreeUtil menuTree = new MenuTreeUtil();//定义工具类
List<Tree> lt = new ArrayList<>();//定义实体类
for (int i = 0; i < lists.size(); i++) {
Tree t = new Tree();//转换成对象
t.setId(lists.get(i).getId());//将数据赋给实体类
t.setTree_name(lists.get(i).getTreeName());
t.setParent_code(lists.get(i).getParentCode());
t.setIsparent(lists.get(i).getIsParent());
t.setTree_img(lists.get(i).getTreeImg());
lt.add(t);
}
List<Object> menuList = menuTree.menuList(lt);//将所有数据转换成树结构
returnMap.put("list",menuList);
return returnMap;
}
}