• 角色管理
      1) 概要设计
      t_role(
      rid
      rname
      rdescription //描述
      yl1
      yl2
      )
      主键如果是自增,最好是使用bigint
      image.png

      insert into t_role values(null,’管理员1’,’很牛’,null,null);
      insert into t_role values(null,’管理员2’,’很牛’,null,null);
      insert into t_role values(null,’管理员3’,’很牛’,null,null);
      insert into t_role values(null,’管理员4’,’很牛’,null,null);
      insert into t_role values(null,’管理员5’,’很牛’,null,null);
      insert into t_role values(null,’管理员6’,’很牛’,null,null);
      insert into t_role values(null,’管理员7’,’很牛’,null,null);
      insert into t_role values(null,’管理员8’,’很牛’,null,null);
      insert into t_role values(null,’管理员9’,’很牛’,null,null);
      insert into t_role values(null,’管理员10’,’很牛’,null,null);
      insert into t_role values(null,’管理员11’,’很牛’,null,null);
      insert into t_role values(null,’管理员12’,’很牛’,null,null);
      insert into t_role values(null,’管理员13’,’很牛’,null,null); ```java //Role.java package com.duyi.qxgl07.domain;

    public class Role { private Integer rid ; private String rname ; private String rdescription ; private String yl1 ; private String yl2 ;

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

    }

    1. 2) 在角色列表页面,使用layui-table数据表格组件 动态请求数据,展示数据<br /> * 提供了一个用来展示数据的标签<br /> * js中使用table组件请求数据,自动渲染数据<br /> layui.use(['table'],function(){<br /> var table = layui.table ;<br /> table.render({<br /> elem:'#roleGrid', //绑定渲染的位置<br /> url:'roleFindAll', //底层使用ajax请求表格数据<br /> cols:[[ //指定查询结果与表格单元之间的展示关系,哪个属性展示在哪个位置<br /> {title:'角色编号',field:'rid'},<br /> {title:'角色名称',field:'rname'},<br /> {title:'角色描述',field:'rdescription'},<br /> {title:'操作'}<br /> ]],<br /> page:true<br /> });<br /> })<br /> * 注意: 默认开启分页栏<br /> 发送url请求时会自动追加page=1和limit=10参数<br /> 3) 服务端处理请求,根据分页条件,查询分页所需要的数据<br /> 这些数据要符合layui-table组件的数据结构要求<br /> {<br /> code:0,<br /> msg:'',<br /> count:10000,<br /> data:[]<br /> }<br /> 这个结构可以用之前分页创建的PageInfo处理
    2. ```java
    3. //PageInfo.java
    4. package com.duyi.qxgl07.domain;
    5. import java.util.List;
    6. //装载(自定义)分页相关的信息
    7. //扩展:增加layui-table组件分页相关的信息
    8. public class PageInfo {
    9. private Integer page ;
    10. private Integer rows ;
    11. private Integer total ;
    12. private Integer max ;
    13. private Integer start ;
    14. private Integer end ;
    15. private List<?> data ;
    16. private Integer count ;
    17. private String msg ;
    18. private Integer code ;
    19. public Integer getPage() {
    20. return page;
    21. }
    22. public void setPage(Integer page) {
    23. this.page = page;
    24. }
    25. public Integer getRows() {
    26. return rows;
    27. }
    28. public void setRows(Integer rows) {
    29. this.rows = rows;
    30. }
    31. public Integer getTotal() {
    32. return total;
    33. }
    34. public void setTotal(Integer total) {
    35. this.total = total;
    36. }
    37. public Integer getMax() {
    38. return max;
    39. }
    40. public void setMax(Integer max) {
    41. this.max = max;
    42. }
    43. public Integer getStart() {
    44. return start;
    45. }
    46. public void setStart(Integer start) {
    47. this.start = start;
    48. }
    49. public Integer getEnd() {
    50. return end;
    51. }
    52. public void setEnd(Integer end) {
    53. this.end = end;
    54. }
    55. public List<?> getData() {
    56. return data;
    57. }
    58. public void setData(List<?> data) {
    59. this.data = data;
    60. }
    61. public PageInfo(Integer page, Integer rows, Integer total, Integer max, Integer start, Integer end, List<?> data) {
    62. this.page = page;
    63. this.rows = rows;
    64. this.total = total;
    65. this.max = max;
    66. this.start = start;
    67. this.end = end;
    68. this.data = data;
    69. }
    70. public PageInfo() {}
    71. }
    1. 4) 表格单元使用自定义模板提供操作按钮<br /> 5) 点击编辑按钮,弹出对话框,内有表单组件,完成编辑修改<br /> 6) 修改请求实现
    1. //comm.html
    2. <link rel="stylesheet" type="text/css" href="layui/css/layui.css" />
    3. <script src="layui/layui.js"></script>
    1. //role.jsp
    2. <%@ page pageEncoding="utf-8" %>
    3. <html>
    4. <head>
    5. <%@include file="comm.html"%>
    6. </head>
    7. <body>
    8. <!-- 角色列表 -->
    9. <table id="roleGrid"></table>
    10. <!-- 表格单元的按钮模板 -->
    11. <script type="text/html" id="rowBtns">
    12. <button class="layui-btn layui-btn-sm layui-btn-warm" onclick="toEdit('{{d.rid}}','{{d.rname}}','{{d.rdescription}}')">
    13. <i class="layui-icon">&#xe642;</i> 编辑
    14. </button>
    15. <button class="layui-btn layui-btn-sm layui-btn-danger">
    16. <i class="layui-icon">&#xe640;</i> 删除
    17. </button>
    18. </script>
    19. <!-- 弹出层中的表单模板 -->
    20. <form id="roleEditForm" class="layui-form" action="roleUpdate" style="display:none;padding-top:10px;">
    21. <div class="layui-form-item">
    22. <label class="layui-form-label">角色编号</label>
    23. <div class="layui-input-inline">
    24. <input type="text" name="rid" id="rid" readonly lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input layui-input-inline">
    25. </div>
    26. </div>
    27. <div class="layui-form-item">
    28. <label class="layui-form-label">角色名称</label>
    29. <div class="layui-input-inline">
    30. <input type="text" name="rname" id="rname" required placeholder="请输入名称" autocomplete="off" class="layui-input">
    31. </div>
    32. </div>
    33. <div class="layui-form-item">
    34. <label class="layui-form-label">角色描述</label>
    35. <div class="layui-input-inline">
    36. <input type="text" name="rdescription" id="rdescription" required lay-verify="required" placeholder="请输入描述" autocomplete="off" class="layui-input">
    37. </div>
    38. </div>
    39. <div class="layui-form-item">
    40. <div class="layui-input-block">
    41. <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
    42. <button type="button" class="layui-btn layui-btn-primary" onclick="closeEditWin()">取消</button>
    43. </div>
    44. </div>
    45. </form>
    46. <script>
    47. layui.use(['table'],function(){
    48. var table = layui.table ;
    49. <-- 渲染表格(函数),约定优于配置 -->
    50. table.render({
    51. elem:'#roleGrid', //绑定渲染的位置
    52. url:'roleFindAll', //底层使用ajax请求表格数据
    53. cols:[[ //指定查询结果与表格单元之间的展示关系,哪个属性展示在哪个位置
    54. {title:'角色编号',field:'rid'},
    55. {title:'角色名称',field:'rname'},
    56. {title:'角色描述',field:'rdescription'},
    57. {title:'操作',templet:'#rowBtns'}
    58. ]],
    59. page:true
    60. });
    61. })
    62. function toEdit(rid,rname,rdescription){
    63. //显示一个弹出层,弹出对话框,使用layui-layer
    64. layui.use(['layer'],function(){
    65. var layer = layui.layer ;
    66. var $ = layui.jquery;
    67. $('#rid').val(rid) ;
    68. document.getElementById('rname').value = rname ;
    69. $('#rdescription').val(rdescription);
    70. layer.open({
    71. type:1,
    72. title:'编辑角色',
    73. area:[400],
    74. content:$('#roleEditForm')
    75. });
    76. });
    77. }
    78. function closeEditWin(){
    79. layui.use('layer',function(){
    80. var layer = layui.layer ;
    81. layer.closeAll();
    82. });
    83. }
    84. </script>
    85. </body>
    86. </html>
    1. //LayPageInfo.java
    2. package com.duyi.qxgl07.domain;
    3. import java.util.List;
    4. //装载layui-table组件分页时的数据结构
    5. public class LayPageInfo {
    6. private List<?> data ;
    7. private Long count ;
    8. private String msg ;
    9. private Integer code ;
    10. public List<?> getData() {
    11. return data;
    12. }
    13. public void setData(List<?> data) {
    14. this.data = data;
    15. }
    16. public Long getCount() {
    17. return count;
    18. }
    19. public void setCount(Long count) {
    20. this.count = count;
    21. }
    22. public String getMsg() {
    23. return msg;
    24. }
    25. public void setMsg(String msg) {
    26. this.msg = msg;
    27. }
    28. public Integer getCode() {
    29. return code;
    30. }
    31. public void setCode(Integer code) {
    32. this.code = code;
    33. }
    34. public LayPageInfo(List<?> data, Long count, String msg, Integer code) {
    35. this.data = data;
    36. this.count = count;
    37. this.msg = msg;
    38. this.code = code;
    39. }
    40. public LayPageInfo() {}
    41. }
    1. //RoleController.java
    2. package com.duyi.qxgl07.controller;
    3. import com.duyi.qxgl07.domain.LayPageInfo;
    4. import com.duyi.qxgl07.domain.Role;
    5. import com.duyi.qxgl07.service.RoleService;
    6. import org.duyi.web7.annotation.Controller;
    7. import org.duyi.web7.annotation.RequestMapping;
    8. import org.duyi.web7.annotation.RequestParam;
    9. import org.duyi.web7.annotation.ResponseBody;
    10. @Controller
    11. public class RoleController {
    12. RoleService service = new RoleService() ;
    13. @RequestMapping("/roleFindAll")
    14. @ResponseBody
    15. public LayPageInfo findAll(@RequestParam("page") int page , @RequestParam("limit") int limit){
    16. return service.findByPage(page,limit);
    17. }
    18. @RequestMapping("/roleUpdate")
    19. public String update(Role role){
    20. service.update(role);
    21. return "role.jsp" ;
    22. }
    23. }
    1. //RoleDao.java
    2. package com.duyi.qxgl07.dao;
    3. import com.duyi.qxgl07.domain.Role;
    4. import orm.annotation.Select;
    5. import orm.annotation.Update;
    6. import java.util.List;
    7. import java.util.Map;
    8. public interface RoleDao {
    9. @Select("select count(*) from t_role")
    10. public long total(); //用long取或者int都可 --> 更推荐用long
    11. @Select("select * from t_role limit #{start},#{length}")
    12. public List<Role> findByPage(Map<String,Integer> param) ;
    13. @Update("update t_role set rname=#{rname} , rdescription=#{rdescription} where rid = #{rid}")
    14. public void update(Role role) ;
    15. }
    1. //RoleService.java
    2. package com.duyi.qxgl07.service;
    3. import com.duyi.qxgl07.dao.RoleDao;
    4. import com.duyi.qxgl07.domain.LayPageInfo;
    5. import com.duyi.qxgl07.domain.Role;
    6. import orm.SqlSession;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10. public class RoleService {
    11. RoleDao dao = new SqlSession().getMapper(RoleDao.class);
    12. public LayPageInfo findByPage(int page , int limit){
    13. //确保page下限
    14. page = Math.max(1,page) ;
    15. //确保page上限 page max(需要计算 total/limit)
    16. long count = dao.total() ;
    17. // 向上取整
    18. int max = (int) Math.ceil( 1.0*count/limit );
    19. //确保max是有效,至少也会有1页
    20. max = Math.max(1,max);
    21. //确保page上限
    22. page = Math.min(page ,max) ;
    23. //mysql的分页只认 start , length
    24. int start = (page-1)*limit ;
    25. int length = limit ;
    26. Map<String,Integer> param = new HashMap<String,Integer>();
    27. param.put("start",start) ;
    28. param.put("length",length) ;
    29. List<Role> roles = dao.findByPage(param);
    30. return new LayPageInfo(roles,count,"",0) ;
    31. }
    32. public void update(Role role){
    33. dao.update(role);
    34. }
    35. }