Javaweb菜单信息管理的一级菜单和二级菜单显示

对于⼀个系统⽽⾔,系统功能是由程序员进⾏开发和实现的,菜单相对⽐较固定的;如
果要新增⼀个菜单,则需要有对应的功能⽀撑,因此对于菜单的添加操作不会让管理员
来实现,⽽是由程序员实现的。因此菜单信息管理功能 主要就是对菜单的展示。
Javaweb菜单信息管理的一级菜单和二级菜单显示_第1张图片
数据库操作代码实现
查询所有的⼀级菜单
查询所有的⼆级菜单
根据⼀级菜单查询所有的⼆级菜单
1. 涉及数据表
菜单信息表 tb_menus
2.SQL 指令
-- 1 查询所有⼀级菜单
select menu_id,menu_code,menu_name,menu_order,menu_level,menu_icon from
tb_menus where menu_level = 1 order by menu_order;
-- 2 查询所有的⼆级菜单
select
menu_id,menu_code,menu_name,menu_order,menu_level,parent_menu_code,menu_
url from tb_menus where menu_level = 2 order by menu_order;
-- 3 根据⼀级菜单 code 查询当前⼀级菜单下的⼆级菜单
select
menu_id,menu_code,menu_name,menu_order,menu_level,parent_menu_code,menu_
url from tb_menus where parent_menu_code = '01' order by menu_order;
3. MenuDAO 类中实现数据库操作
package com.it.dao;

import com.it.entity.Menu1;
import com.it.entity.Menu2;
import com.it.untils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//实现菜单的查询
public class MenuDAO {
    //根据管理员ID查询对应的一级菜单
    public List selectFirstLevelMenusById(String mgrId) throws SQLException {
        List menu1List = new ArrayList<>();
        String str="SELECT c.menu_id menuId,menu_code menuCode,menu_name menuName,menu_order menuOrder,menu_level menuLevel,menu_icon menuIcon FROM tb_mgr_role a " +
                "INNER JOIN tb_role_menu b INNER JOIN tb_menus c ON a.role_id=b.role_id AND b.menu_id=c.menu_id WHERE a.mgr_id=? AND menu_level=1 ORDER BY c.menu_order";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        menu1List= queryRunner.query(str, new BeanListHandler(Menu1.class), mgrId);
        return menu1List;
    }
    //根据管理员ID和一级菜单ID,查询当前一级菜单所拥有的二级菜单
    public List selectSecondLevelMenusByIdAndParentCode(String mgrId,String parentCode) throws SQLException {
        List menu2List = new ArrayList<>();
        String sql="SELECT c.menu_id menuId,menu_code menuCode,menu_name menuName,menu_order menuOrder,menu_level menuLevel,parent_menu_code parentMenuCode,menu_url menuUrl FROM tb_mgr_role a " +
                "INNER JOIN tb_role_menu b INNER JOIN tb_menus c ON a.role_id=b.role_id AND b.menu_id=c.menu_id WHERE a.mgr_id=? AND menu_level=2 AND parent_menu_code=? ORDER BY c.menu_order";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        menu2List=queryRunner.query(sql,new BeanListHandler(Menu2.class),mgrId,parentCode);
        return menu2List;
    }

    //查询所有的一级菜单
    public List selectMenu1() throws SQLException {
        List  menu1List = new ArrayList<>();
        String sql="SELECT menu_id menuId,menu_code menuCode,menu_name menuName,menu_order menuOrder,menu_level menuLevel,menu_icon menuIcon FROM tb_menus WHERE menu_level=1 ORDER BY menu_order";
        QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
        menu1List= queryRunner.query(sql, new BeanListHandler(Menu1.class));
        return  menu1List;
    }
    //查询所有的二级菜单
    public List selectMenu2() throws SQLException {
        List menu2List=new ArrayList<>();
        String sql="SELECT menu_id menuId,menu_code menuCode,menu_name menuName,menu_order menuOrder,menu_level menuLevel,parent_menu_code parentMenuCode,menu_url menuUrl FROM tb_menus WHERE menu_level=2 ORDER BY menu_code ";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        menu2List = queryRunner.query(sql, new BeanListHandler(Menu2.class));
        return menu2List;
    }
    //查询某一级菜单所拥有的二级菜单
    public List selectMenu2ByMenu1Code(String parentMenuCode) throws SQLException {
        List menu2List=new ArrayList<>();
       String sql="SELECT menu_id menuId,menu_code menuCode,menu_name menuName,menu_order menuOrder,menu_level menuLevel,parent_menu_code parentMenuCode,menu_url menuUrl FROM tb_menus " +
               "WHERE parent_menu_code=? AND menu_level=2 ORDER BY menu_code;";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        menu2List= queryRunner.query(sql, new BeanListHandler(Menu2.class), parentMenuCode);
        return menu2List;
    }
}
4业务逻辑层代码实现
MenuService 中实现两个业务
查询所有⼀级菜单和⼆级菜单
根据⼀级菜单查询⼆级菜单
package com.it.service;

import com.it.dao.MenuDAO;
import com.it.entity.Menu1;
import com.it.entity.Menu2;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuService {
    public List selectMenuByMgrId(String mgrId) throws SQLException {
        MenuDAO menuDAO = new MenuDAO();
        //根据管理员ID查询该管理员的一级菜单
        List menu1List = menuDAO.selectFirstLevelMenusById(mgrId);
        //查询每一个一级菜单中的二级菜单
        for (int i = 0; i < menu1List.size(); i++) {
            Menu1 menu1 = menu1List.get(i);
            List menu2s = menuDAO.selectSecondLevelMenusByIdAndParentCode(mgrId, menu1.getMenuCode());
            menu1.setChildMenus(menu2s);
        }
        return menu1List;
    }
    //查询所有的一级二级菜单
    public Map listMenus() throws SQLException {
        Map mapMenus = new HashMap<>();
        MenuDAO menuDAO = new MenuDAO();
        List menu1List = menuDAO.selectMenu1();
        List menu2List = menuDAO.selectMenu2();
        mapMenus.put("menu1List",menu1List);
        mapMenus.put("menu2List",menu2List);
        return mapMenus;
    }
    //查询某个一级菜单所拥有的二级菜单
    public List selectMenu2ByMenu1Code(String menu1Code) throws SQLException {
        List menu2List = new ArrayList<>();
        MenuDAO menuDAO = new MenuDAO();
        menu2List = menuDAO.selectMenu2ByMenu1Code(menu1Code);
        return menu2List;
    }
}
5创建 MenuListServlet
package com.it.servlets;

import com.it.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/MenuListServlet")
public class MenuListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuService menuService = new MenuService();
        try {
            Map map = menuService.listMenus();
            req.setAttribute("menu1List",map.get("menu1List"));
            req.setAttribute("menu2List",map.get("menu2List"));
            req.getRequestDispatcher("admin_menu_list.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
6. admin_menu_list.jsp 使⽤ JSTL+EL 显示菜单信息

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    管理员



一级菜单列表

菜单编号 菜单名称 菜单排序 菜单级别 菜单编号 菜单URL 状态 操作
${menu2.menuCode} ${menu2.menuName} ${menu2.menuOrder} ${menu2.menuLevel} ${menu2.parentMenuCode} ${menu2.menuUrl} 已启用

Javaweb菜单信息管理的一级菜单和二级菜单显示_第2张图片

 Javaweb菜单信息管理的一级菜单和二级菜单显示_第3张图片

 

你可能感兴趣的:(javaWeb,数据库,java,sql)