根据登录id查询对应菜单选项

菜单选项都放在一张表,tongguo用户id查询对应菜单,在后台通过递归,形成树桩结构

 

工具:IntelliJ IDEA  SQLyog (Mysql)

SQL

/*
SQLyog Ultimate v12.08 (64 bit)
MySQL - 5.5.28 : Database - ppc
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`ppc` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `ppc`;

/*Table structure for table `admin_tab` */

DROP TABLE IF EXISTS `admin_tab`;

CREATE TABLE `admin_tab` (
  `id` int(11) NOT NULL,
  `name` varchar(10) NOT NULL,
  `password` varchar(255) NOT NULL,
  `createdate` datetime DEFAULT NULL,
  `updatedate` datetime DEFAULT NULL,
  `operator` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `admin_tab` */

insert  into `admin_tab`(`id`,`name`,`password`,`createdate`,`updatedate`,`operator`) values (1,'superadmin','123456','2018-12-29 21:51:33','2018-12-29 21:51:37','superadmin'),(2,'admin','123456','2018-12-29 22:33:07','2018-12-29 22:33:10','ppc'),(3,'a','123456','2019-01-21 10:39:45','2019-01-21 10:39:48','ppc');

/*Table structure for table `menu_tab` */

DROP TABLE IF EXISTS `menu_tab`;

CREATE TABLE `menu_tab` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  `parentId` int(11) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `createdate` datetime DEFAULT NULL,
  `updatedate` datetime DEFAULT NULL,
  `operator` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `menu_tab` */

insert  into `menu_tab`(`id`,`name`,`parentId`,`url`,`createdate`,`updatedate`,`operator`) values (1,'主菜单',0,'www.baidu.com','2018-12-29 22:04:18','2018-12-29 22:04:21','ppc'),(4,'二级子菜单2-1',2,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(3,'一级子菜单2',1,'www.baidu.com','2018-12-29 22:05:49','2018-12-29 22:05:53','ppc'),(5,'二级子菜单2-2',2,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(6,'二级子菜单2-3',2,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(2,'一级子菜单1',1,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(7,'二级子菜单3-1',3,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(8,'二级子菜单3-2',3,'www.baidu.com','2018-12-29 22:05:10','2018-12-29 22:05:12','ppc'),(9,'三级子菜单4-1',4,'www.baidu.com','2018-12-29 22:09:15','2018-12-29 22:09:18','ppc');

/*Table structure for table `role_admin_tab` */

DROP TABLE IF EXISTS `role_admin_tab`;

CREATE TABLE `role_admin_tab` (
  `id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  `admin_id` int(11) DEFAULT NULL,
  `createdate` datetime DEFAULT NULL,
  `updatedate` datetime DEFAULT NULL,
  `operator` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `role_admin_tab` */

insert  into `role_admin_tab`(`id`,`role_id`,`admin_id`,`createdate`,`updatedate`,`operator`) values (1,1,2,'2018-12-29 22:34:04','2018-12-29 22:34:08','ppc'),(2,1,1,'2019-01-21 10:35:35','2019-01-21 10:35:37','ppc'),(3,2,3,'2019-01-21 10:40:08','2019-01-21 10:40:10','ppc');

/*Table structure for table `role_menu_tab` */

DROP TABLE IF EXISTS `role_menu_tab`;

CREATE TABLE `role_menu_tab` (
  `id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  `menu_id` int(11) DEFAULT NULL,
  `createdate` datetime DEFAULT NULL,
  `updatedate` datetime DEFAULT NULL,
  `operator` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `role_menu_tab` */

insert  into `role_menu_tab`(`id`,`role_id`,`menu_id`,`createdate`,`updatedate`,`operator`) values (1,1,2,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(2,1,4,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(3,1,9,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(4,1,5,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(5,1,6,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(6,1,1,'2018-12-29 22:36:21','2018-12-29 22:36:23','ppc'),(7,2,1,'2019-01-21 10:38:45','2019-01-21 10:39:03','ppc'),(8,2,2,'2019-01-21 10:38:48','2019-01-21 10:39:05','ppc'),(9,2,3,'2019-01-21 10:38:58','2019-01-21 10:39:08','ppc'),(10,2,4,'2019-01-21 10:39:01','2019-01-21 10:39:11','ppc');

/*Table structure for table `role_tab` */

DROP TABLE IF EXISTS `role_tab`;

CREATE TABLE `role_tab` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  `createdate` datetime DEFAULT NULL,
  `updatedate` datetime DEFAULT NULL,
  `operator` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `role_tab` */

insert  into `role_tab`(`id`,`name`,`createdate`,`updatedate`,`operator`) values (1,'管理员1','2018-12-29 22:28:36','2018-12-29 22:28:41','ppc'),(2,'管理员2','2018-12-29 22:31:29','2018-12-29 22:31:32','ppc'),(0,'管理员0','2019-01-08 14:06:19','2019-01-08 14:06:19','ppc');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

代码

Mybatis





  
    
    
    
    
    
    
  
  
    id, username, parentId, createdate, updatedate,operator
  
  

Dao层

package com.example.operationplatform.dao;

import com.example.operationplatform.entity.Menu;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("menuDao")
public interface MenuDao {
    List findMenuByAdminId(Integer adminId);
}

entity

package com.example.operationplatform.entity;

import com.sun.javafx.beans.IDProperty;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

public class Menu {

    private int id;
    private String name;
    private int parentId;
    private String url;
    private Timestamp createDate;
    private Timestamp updateDate;
    private String operator;
    private List list;

    public Menu() {
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Menu(int id, String name, int parentId, String url, Timestamp createDate, Timestamp updateDate, String operator, List list) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.url = url;
        this.createDate = createDate;
        this.updateDate = updateDate;
        this.operator = operator;
        this.list = list;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    public Timestamp getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Timestamp updateDate) {
        this.updateDate = updateDate;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Menu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", parentId=" + parentId +
                ", url='" + url + '\'' +
                ", createDate=" + createDate +
                ", updateDate=" + updateDate +
                ", operator='" + operator + '\'' +
                ", list=" + list +
                '}';
    }
}

service、实现类

 

package com.example.operationplatform.service;

import com.example.operationplatform.entity.Menu;

import java.util.List;

public interface MenuService {
    List findMenuByAdminId(Integer adminId);

}
package com.example.operationplatform.service.impl;

import com.example.operationplatform.dao.MenuDao;
import com.example.operationplatform.entity.Menu;
import com.example.operationplatform.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("menuService")
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List findMenuByAdminId(Integer adminId) {
        List list =menuDao.findMenuByAdminId(adminId);
        list = sortTree(list,0);
        px(list);
        return list;
    }

    private List  sortTree(List list,int parentId){//递归塞值
        List returnList = new ArrayList<>();
        for (Menu menu:list) {
            if(parentId==menu.getParentId()){
                menu.setList(sortTree(list,menu.getId()));
                returnList.add(menu);
            }
        }
        return returnList;
    }

    private void px(List nlist) {//排序
        for (Menu n : nlist) {
            px(n.getList());

        }
        Collections.sort(nlist, new Comparator() {
            public int compare(Menu o1, Menu o2) {
                return o1.getId()-o2.getId();
            }
        });
    }
}

controller

package com.example.operationplatform.controller;

import com.example.operationplatform.entity.Menu;
import com.example.operationplatform.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/byAdminId")
    public List findMenuByAdminId(int adminId){
        return menuService.findMenuByAdminId(adminId);
    }
}

启动项目

查询Id:2

[{
	"id": 1,
	"name": "主菜单",
	"parentId": 0,
	"url": null,
	"createDate": "2018-12-29T14:04:18.000+0000",
	"updateDate": "2018-12-29T14:04:21.000+0000",
	"operator": "ppc",
	"list": [{
		"id": 2,
		"name": "一级子菜单1",
		"parentId": 1,
		"url": null,
		"createDate": "2018-12-29T14:05:10.000+0000",
		"updateDate": "2018-12-29T14:05:12.000+0000",
		"operator": "ppc",
		"list": [{
			"id": 4,
			"name": "二级子菜单2-1",
			"parentId": 2,
			"url": null,
			"createDate": "2018-12-29T14:05:10.000+0000",
			"updateDate": "2018-12-29T14:05:12.000+0000",
			"operator": "ppc",
			"list": [{
				"id": 9,
				"name": "三级子菜单3-1",
				"parentId": 4,
				"url": null,
				"createDate": "2018-12-29T14:09:15.000+0000",
				"updateDate": "2018-12-29T14:09:18.000+0000",
				"operator": "ppc",
				"list": []
			}]
		}, {
			"id": 5,
			"name": "二级子菜单2-2",
			"parentId": 2,
			"url": null,
			"createDate": "2018-12-29T14:05:10.000+0000",
			"updateDate": "2018-12-29T14:05:12.000+0000",
			"operator": "ppc",
			"list": []
		}, {
			"id": 6,
			"name": "二级子菜单2-3",
			"parentId": 2,
			"url": null,
			"createDate": "2018-12-29T14:05:10.000+0000",
			"updateDate": "2018-12-29T14:05:12.000+0000",
			"operator": "ppc",
			"list": []
		}]
	}]
}]

 

你可能感兴趣的:(后端)