您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Layui连接mysql操作CRUD

2025/4/2 10:14:30发布49次查看
一、使用layui框架实现权限登陆显示树形菜单
1、首先引用一下layui框架所需的包
2、创建二星权限显现树形菜单所需要的表和所需要操作的书本类型表
用户表t_xm_user
树形菜单展现所需的权限表 t_book_menu
中间权限表t_usermenuid
书本类型表t_book_category2
准备下面代码中所需要的包工具包:
https://pan.baidu.com/s/1xnwiojqudyw0cjads5pddw
在项目中配置与mysql数据库连接
3、权限登陆
userdao类
package com.ht.dao;import java.sql.sqlexception;import java.util.list;import java.util.map;import com.ht.daoimpl.iuserdao;import com.ht.util.jsonbasedao;import com.ht.util.jsonutils;import com.ht.util.pagebean;import com.ht.util.stringutils;public class userdao extends jsonbasedao implements iuserdao{    /**     * 登陆查询用户表     * @param pamap     * @param pagebean     * @return     * @throws sqlexception      * @throws illegalaccessexception      * @throws instantiationexception      */    public list<map<string,object>> list(map<string,string[]> pamap,pagebean pagebean) throws instantiationexception, illegalaccessexception, sqlexception{        string sql=select * from t_xm_user where true;        string user_name=jsonutils.getparamval(pamap, user_name);        string user_pwd=jsonutils.getparamval(pamap, user_pwd);        if (stringutils.isnotblank(user_name)) {            sql= sql + and user_name =+user_name;        }        if (stringutils.isnotblank(user_pwd)) {            sql= sql + and user_pwd =+user_pwd;        }        return super.executequery(sql, pagebean);    }    /**     * 通过中间表查询登陆用户所对应的权限     * @param pamap     * @param pagebean     * @return     * @throws instantiationexception     * @throws illegalaccessexception     * @throws sqlexception     */    public list<map<string,object>> listmenu(string user_id,pagebean pagebean) throws instantiationexception, illegalaccessexception, sqlexception{        string sql=select * from t_usermenuid where true;        if (stringutils.isnotblank(user_id)) {            sql= sql + and user_id =+user_id;        }        return super.executequery(sql, pagebean);    }}
web层
useraction类
package com.ht.web;import java.util.hashmap;import java.util.list;import java.util.map;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import com.fasterxml.jackson.databind.objectmapper;import com.ht.dao.userdao;import com.zking.framework.actionsupport;import com.ht.util.pagebean;import com.ht.util.responseutil;import com.ht.util.stringutils;public class useraction extends actionsupport {    private userdao uesrdao = new userdao();    public string login(httpservletrequest req, httpservletresponse rep) {        try {            list<map<string, object>> list = this.uesrdao.list(req.getparametermap(), null);            if (list != null && list.size() > 0) {                list<map<string, object>> listmenu = this.uesrdao.listmenu(req.getparameter(user_name), null);                stringbuffer sb = new stringbuffer();                for (map<string, object> map : listmenu) {                    sb.append(, + map.get(menuid));                }                req.getsession().setattribute(menuhid, sb.substring(1));                return index;            }        } catch (exception e) {            // todo auto-generated catch block            e.printstacktrace();        }        return login;    }    /**     * datagrid所需数据后端程序员开发完毕     * @param req     * @param resp     * @return     */    public string list(httpservletrequest req,httpservletresponse resp){        try {            pagebean pagebean=new pagebean();            pagebean.setrequest(req);            list<map<string, object>> list = this.uesrdao.list(req.getparametermap(), pagebean);            objectmapper om=new objectmapper();            //数据格式转换            map<string, object> map=new hashmap<>();            map.put(total, pagebean.gettotal());            map.put(rows, list);            responseutil.write(resp, om.writevalueasstring(map));        } catch (exception e) {            // todo auto-generated catch block            e.printstacktrace();        }         return null;    }    }
login.jsp 显示登陆界面
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%><!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>    <link href="${pagecontext.request.contextpath }/css/shop.css" type="text/css" rel="stylesheet"/>    <link href="${pagecontext.request.contextpath }/css/skin.css" rel="stylesheet" type="text/css"/>    <link href="${pagecontext.request.contextpath }/css/sellerber.css" type="text/css" rel="stylesheet" />    <link href="${pagecontext.request.contextpath }/css/bkg_ui.css" type="text/css" rel="stylesheet" />    <link href="${pagecontext.request.contextpath }/css/font-awes0ome.min.css" rel="stylesheet" type="text/css" />    <script src="${pagecontext.request.contextpath }/static/js/jquery-1.9.1.min.js" type="text/javascript"></script>    <script src="${pagecontext.request.contextpath }/static/js/layer.js" type="text/javascript"></script>    <script src="${pagecontext.request.contextpath }/static/js/bootstrap.min.js" type="text/javascript"></script>    <script src="${pagecontext.request.contextpath }/static/js/sellerber.js" type="text/javascript"></script>    <script src="${pagecontext.request.contextpath }/static/js/shopframe.js" type="text/javascript"></script>    <script type="text/javascript" src="${pagecontext.request.contextpath }/static/js/jquery.cookie.js"></script>    <script type="text/javascript" src="${pagecontext.request.contextpath }/js/login.js"></script>    <title>用户登录</title></head><body class="login-layout reg_log_style" onload="mya()">    <p class="logintop">        <span>欢迎后台管理界面平台</span>    </p>    <p class="loginbody">        <p class="login-container">            <p class="center">                <img src="images/logo.png" />            </p>            <p class="space-6"></p>            <p class="position-relative">                <p id="login-box" class="login-box widget-box no-border visible">                    <p class="login-main">                        <p class="clearfix">                            <p class="login_icon">                                <img src="images/login_img.png" />                            </p>                            <form action="${pagecontext.request.contextpath}/useraction.action?methodname=login" method="post" style="width: 300px; float: right; margin-right: 50px;" onsubmit="return vlogin()">                                <h4 class="title_name">                                    <img src="images/login_title.png" />                                </h4>                                <fieldset>                                    <ul>                                        <li class="frame_style form_error"><label class="user_icon"></label> <input name="user_name" type="text" placeholder="请输入用户名" id="username" /><span id="username" style="color: red;"></span></li>                                        <li class="frame_style form_error"><label class="password_icon"></label> <input name="user_pwd" type="password" placeholder="请输入密码" id="userpwd" /><span id="userpwd" style="color: red;"></span></li>                                        <li class="frame_style form_error"><label class="codes_icon"></label><input type="text" id="yzm" placeholder="请输入正确验证码"/>                                        <p class="codes_region">                                                <span id="yz" style="color: red;">x x x x</span>                                            </p></li>                                    </ul>                                    <p class="space"></p>                                    <p class="clearfix">                                        <input type="submit" class="login_btn" value="登陆" />                                    </p>                                    <p class="space-4"></p>                                </fieldset>                            </form>                        </p>                </p>                <!-- /login-box -->            </p>            <!-- /position-relative -->        </p>    </p>    </p></body></html>
配置xml
<action path="/useraction" type="com.ht.web.useraction">        <forward name="login" path="/login.jsp" redirect="false" />    </action>
效果如下:
4、树形菜单显示
对树形菜单的格式进行描述  写一个treenode 类
package com.ht.entity;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;public class treenode {    private string id;    private string name;    private map<string, object> attributes = new hashmap<>();    private list<treenode> children = new arraylist<>();    public string getid() {        return id;    }    public void setid(string id) {        this.id = id;    }    public string getname() {        return name;    }    public void setname(string name) {        this.name = name;    }    public map<string, object> getattributes() {        return attributes;    }    public void setattributes(map<string, object> attributes) {        this.attributes = attributes;    }    public list<treenode> getchildren() {        return children;    }    public void setchildren(list<treenode> children) {        this.children = children;    }    public treenode(string id, string text, map<string, object> attributes, list<treenode> children) {        super();        this.id = id;        this.name = name;        this.attributes = attributes;        this.children = children;    }    public treenode() {        super();    }    @override    public string tostring() {        return treenode [id= + id + , name= + name + , attributes= + attributes + , children= + children + ];    }}
dao层 menudao类
在dao类中使用了递归来转换josn格式,因为layui只能识别这种格式
package com.ht.dao;import java.sql.sqlexception;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;import com.ht.daoimpl.imenudao;import com.ht.entity.treenode;import com.ht.util.jsonbasedao;import com.ht.util.jsonutils;import com.ht.util.pagebean;import com.ht.util.stringutils;/** * 树形权限管理类 * @author administrator * */public class menudao extends jsonbasedao implements imenudao{    /**     *      * @param map   req.getparametermap     * @param pagebean  分页     * @return     * @throws exception      */    public list<treenode> list(map<string, string[]> map,pagebean pagebean) throws exception{        list<map<string, object>> listmenu=this.listmenusef(map, pagebean);        list<treenode> treenodelist=new arraylist<>();        menulist2treenodelist(listmenu, treenodelist);        return treenodelist;    }    /**     * 查询子节点     * @param map     * @param pagebean     * @return     * @throws exception     */    public list<map<string, object>> listmenusef(map<string, string[]> map,pagebean pagebean)throws exception{        string sql= select * from t_book_menu where true;        string id=jsonutils.getparamval(map, menuhid);        if(stringutils.isnotblank(id)) {            sql= sql +  and menuid in (+id+);            }        else {              sql= sql +  and menuid =-1;        }        return super.executequery(sql, pagebean);    }    /**     * 查询menu表的数据     * @param map     * @param pagebean     * @return     */    public list<map<string, object>> listmenu(map<string, string[]> map,pagebean pagebean)throws exception{        string sql= select * from t_book_menu where true;        string id=jsonutils.getparamval(map, id);        if(stringutils.isnotblank(id)) {            sql= sql +  and parentid =+id;            }        else {            sql= sql +  and parentid = -1;        }        return super.executequery(sql, pagebean);    }        /**     * {menuid:1,....[]}     * ->{id:1,....[]}     * menu表的数据不符合easyui树形展示的数据格式     * 需要转换成easyui所能识别的数据格式     * @param map     * @param retreenode     * @throws exception      */    public void menu2treenode(map<string, object> map, treenode treenode) throws exception {        treenode.setid(map.get(menuid).tostring());        treenode.setname(map.get(menuname).tostring());        treenode.setattributes(map);        map<string, string[]> jspmap=new hashmap<>();        jspmap.put(id, new string[] {treenode.getid()});        this.listmenu(jspmap, null);        list<map<string, object>> listmenu=this.listmenu(jspmap, null);        list<treenode> treenodelist=new arraylist<>();        menulist2treenodelist(listmenu, treenodelist);        treenode.setchildren(treenodelist);    }    /**     * [{menuid:1,....[]},{menuid:2,....[]}]     * ->[{id:1,....[]},{id:2,....[]}]     * @param maplist     * @param treenodelist     * @throws exception     */    public void menulist2treenodelist(list<map<string, object>> maplist, list<treenode> treenodelist)throws exception {        treenode treenode =null;        for (map<string, object> map : maplist) {            treenode =new treenode();            menu2treenode(map, treenode);            treenodelist.add(treenode);        }    }}
web层
menuaction类
package com.ht.web;import java.io.printwriter;import java.sql.sqlexception;import java.util.list;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import com.fasterxml.jackson.databind.objectmapper;import com.ht.biz.menubiz;import com.ht.bizipml.imenubiz;import com.ht.dao.menudao;import com.ht.entity.treenode;import com.zking.framework.actionsupport;import com.ht.util.responseutil;public class menuaction extends actionsupport {    private imenubiz menudao = new menubiz();    public string treemenu(httpservletrequest req, httpservletresponse response) throws exception {        @suppresswarnings(unchecked)        list<treenode> list = this.menudao.list(req.getparametermap(), null);        objectmapper om = new objectmapper();        string jsonstr = om.writevalueasstring(list);        response.setcontenttype(text/html;charset=utf-8);        printwriter out=response.getwriter();        out.println(jsonstr);        out.flush();        out.close();        return null;    }}
index.jsp  显示树形菜单
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%><html ><head>    <meta charset="utf-8">    <title></title>    <link rel="stylesheet" href="${pagecontext.request.contextpath }/static/js/layui/css/layui.css" media="all">    <script src="${pagecontext.request.contextpath }/static/js/layui/layui.js"></script>    <script type="text/javascript" src="${pagecontext.request.contextpath }/static/js/jquery-3.3.1.js"></script>    <script type="text/javascript" src="${pagecontext.request.contextpath }/static/js/layui/layui.all.js"></script>    <link rel="stylesheet" href="${pagecontext.request.contextpath }/static/js/layui/css/index.css">     <script type="text/javascript" src="${pagecontext.request.contextpath }/js/index.js"></script></head><body><input type="hidden" id="menuhid" value="${menuhid}"><p class="layui-layout layui-layout-admin " id="app">    <p class="layui-header">        <p class="layui-logo">后台管理界面</p>        <!-- 头部区域(可配合layui已有的水平导航) -->        <ul class="layui-nav layui-layout-right">            <li class="layui-nav-item"><a href="#" onclick="exit()">退出登录</a></li>        </ul>    </p>    <!-- 左边tree页面 -->    <p class="layui-side layui-bg-black">        <p class="layui-side-scroll ">            <p id="demo" ></p>        </p>    </p>    <!-- 选项卡要显示的地方 -->    <p class="layui-body">        <p class="layui-tab" lay-filter="tabs" lay-allowclose="true">            <ul class="layui-tab-title">            </ul>            <p class="layui-tab-content">            </p>        </p>    </p>    <p class="layui-footer" align="center" >        <!-- 底部固定区域 -->        © layui.com - 底部固定区域    </p></p></body></html>
配置xml
<action path="/menuaction" type="com.ht.web.menuaction">        <forward name="index" path="/index.jsp" redirect="false" />    </action>
效果图:
二、使用layui框架实现增,删,改,查
dao层,从数据库中拿出数据并且对其进行增,删,改,查操作
package com.ht.dao;import java.sql.sqlexception;import java.util.list;import java.util.map;import com.ht.daoimpl.ibooktypedao;import com.ht.util.jsonbasedao;import com.ht.util.jsonutils;import com.ht.util.pagebean;import com.ht.util.stringutils;/** * 书籍类别管理 * @author administrator * */public class booktypedao extends jsonbasedao implements ibooktypedao{    /**     * 编辑用户信息 查询书本类型表     * @param pamap     * @return     * @throws exception     */    public int edit(map<string, string[]> pamap) throws exception {        string sql = update t_book_category2 set book_category_name=? where book_category_id=?;        return super.executeupdate(sql, new string[] { book_category_name,book_category_id }, pamap);    }    /**     * 新增 查询书本类型表     * @param pamap     * @return     * @throws exception     */    public int add(map<string, string[]> pamap) throws exception {        string sql = insert into t_book_category2(book_category_name) values(?);        return super.executeupdate(sql, new string[] { book_category_name }, pamap);    }    /**     * 删除 查询书本类型表     * @param pamap     * @return     * @throws exception     */    public int remove(map<string, string[]> pamap) throws exception {        string sql = delete from t_book_category2 where  book_category_id=?;        return super.executeupdate(sql, new string[] { book_category_id }, pamap);    }    /**     * 查询的方法     * @param pamap     * @param pagebean     * @return     * @throws instantiationexception     * @throws illegalaccessexception     * @throws sqlexception     */    public list<map<string, object>> select(map<string, string[]> pamap, pagebean pagebean)            throws instantiationexception, illegalaccessexception, sqlexception {        string sql = select * from t_book_category2 where true;        string book_category_name = jsonutils.getparamval(pamap, book_category_name);        if (stringutils.isnotblank(book_category_name)) {            sql = sql +  and book_category_name  like '% + book_category_name + %';        }        return super.executequery(sql, pagebean);    }}
web层
booktypeaction类
package com.ht.web;import java.util.hashmap;import java.util.list;import java.util.map;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import com.fasterxml.jackson.databind.objectmapper;import com.ht.biz.booktypebiz;import com.ht.bizipml.ibooktypebiz;import com.ht.dao.booktypedao;import com.zking.framework.actionsupport;import com.ht.util.pagebean;import com.ht.util.responseutil;public class booktypeaction extends actionsupport{    private ibooktypebiz booktypedao=new booktypebiz();    /**     * 查询的请求方法     * @param req     * @param resp     * @return     */    public string select(httpservletrequest req,httpservletresponse resp) {        try {            pagebean pagebean=new pagebean();            pagebean.setrequest(req);            list<map<string, object>> list = this.booktypedao.select(req.getparametermap(), pagebean);            objectmapper om=new objectmapper();            map<string, object> map=new hashmap<>();            map.put(total, pagebean.gettotal());            map.put(rows, list);            responseutil.write(resp, om.writevalueasstring(map));        } catch (exception e) {            // todo: handle exception            e.printstacktrace();        }        return index;            }    /**     * form组件提交所需数据后端程序员处理完毕     * @param req     * @param resp     * @return     */    public string edit(httpservletrequest req,httpservletresponse resp){        try {            int edit = this.booktypedao.edit(req.getparametermap());            objectmapper om=new objectmapper();            responseutil.write(resp, om.writevalueasstring(edit));        } catch (exception e) {            // todo auto-generated catch block            e.printstacktrace();        }         return null;    }    /**     * 新增的请求方法     * @param req     * @param resp     * @return     */    public string add(httpservletrequest req,httpservletresponse resp){        try {            int add = this.booktypedao.add(req.getparametermap());            objectmapper om=new objectmapper();            responseutil.write(resp, om.writevalueasstring(add));        } catch (exception e) {            // todo auto-generated catch block            e.printstacktrace();        }         return null;    }    /**     * 删除的请求方法     * @param req     * @param resp     * @return     */    public string remove(httpservletrequest req,httpservletresponse resp) {        try {            int remove=this.booktypedao.remove(req.getparametermap());            objectmapper om=new objectmapper();            responseutil.write(resp, om.writevalueasstring(remove));        } catch (exception e) {            // todo: handle exception            e.printstacktrace();        }        return null;    }}
前台代码,用layui框架规划显示的页面
usermanage.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%><html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"><link rel="stylesheet" href="${pagecontext.request.contextpath }/static/js/layui/css/layui.css"><script src="${pagecontext.request.contextpath }/static/js/layui/layui.js"></script><script type="text/javascript" src="${pagecontext.request.contextpath }/static/js/jquery-3.3.1.js"></script><script type="text/javascript" src="${pagecontext.request.contextpath }/static/js/usermanage.js"></script><title>书记类别管理</title></head><body class="child-body"><p class="child-nav">    <span class="layui-breadcrumb">         <a>书籍类别管理</a>         <a><cite>分类列表</cite></a>    </span></p><blockquote class="layui-elem-quote"><!--搜索维度 --> <p class="layui-form">    <p class="layui-form-item">        <label class="layui-form-label">书籍名称</label>        <p class="layui-input-inline">            <input type="text" id="book_category_name" name="book_category_name" lay-verify="required" placeholder="请输入书籍名" autocomplete="true" class="layui-input">        </p>        <button class="layui-btn layui-btn-normal layui-btn-radius" data-type="reload"><i class="layui-icon"></i>查询</button>        <button class="layui-btn layui-btn-normal" data-type="add">新建</button>    </p></p>   </blockquote><!--隐藏域传值 --><input type="hidden" id="ht" value="${pagecontext.request.contextpath}" ><!--根据table id 来展示表格数据 --><table class="layui-hide" id="test" lay-filter="test"></table><!--行内样式按钮 --><script type="text/html" id="linebtns">  <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon"></i>编辑</a>  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a></script><!--弹出层 -->     <p class="site-text" style="margin: 5%; display: none" id="bt" target="test123">        <form class="layui-form layui-form-pane" onsubmit="return false" id="booktype">            <p class="layui-form-item">               <label class="layui-form-label">类型编号</label>                <p class="layui-input-block">                    <input type="text" class="layui-input layui-disabled text_add " id="book_category_id" name="book_category_id" disabled="disabled">                </p>                <br>                <label class="layui-form-label">书本类别名</label>                <p class="layui-input-block">                    <input type="text" class="layui-input" id="book_category_name" name="book_category_name"><br>                </p>            </p>        </form>    </p> </body></html>
usermanage.js
处理从后台传来的数据,并且根据前端代码进行展示
//执行渲染layui.use(['table','layer','form'],function(){    var data=document.getelementbyid(ht).value;    var table =layui.table;    var layer=layui.layer;    var form = layui.form;     /*展示数据表格  */    table.render({          elem:'#test'//表格id        ,url:data+'/booktypeaction.action?methodname=select'//所对应调用的接口        ,method:'post'        //提交方式        ,cols:[[            /*根据数据库的表格所对应的名称  */             {field:'book_category_id',height:80, width:300, title: '书籍类别序号', sort: true}             ,{field:'book_category_name', height:80,width:300, title: '书籍类别名称'}             ,{field:'createtime',height:80, width:300, title: '当前时间',templet:<p>{{layui.util.todatestring(d.createtime, 'yyyy-mm-dd hh:mm:ss')}}</p>}             ,{field:'right',height:80, width:300, title: '操作', toolbar:'#linebtns'}//操作栏         ]]             ,page:'true'//分页             , id: 'testreload'    });        //上方菜单操作栏(查询、以及  增加  按钮  )    var $ = layui.$, active = {            //查询            reload: function () {                var book_category_name = $('#book_category_name');//书籍类别名称 根据 id来取值                console.log(booktypename.val());                // 执行重载                table.reload('testreload', {                    page: {                        curr: 1                        // 重新从第 1 页开始                    },                    where: {                        key: 'book_category_name',                        book_category_name: book_category_name.val(),                    }                });            }, add: function () { //添加                layer.open({//弹出框                    type: 1,                    title: '添加书本类别',                    maxmin: true,                    shadeclose: true, //点击遮罩关闭层                    area: ['80%', '80%'],                    content: $('#box1'),                    btn: ['确定', '取消'],                    yes: function (index, layero) {//确定执行函数                        console.log(layero);                        //执行添加方法                        $.getjson(data+/booktypeaction.action?methodname=add, {                             booktypename: $(#book_category_name).val(), //书本类型名称                        }, function (data) {                            /*根据后台返回的参数来进行判断  */                            if (data==1) {                                layer.alert('添加成功', {icon: 1, title: '提示'}, function (i) {                                    layer.close(i);                                    layer.close(index);//关闭弹出层                                    $(#booktype)[0].reset()//重置form                                })                                table.reload('testreload', {//重载表格                                    page: {                                        curr: 1                                        // 重新从第 1 页开始                                    }                                })                            } else if(data==2){                                layer.msg('添加失败,请勿重复添加书本类别名称')                            }                        })                    }, cancel: function (index, layero) {//取消                        $(#booktype)[0].reset()//重置form  根据id                        layer.close(index)                    }                });            }    }    $('.layui-form .layui-btn').on('click', function () {        var type = $(this).data('type');        active[type] ? active[type].call(this) : '';    });                /*表格 行内操作(编辑  以及  删除 按钮操作)  */        table.on('tool(test)', function(obj){         var data = obj.data; //获得当前行数据         var urlex=document.getelementbyid(ht).value;         var tr=obj.tr//活动当前行tr 的  dom对象         console.log(data);         var layevent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)         if(layevent === 'del'){ //删除             layer.confirm('确定删除吗?',{title:'删除'}, function(index){                 //向服务端发送删除指令og                 $.getjson(urlex+'/booktypeaction.action?methodname=del',{book_category_id:data.book_category_id}, function(ret){                         layer.close(index);//关闭弹窗                         table.reload('testreload', {//重载表格                             page: {                                 curr: 1                                 // 重新从第 1 页开始                             }                         })                 });                 layer.close(index);             });         } else if(layevent === 'edit'){ //编辑             layer.open({                 type: 1 //page层类型                 ,skin: 'layui-layer-molv'                 ,area: ['380px', '270px']                 ,title: ['编辑书本类别信息','font-size:18px']                 ,btn: ['确定', '取消']                  ,shadeclose: true                 ,shade: 0 //遮罩透明度                 ,maxmin: true //允许全屏最小化                 ,content:$('#bt')  //弹窗id                 ,success:function(layero,index){                     $('#book_category_id').val(data.book_category_id);                     $('#book_category_name').val(data.book_category_name);                   },yes:function(index,layero){                    /*  $.ajaxsettings.async = false; */                      $.getjson(urlex+'/booktypeaction.action?methodname=edit',{                          book_category_id: $('#book_category_id').val(),                          book_category_name: $('#book_category_name').val(),                           book_category_id: data.book_category_id,                      },function(data){                      //根据后台返回的参数,来进行判断                          if(data>0){                              layer.alert('编辑成功',{icon:1,title:'提示'},function(i){                                  layer.close(i);                                  layer.close(index);//关闭弹出层                                  $(#booktype)[0].reset()//重置form                              })                              table.reload('testreload',{//重载表格                                  page:{                                      curr:1                                  }                              })                          }                      });                 }                                         });         }              });      });/** * 顶部搜索框 模糊查询 * @returns *//*function dingselect(){    var  select = $('#ks').val();    $(#dg).datagrid({        url : $(#txc).val()+'/booktypeaction.action?methodname=select&book_category_name='+select,    })}*//** * 查询增删改方法 * @returns *//*function usermanage(){        var url = booktypeaction.action?methodname=+$(#xm).attr(data);    $('#ff').form('submit', {           url:url,            success: function(param){                $('#dd').dialog('close');            $('#dg').datagrid('reload');            $('#ff').form('clear');        }        });  }*//** * 修改方法 * @returns *//*function edit(){    var row = $('#dg').datagrid('getselected');    if(row){        $('#ff').form('load', row);        $('#dd').dialog('open');        $('#xm').html('编辑');        $('#xm').attr(data,edit);    }}*//** * 新增方法 * @returns *//*function add(){    $('#ff').form('clear');    $('#dd').dialog('open');    $('#xm').html('新增');    $('#xm').attr(data,add);}*//** * 删除方法 * @returns *//*function remove(){    var data=$('#dg').datagrid('getselected');    $.ajax({        url : $(#txc).val()+'/booktypeaction.action?methodname=remove&book_category_id='+data.book_category_id,        success: function(param){                $('#dg').datagrid('reload');            }        })}*/
配置xml
<action path="/booktypeaction" type="com.ht.web.booktypeaction">        <forward name="index" path="/index.jsp" redirect="false" />        <forward name="usermanage" path="/usermanage.jsp" redirect="false" />    </action>
更多layui知识请关注layui教程栏目
以上就是layui连接mysql操作crud的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product