角色管理
    ①概要设计
    在数据库中建立表格并创建对应实体类

    1. package domain;
    2. public class Role {
    3. private Integer rid;
    4. private String rname;
    5. private String rdescription;
    6. private String yl1;
    7. private String yl2;
    8. public Role() {
    9. }
    10. public Role(Integer rid, String rname, String rdescription, String yl1, String yl2) {
    11. this.rid = rid;
    12. this.rname = rname;
    13. this.rdescription = rdescription;
    14. this.yl1 = yl1;
    15. this.yl2 = yl2;
    16. }
    17. public Integer getRid() {
    18. return rid;
    19. }
    20. public void setRid(Integer rid) {
    21. this.rid = rid;
    22. }
    23. public String getRname() {
    24. return rname;
    25. }
    26. public void setRname(String rname) {
    27. this.rname = rname;
    28. }
    29. public String getRdescription() {
    30. return rdescription;
    31. }
    32. public void setRdescription(String rdescription) {
    33. this.rdescription = rdescription;
    34. }
    35. public String getYl1() {
    36. return yl1;
    37. }
    38. public void setYl1(String yl1) {
    39. this.yl1 = yl1;
    40. }
    41. public String getYl2() {
    42. return yl2;
    43. }
    44. public void setYl2(String yl2) {
    45. this.yl2 = yl2;
    46. }
    47. }

    image.png
    ②在角色列表页面,使用layui-table数据表格组件,动态请求数据,展示数据
    提供了一个用来展示数据的标签,在js中使用table组件请求数据,自动渲染数据

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" type="text/css" href="layui/css/layui.css"/>
    5. <script src="./layui/layui.all.js"></script>
    6. </head>
    7. <body>
    8. <%--角色列表--%>
    9. <table id="roleGrid"></table>
    10. <script>
    11. layui.use(['table'],function () {
    12. var table=layui.table;
    13. table.render(
    14. {
    15. elem: '#roleGrid',//绑定渲染位置
    16. url: 'roleFindAll',//底层使用ajax请求表格数据
    17. cols: [[
    18. {title: '角色编号', field: 'rid'},
    19. {title: '角色名称', field: 'rname'},
    20. {title: '角色描述', field: 'rdescription'},
    21. {title: '操作'}
    22. ]], //指定查询结果与表格单元之间的展示关系,看哪个属性展示在哪个位置
    23. page:true
    24. });
    25. })
    26. </script>
    27. </body>
    28. </html>

    ③服务端处理请求,根据分页条件,查询分页所需要的数据,这些数据要符合layui-table组件的数据结构要求

    1. package controller;
    2. import domain.LayPageInfo;
    3. import myweb.annotation.Controller;
    4. import myweb.annotation.RequestMapping;
    5. import myweb.annotation.RequestParam;
    6. import myweb.annotation.ResponseBody;
    7. import service.RoleService;
    8. @Controller
    9. public class RoleController {
    10. private RoleService roleService=new RoleService();
    11. @RequestMapping("/roleFindAll")
    12. @ResponseBody
    13. public LayPageInfo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
    14. return roleService.findByPage(page, limit);
    15. }
    16. }
    1. package service;
    2. import dao.RoleDao;
    3. import domain.LayPageInfo;
    4. import domain.Role;
    5. import orm.SqlSession;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map;
    9. public class RoleService {
    10. private RoleDao roleDao=new SqlSession().getMapper(RoleDao.class);
    11. public LayPageInfo findByPage(int page,int limit){
    12. //确保page下限
    13. page=Math.max(1,page);
    14. //确保page上限
    15. int count= roleDao.total();
    16. int max=(int)Math.ceil(1.0*count/limit);
    17. max=Math.max(1,max);//有可能为0
    18. page=Math.min(page, max);
    19. int start=(page-1)*limit;
    20. int length=limit;
    21. Map<String,Integer>param=new HashMap<>();
    22. param.put("start",start);
    23. param.put("length",length);
    24. List<Role> roleList= roleDao.findByPage(param);
    25. return new LayPageInfo(roleList,count,"",0) ;
    26. }
    27. }
    1. package dao;
    2. import domain.Role;
    3. import orm.annotation.Select;
    4. import java.util.List;
    5. import java.util.Map;
    6. public interface RoleDao {
    7. @Select("select count(*) from t_role")
    8. public int total();
    9. @Select("select * from t_role limit #{start},#{length}")
    10. public List<Role> findByPage(Map<String,Integer> param);
    11. }
    1. package domain;
    2. import java.util.List;
    3. //装载layui-table组件分页时的数据结构
    4. public class LayPageInfo {
    5. private List<?> data ;
    6. private Integer count ;
    7. private String msg ;
    8. private Integer code ;
    9. public List<?> getData() {
    10. return data;
    11. }
    12. public void setData(List<?> data) {
    13. this.data = data;
    14. }
    15. public int getCount() {
    16. return count;
    17. }
    18. public void setCount(Integer count) {
    19. this.count = count;
    20. }
    21. public String getMsg() {
    22. return msg;
    23. }
    24. public void setMsg(String msg) {
    25. this.msg = msg;
    26. }
    27. public Integer getCode() {
    28. return code;
    29. }
    30. public void setCode(Integer code) {
    31. this.code = code;
    32. }
    33. public LayPageInfo(List<?> data, Integer count, String msg, Integer code) {
    34. this.data = data;
    35. this.count = count;
    36. this.msg = msg;
    37. this.code = code;
    38. }
    39. public LayPageInfo() {
    40. }
    41. }

    ④表格单元使用自定义模板提供操作按钮
    image.png

    1. <%--表格单元的按钮模板--%>
    2. <script type="text/html" id="rowBtns">
    3. <button type="button" class="layui-btn layui-btn-danger layui-btn-sm">
    4. <i class="layui-icon">&#xe640;</i>删除</button>
    5. <button type="button" class="layui-btn layui-btn-warm layui-btn-sm" onclick="toUpdate()">
    6. <i class="layui-icon">&#xe642;</i>修改</button>
    7. </script>

    ⑤点击编辑按钮,弹出对话框,内有表单组件,完成编辑修改

    <!-- 弹出层中的表单模板 -->
            <form id="roleUpdateForm" class="layui-form" action="roleUpdate" style="display:none;padding-top:10px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="rid" id="rid" readonly  lay-verify="required" autocomplete="off" class="layui-input layui-input-inline">
                    </div>
                </div>
    
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="rname" id="rname" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input layui-input-inline">
                    </div>
                </div>
    
                <div class="layui-form-item">
                    <label class="layui-form-label">角色描述</label>
                    <div  class="layui-input-inline">
                        <input type="text" name="rdescription" id="rdescription" placeholder="请输入描述" autocomplete="off" class="layui-input">
                    </div>
                </div>
    
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="closeEditWin()">取消</button>
                    </div>
                </div>
    
            </form>
    
    function toUpdate(rid,rname,rdescription) {
                    layui.use(['layer'],function () {
                        var layer=layui.layer;
                        var $=layui.jquery;
    
                        $('#rid').val(rid) ;//jQuery写法设置标签value属性
                        document.getElementById('rname').value = rname ;
                        $('#rdescription').val(rdescription);
    
                        layer.open({
                            type:1,//打开一个页面层,可以展示一些html效果
                            area:[400,300],
                            title:'修改角色',
                            content:$('#roleUpdateForm')//必须使用jQuery,jQuery根据id找到form模板,将其引入到弹出层
                        })
                    })
                }
    
                function closeEditWin() {
                    layui.use(['layer'],function () {
                        var layer=layui.layer;
                        layer.closeAll();
                    })
                }
    

    ⑥修改请求实现

    @RequestMapping("/roleUpdate")
        public String update(Role role){
            roleService.update(role);
            return "role.jsp";
        }
    
    public void update(Role role){
            roleDao.update(role);
        }
    
    @Update("update t_role set rname=#{rname} , rdescription=#{rdescription} where rid = #{rid}")
    public void update(Role role);