詳解Java快速上手用戶後臺管理系統

主要功能

管理員登錄功能、管理員列表操作、用戶列表操作、訂單管理

登錄功能

設計思路:前端頁面獲取輸入的數據,然後發送Ajax請求,在servlet中獲取數據並調用service層中的方法進行處理,service層中調用dao層的實現方法,最終servlet返回一個結果集,判斷登錄是否成功。

login.html主要功能代碼:

<script>
    $(function () {
        $("#btn_login").click(function () {
            //獲取數據
            let username = $("#username").val();
            let password = $("#password").val();
            //數據處理
            $.post("admin/login", {username: username, password: password}, function (result) {
                if (result.flag) {
                    location.href = "index.html";
                } else {
                    alert(result.errorMsg);
                }
            });
        });
    })
</script>

servlet中功能代碼:

    //登錄
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取用戶名和密碼
        String username = request.getParameter("username");
        String password = request.getParameter("password");
 
        //封裝管理員對象
        Administrator admin = new Administrator();
        admin.setAname(username);
        admin.setPassword(password);
 
        //創建結果對象
        ResultInfo resultInfo = new ResultInfo();
        //調用service查詢
        Administrator administrator = service.login(admin);
        if (administrator == null) {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("用戶名或密碼錯誤!");
        }
        if (administrator != null && !administrator.getStatus().equals("Y")) {
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("該賬戶未激活!");
        }
        if (administrator != null && administrator.getStatus().equals("Y")) {
            resultInfo.setFlag(true);
            //設置session
            request.getSession().setAttribute("username", administrator.getAname());
        }
 
        //調用父類中定義的方法,轉json數據後回傳
        writeValue(resultInfo, response);
    }

service層中代碼實現:

    @Override
    public Administrator login(Administrator administrator) {
        return dao.login(administrator);
    }

dao層中代碼實現:

    @Override
    public Administrator login(Administrator administrator) {
        Administrator admin = null;
        try {
            String sql = "SELECT * FROM administrator WHERE aname = ? AND PASSWORD = ?";
            admin = template.queryForObject(sql, new BeanPropertyRowMapper<>(Administrator.class), administrator.getAname(), administrator.getPassword());
        } catch (Exception e) {
 
        }
        return admin;
    }

實現效果:

管理員列表

設計思路:數據操作與登錄功能大同小異,實現瞭管理員的增刪改查,在此重點介紹分頁、模糊查詢。

admin_list.html主要功能代碼:

    <script>
        $(function () {
            let name = null;
            $("#btn_search").click(function () {
                name = $('#admin_name').val();
                load(null, name)
                //取消按鈕的默認跳轉行為 導致會刷新頁面
                return false;
            })
            load(null, name);
        });
        
        function load(currentPage, admin_name) {
            //發送ajax請求,請求route/pageQuery,傳遞cid
            $.get("admin/pageQuery", {currentPage: currentPage, admin_name: admin_name}, function (pb) {
                //定義字符
                let lis = "";
 
                //計算上一頁的頁碼
                let beforeNum = pb.currentPage - 1;
                if (beforeNum <= 0) {
                    beforeNum = 1;
                }
                let beforePage = '<a class="prev" href="javascript:load(' + beforeNum + ',\'' + admin_name + '\')" rel="external nofollow" >&lt;&lt;</a>';
                lis += beforePage;
 
                //定義開始位置begin,結束位置end
                let begin;
                let end;
 
                if (pb.totalPage < 10) {
                    //總頁碼不足10頁
                    begin = 1;
                    end = pb.totalPage;
                } else {
                    //總頁碼超過10頁
                    begin = pb.currentPage - 5;
                    end = pb.currentPage + 4;
 
                    //前邊不足5個,後邊補齊
                    if (begin < 1) {
                        begin = 1;
                        end = begin + 9;
                    }
 
                    if (end > pb.totalPage) {
                        end = pb.totalPage;
                        begin = end - 9;
                    }
                }
 
                //展示分頁頁碼
                for (let i = begin; i <= end; i++) {
                    let li;
                    //判斷當前頁碼是否等於i,創建頁碼的li
                    if (pb.currentPage === i) {
                        li = '<a class="num" href="javascript:load(' + i + ',\'' + admin_name + '\')" rel="external nofollow"  rel="external nofollow" ><span class="current">' + i + '</span></a>';
                    } else {
                        li = '<a class="num" href="javascript:load(' + i + ',\'' + admin_name + '\')" rel="external nofollow"  rel="external nofollow" >' + i + '</a>';
                    }
                    //拼接字符串
                    lis += li;
                }
 
                //計算下一頁的頁碼
                let nextNum = pb.currentPage + 1;
                if (nextNum >= pb.totalPage) {
                    nextNum = pb.totalPage;
                }
                let nextPage = '<a class="next" href="javascript:load(' + nextNum + ',\'' + admin_name + '\')" rel="external nofollow" >&gt;&gt;</a>';
                lis += nextPage;
 
                //將lis內容設置到ul
                $("#pageNum").html(lis);
 
                //列表數據展示
                let admin_lis = "";
                for (let i = 0; i < pb.list.length; i++) {
                    let admin = pb.list[i];
 
                    let li;
                    if (admin.status === "Y") {
                        li = '<tr>\n' +
                            '    <td>\n' +
                            '        <input type="checkbox" name="checkedId" id="checkedId" lay-skin="primary">\n' +
                            '        <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>' +
                            '    </td>\n' +
                            '    <td>' + admin.aid + '</td>\n' +
                            '    <td>' + admin.aname + '</td>\n' +
                            '    <td>' + admin.phone + '</td>\n' +
                            '    <td>' + admin.email + '</td>\n' +
                            '    <td>' + admin.role + '</td>\n' +
                            '    <td>' + admin.date + '</td>\n' +
                            '    <td class="td-status">\n' +
                            '        <span class="layui-btn layui-btn-normal layui-btn-mini" style="background-color: #1E9FFF">已啟用</span></td>\n' +
                            '    <td class="td-manage">\n' +
                            '        <a onclick="member_stop(this,' + admin.aid + ')" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  title="啟用">\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '        <a title="編輯" onclick="xadmin.open(\'編輯管理員\',\'admin-edit.html?aid=' + admin.aid + '\',600,500)" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '        <a title="刪除" onclick="member_del(this,\'要刪除的id\')" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '    </td>\n' +
                            '</tr>';
                    } else if (admin.status === "N") {
                        li = '<tr>\n' +
                            '    <td>\n' +
                            '        <input type="checkbox" name="checkedId" id="checkedId" lay-skin="primary">\n' +
                            '        <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>' +
                            '    </td>\n' +
                            '    <td>' + admin.aid + '</td>\n' +
                            '    <td>' + admin.aname + '</td>\n' +
                            '    <td>' + admin.phone + '</td>\n' +
                            '    <td>' + admin.email + '</td>\n' +
                            '    <td>' + admin.role + '</td>\n' +
                            '    <td>' + admin.date + '</td>\n' +
                            '    <td class="td-status">\n' +
                            '        <span class="layui-btn layui-btn-normal layui-btn-mini" style="background-color: gray">已停用</span></td>\n' +
                            '    <td class="td-manage">\n' +
                            '        <a onclick="member_stop(this,' + admin.aid + ')" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  title="停用">\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '        <a title="編輯" onclick="xadmin.open(\'編輯管理員\',\'admin-edit.html?aid=' + admin.aid + '\',600,500)" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '        <a title="刪除" onclick="member_del(this,\'要刪除的id\')" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >\n' +
                            '            <i class="layui-icon"></i>\n' +
                            '        </a>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }
                    admin_lis += li;
                }
                //設置列表數據
                $("#admin_msg").html(admin_lis);
            });
        }
    </script>

servlet中功能代碼:

    //分頁、模糊查詢
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收參數
        String currentPageStr = request.getParameter("currentPage");
        //接收admin_name管理員名稱
        String admin_name = request.getParameter("admin_name");
        //判斷admin_name是否為null
        if (admin_name != null && !"null".equals(admin_name) && admin_name.length() > 0) {
            admin_name = new String(admin_name.getBytes("iso-8859-1"), "utf-8");
        } else {
            admin_name = "";
        }
 
        int currentPage = 0; //當前頁碼,不傳遞參數,默認為1
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }
 
        //每頁顯示條數,默認為5
        int pageSize = 5;
 
        //調用service查詢PageBean對象
        PageBean<Administrator> pb = service.pageQuery(currentPage, pageSize, admin_name);
 
        //將pageBean對象序列化為json,返回
        writeValue(pb, response);
    }

service層中代碼實現:

    @Override
    public PageBean<Administrator> pageQuery(int currentPage, int pageSize, String admin_name) {
        //封裝PageBean
        PageBean<Administrator> pb = new PageBean<>();
        //設置當前頁碼
        pb.setCurrentPage(currentPage);
        //設置每頁顯示條數
        pb.setPageSize(pageSize);
 
        //設置總記錄數
        int totalCount = dao.findTotalCount(admin_name);
 
        pb.setTotalCount(totalCount);
        //設置當前頁顯示的數據集合
        int start = (currentPage - 1) * pageSize; //開始的記錄數
        List<Administrator> list = dao.findByPage(start, pageSize, admin_name);
        pb.setList(list);
 
        //設置總頁數  = 總記錄數/每頁顯示條數
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pb.setTotalPage(totalPage);
 
        return pb;
    }

dao層中代碼實現:

    @Override
    public int findTotalCount(String admin_name) {
        //定義sql模板
        String sql = "SELECT COUNT(*) FROM administrator WHERE 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        //條件
        List<Object> params = new ArrayList<>();
        if (admin_name != null && admin_name.length() > 0) {
            sb.append(" and aname like ? ");
            params.add("%" + admin_name + "%");
        }
        sql = sb.toString();
        return template.queryForObject(sql, Integer.class, params.toArray());
    }
 
    @Override
    public List<Administrator> findByPage(int start, int pageSize, String admin_name) {
        String sql = "SELECT * FROM administrator WHERE 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        //條件
        List<Object> params = new ArrayList<>();
        //判斷參數是否有值
        if (admin_name != null && admin_name.length() > 0) {
            sb.append(" and aname like ? ");
            params.add("%" + admin_name + "%");
        }
        //分頁條件
        sb.append(" limit ?,? ");
        params.add(start);
        params.add(pageSize);
        //轉字符串
        sql = sb.toString();
        return template.query(sql, new BeanPropertyRowMapper<>(Administrator.class), params.toArray());
    }

實現效果:

用戶列表

設計思路:與管理員列表設計思路基本類似。

實現效果:

訂單管理

設計思路:與管理員列表設計思路基本類似。

到此這篇關於詳解Java快速上手用戶後臺管理系統的文章就介紹到這瞭,更多相關Java 後臺管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: