数据库设计
实体类
package com.heeexy.example.entity;
import com.alibaba.fastjson.annotation.JSONField;
import java.util.ArrayList;
import java.util.List;
//菜单实体类
public class Menu {
private String id;
private String pid;
private String path;
private String redirect;
private String name;
private String component;
private Boolean alwaysShow;
private String menu;
//菜单标识
private Meta meta;
//子菜单
private List<Menu> children;
public Menu(String id, String pid, String path, String redirect, String name, String component, Boolean alwaysShow, String menu, Meta meta) {
this.id = id;
this.pid = pid;
this.path = path;
this.redirect = redirect;
this.name = name;
this.component = component;
this.alwaysShow = alwaysShow;
this.menu = menu;
this.meta = meta;
}
public Menu(){
}
//@JSONField(serialize=false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//@JSONField(serialize=false)
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComponent() {
return component;
}
public void setComponent(String component) {
this.component = component;
}
public Boolean getAlwaysShow() {
return alwaysShow;
}
public void setAlwaysShow(Boolean alwaysShow) {
this.alwaysShow = alwaysShow;
}
public String getMenu() {
return menu;
}
public void setMenu(String menu) {
this.menu = menu;
}
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public List<Menu> getChildren() {
return children;
}
public void setChildren(List<Menu> children) {
this.children = children;
}
@Override
public String toString() {
return "Menu{" +
"id='" + id + '\'' +
", pid='" + pid + '\'' +
", path='" + path + '\'' +
", redirect='" + redirect + '\'' +
", name='" + name + '\'' +
", component='" + component + '\'' +
", alwaysShow=" + alwaysShow +
", menu='" + menu + '\'' +
", meta=" + meta +
", children=" + children +
'}';
}
}
package com.heeexy.example.entity;
//菜单标识
public class Meta {
private String title;
private String icon;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
@Override
public String toString() {
return "Meta{" +
"title='" + title + '\'' +
", icon='" + icon + '\'' +
'}';
}
public Meta(String title, String icon) {
this.title = title;
this.icon = icon;
}
}
controller方法
package com.heeexy.example.controller;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.service.MenuService;
import com.heeexy.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
//菜单controller
@RestController
@RequestMapping("/menu")
public class MenuController {
@Autowired
private MenuService menuService;
@PostMapping("getInfo")
public JSONObject getInfo(HttpServletRequest request){
JSONObject jsonObject = CommonUtil.request2Json(request);
JSONObject returnData = menuService.getInfo(jsonObject);
System.out.println(returnData);
return returnData;
}
@PostMapping("getInfo2")
public JSONObject getInfo2(@RequestBody JSONObject jsonObject){
System.out.println(jsonObject);
return CommonUtil.successJson();
}
}
service方法
package com.heeexy.example.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.heeexy.example.dao.MenuDao;
import com.heeexy.example.entity.Menu;
import com.heeexy.example.entity.Meta;
import com.heeexy.example.util.TreeUtil;
import com.heeexy.example.service.MenuService;
import com.heeexy.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class MenuServiceImpl implements MenuService{
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MenuService.class);
@Autowired
private MenuDao menuDao;
@Override
//查询所有菜单列表
public JSONObject getInfo(JSONObject jsonObject) {
JSONObject returnData = new JSONObject();
List<JSONObject> jsonlist = menuDao.getInfo(jsonObject);
//定义list集合,存储从数据库查询出的所有记录
List<Menu> menuList = new ArrayList<Menu>();
// String id, String pid, String path, String redirect, String name, String component, Boolean alwaysShow, String menu
//遍历集合
for (JSONObject json:jsonlist){
//手动封装Menu实体类对象
Menu menu = new Menu(
json.getString("id"),
json.getString("pid"),
json.getString("path"),
json.getString("redirect"),
json.getString("name"),
json.getString("component"),
json.getBoolean("alwaysShow"),
json.getString("menu"),
new Meta(json.getString("title"),json.getString("icon"))
);
//添加每个实体类对象到list集合
menuList.add(menu);
}
//调用TreeTest工具类方法生成树形结构的List集合
List<Menu> treeList = TreeUtil.listToTree(menuList);
//使用fastjson对树形list件序列化转成json字符串,过滤掉属性值为null的属性
String message = JSON.toJSONString(treeList,SerializerFeature.PrettyFormat);
//重新将json字符串转成jsonObject对象,返回给前端
returnData.put("message",JSON.parse(message));
return CommonUtil.successJson(returnData);
}
}
TreeUtil工具类
package com.heeexy.example.util;
import com.heeexy.example.entity.Menu;
import java.util.ArrayList;
import java.util.List;
//用List构建带有层次结构的json数据
//List父子节点构造树形Json
public class TreeUtil {
//将list集合转成树形结构的list集合
public static List<Menu> listToTree(List<Menu> list) {
//用递归找子。
List<Menu> treeList = new ArrayList<Menu>();
for (Menu tree : list) {
if (tree.getPid().equals("0")) {
treeList.add(findChildren(tree, list));
}
}
return treeList;
}
//寻找子节点
private static Menu findChildren(Menu tree, List<Menu> list) {
for (Menu node : list) {
if (node.getPid().equals(tree.getId())) {
if (tree.getChildren() == null) {
tree.setChildren(new ArrayList<Menu>());
}
tree.getChildren().add(findChildren(node, list));
}
}
return tree;
}
}
数据库中查询出的记录,封装成javabean的结果集
Menu{id='1', pid='0', path='/system', redirect='noredirect', name='功能模块', component='Layout', alwaysShow=true, menu='null', meta=Meta{title='功能模块', icon='tree'}, children=null}
Menu{id='2', pid='1', path='article', redirect='null', name='文章', component='article/article', alwaysShow=null, menu='null', meta=Meta{title='文章', icon='example'}, children=null}
Menu{id='3', pid='0', path='/manage', redirect='noredirect', name='系统管理', component='Layout', alwaysShow=true, menu='null', meta=Meta{title='系统管理', icon='table'}, children=null}
Menu{id='4', pid='3', path='user', redirect='null', name='用户列表', component='user/user', alwaysShow=null, menu='null', meta=Meta{title='用户列表', icon='user'}, children=null}
Menu{id='5', pid='3', path='role', redirect='null', name='权限管理', component='user/role', alwaysShow=null, menu='null', meta=Meta{title='权限管理', icon='password'}, children=null}
Menu{id='6', pid='3', path='test', redirect='null', name='测试', component='test/test', alwaysShow=null, menu='null', meta=Meta{title='测试', icon='test'}, children=null}
{"returnCode":"100","returnMsg":"请求成功","returnData":{"message":[{"redirect":"noredirect","path":"/system","component":"Layout","children":[{"path":"article","component":"article/article","meta":{"icon":"example","title":"文章"},"name":"文章","pid":"1","id":"2"}],"meta":{"icon":"tree","title":"功能模块"},"name":"功能模块","pid":"0","id":"1","alwaysShow":true},{"redirect":"noredirect","path":"/manage","component":"Layout","children":[{"path":"user","component":"user/user","meta":{"icon":"user","title":"用户列表"},"name":"用户列表","pid":"3","id":"4"},{"path":"role","component":"user/role","meta":{"icon":"password","title":"权限管理"},"name":"权限管理","pid":"3","id":"5"},{"path":"test","component":"test/test","meta":{"icon":"test","title":"测试"},"name":"测试","pid":"3","id":"6"}],"meta":{"icon":"table","title":"系统管理"},"name":"系统管理","pid":"0","id":"3","alwaysShow":true}]}}
返回给前端的树形json
{
"returnCode": "100",
"returnMsg": "请求成功",
"returnData": {
"message": [
{
"redirect": "noredirect",
"path": "/system",
"component": "Layout",
"children": [
{
"path": "article",
"component": "article/article",
"meta": {
"icon": "example",
"title": "文章"
},
"name": "文章",
"pid": "1",
"id": "2"
}
],
"meta": {
"icon": "tree",
"title": "功能模块"
},
"name": "功能模块",
"pid": "0",
"id": "1",
"alwaysShow": true
},
{
"redirect": "noredirect",
"path": "/manage",
"component": "Layout",
"children": [
{
"path": "user",
"component": "user/user",
"meta": {
"icon": "user",
"title": "用户列表"
},
"name": "用户列表",
"pid": "3",
"id": "4"
},
{
"path": "role",
"component": "user/role",
"meta": {
"icon": "password",
"title": "权限管理"
},
"name": "权限管理",
"pid": "3",
"id": "5"
},
{
"path": "test",
"component": "test/test",
"meta": {
"icon": "test",
"title": "测试"
},
"name": "测试",
"pid": "3",
"id": "6"
}
],
"meta": {
"icon": "table",
"title": "系统管理"
},
"name": "系统管理",
"pid": "0",
"id": "3",
"alwaysShow": true
}
]
}
}