Layui三级菜单

本文主要介绍了如何使用Layui框架来处理三级菜单的数据操作。首先,文章详细展示了`DBAccess`类如何连接并管理MySQL数据库,包括读取配置文件和执行数据库操作的方法。接着,`BuildTree`类用于构建菜单树结构,它包含两个方法,分别用于创建指定顶级节点的树和默认顶级节点的树。`TreeVo`类作为树结构的节点,包含了节点ID、文本、状态等属性。在测试类`ShopDaoTest`中,对数据库查询和菜单数据转换为树结构进行了验证。最后,前端部分通过Ajax请求获取后台生成的菜单数据,并利用Layui的导航组件动态渲染成三级菜单。
摘要由CSDN通过智能技术生成

目录

一、准备工作

二、后台处理

三、前台处理


一、准备工作

数据库的连接与相关数据表

数据库的连接

#mysql8
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db_0701?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
user=root
pwd=123456


二、后台处理

Util包


public class DBAccess {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
 
    static {// 静态块执行一次,加载 驱动一次
        try {
            InputStream is = DBAccess.class
                    .getResourceAsStream("config.properties");
 
            Properties properties = new Properties();
            properties.load(is);
 
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("pwd");
 
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 获得数据连接对象
     * 
     * @return
     */
    public static Connection getConnection() {
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
 
    public static void close(ResultSet rs) {
        if (null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
 
    public static void close(Statement stmt) {
        if (null != stmt) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
 
    public static void close(Connection conn) {
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
 
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        close(rs);
        close(stmt);
        close(conn);
    }
 
    public static boolean isOracle() {
        return "oracle.jdbc.driver.OracleDriver".equals(driver);
    }
 
    public static boolean isSQLServer() {
        return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
    }
    
    public static boolean isMysql() {
        return "com.mysql.cj.jdbc.Driver".equals(driver);
    }
 
    public static void main(String[] args) {
        Connection conn = DBAccess.getConnection();
        System.out.println(conn);
        DBAccess.close(conn);
        System.out.println("isOracle:" + isOracle());
        System.out.println("isSQLServer:" + isSQLServer());
        System.out.println("isMysql:" + isMysql());
        System.out.println("数据库连接(关闭)成功");
    }
}
 

BuildTree类


 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class BuildTree {
 
    /**
     * 默认-1为顶级节点
     * @param nodes
     * @param <T>
     * @return
     */
    public static <T> TreeVo<T> build(List<TreeVo<T>> nodes) {
 
        if (nodes == null) {
            return null;
        }
        List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();
 
        for (TreeVo<T> children : nodes) {
            String pid = children.getParentId();
            if (pid == null || "-1".equals(pid)) {
                topNodes.add(children);
 
                continue;
            }
 
            for (TreeVo<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildren().add(children);
                    childre

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值