6. 添加权限管理模块
Posted 旅鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6. 添加权限管理模块相关的知识,希望对你有一定的参考价值。
本文主要是以权限管理模块为例来介绍使用springboot+mybatis完成增删改查的功能。
1. 添加Permission实体类
1 package com.lvniao.blog.model; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Permission { 7 8 private Long id; 9 10 private String name; 11 12 private String alias; 13 14 private String description; 15 16 private boolean available; 17 18 private Permission parent; 19 20 private List<Permission> children = new ArrayList<Permission>(); 21 22 public Long getId() { 23 return id; 24 } 25 26 public void setId(Long id) { 27 this.id = id; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public String getAlias() { 39 return alias; 40 } 41 42 public void setAlias(String alias) { 43 this.alias = alias; 44 } 45 46 public String getDescription() { 47 return description; 48 } 49 50 public void setDescription(String description) { 51 this.description = description; 52 } 53 54 public boolean isAvailable() { 55 return available; 56 } 57 58 public void setAvailable(boolean available) { 59 this.available = available; 60 } 61 62 public Permission getParent() { 63 return parent; 64 } 65 66 public void setParent(Permission parent) { 67 this.parent = parent; 68 } 69 70 public List<Permission> getChildren() { 71 return children; 72 } 73 74 public void setChildren(List<Permission> children) { 75 this.children = children; 76 } 77 }
2. 添加Mapper类
1 package com.lvniao.blog.mapper; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Delete; 6 import org.apache.ibatis.annotations.Insert; 7 import org.apache.ibatis.annotations.InsertProvider; 8 import org.apache.ibatis.annotations.Many; 9 import org.apache.ibatis.annotations.One; 10 import org.apache.ibatis.annotations.Mapper; 11 import org.apache.ibatis.annotations.Param; 12 import org.apache.ibatis.annotations.Result; 13 import org.apache.ibatis.annotations.Results; 14 import org.apache.ibatis.annotations.Select; 15 import org.apache.ibatis.annotations.UpdateProvider; 16 import org.apache.ibatis.mapping.FetchType; 17 18 import com.lvniao.blog.model.Permission; 19 20 @Mapper 21 public interface PermissionMapper { 22 23 @InsertProvider(type=PermissionProvider.class, method="insert") 24 public void insert(Permission permission); 25 26 @UpdateProvider(type=PermissionProvider.class, method="update") 27 public void update(Permission permission); 28 29 @Delete("delete from permissions where id=#{id}") 30 public void delete(@Param("id") Long id); 31 32 @Select("select count(id) count from permissions where name like \'%${query}%\'") 33 public int getCountByQuery(@Param("query") String query); 34 35 @Select("select count(id) count from permissions where parentid is null") 36 public int getParentCount(); 37 38 @Select("select id, name, alias, description, available, parentid from permissions where name like \'%${query}%\' limit #{start}, #{total}") 39 @Results({ 40 @Result(id=true, column="id", property="id"), 41 @Result(column="name", property="name"), 42 @Result(column="alias", property="alias"), 43 @Result(column="description", property="description"), 44 @Result(column="available", property="available"), 45 @Result(column="parentid", property="parent", 46 one=@One(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionById", fetchType=FetchType.EAGER)) 47 }) 48 public List<Permission> getPermissionByPage(@Param("start") int start, @Param("total") int total, @Param("query") String query); 49 50 @Select("select id, name, alias, description, available, parentid from permissions where parentid is null limit #{start}, #{total}") 51 @Results({ 52 @Result(id=true, column="id", property="id"), 53 @Result(column="name", property="name"), 54 @Result(column="alias", property="alias"), 55 @Result(column="description", property="description"), 56 @Result(column="available", property="available"), 57 @Result(column="id", property="children", 58 many=@Many(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionByParent", fetchType=FetchType.EAGER)), 59 @Result(column="parentid", property="parent", 60 one=@One(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionById", fetchType=FetchType.EAGER)) 61 }) 62 public List<Permission> getParentPermissionByPage(@Param("start") int start, @Param("total") int total); 63 64 @Select("select id, name, alias, description, available, parentid from permissions where parentid is null") 65 @Results({ 66 @Result(id=true, column="id", property="id"), 67 @Result(column="name", property="name"), 68 @Result(column="alias", property="alias"), 69 @Result(column="description", property="description"), 70 @Result(column="available", property="available"), 71 @Result(column="id", property="children", 72 many=@Many(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionByParent", fetchType=FetchType.EAGER)), 73 @Result(column="parentid", property="parent", 74 one=@One(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionById", fetchType=FetchType.EAGER)) 75 }) 76 public List<Permission> getParentPermission(); 77 78 @Select("select id, name, alias, description, available, parentid from permissions where parentid is null") 79 public List<Permission> getParent(); 80 81 @Select("select id, name, alias, description, available, parentid from permissions where parentid=#{id}") 82 @Results({ 83 @Result(id=true, column="id", property="id"), 84 @Result(column="name", property="name"), 85 @Result(column="alias", property="alias"), 86 @Result(column="description", property="description"), 87 @Result(column="available", property="available"), 88 @Result(column="parentid", property="parent", 89 one=@One(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionById", fetchType=FetchType.EAGER)) 90 }) 91 public List<Permission> getPermissionByParent(Long id); 92 93 @Select("select id, name, alias, description, available, parentid from permissions where id=#{id}") 94 @Results({ 95 @Result(id=true, column="id", property="id"), 96 @Result(column="name", property="name"), 97 @Result(column="alias", property="alias"), 98 @Result(column="description", property="description"), 99 @Result(column="available", property="available"), 100 @Result(column="parentid", property="parent", 101 one=@One(select="com.lvniao.blog.mapper.PermissionMapper.getPermissionById", fetchType=FetchType.EAGER)) 102 }) 103 public Permission getPermissionById(@Param("id") Long id); 104 } 105 因为对数据进行插入或更新的时候要根据Permission.parent是否为空来执行不同的sql,所以这儿使用了provider来动态拼接sql,provider代码如下: 106 package com.lvniao.blog.mapper; 107 108 import org.apache.ibatis.jdbc.SQL; 109 110 import com.lvniao.blog.model.Permission; 111 import com.lvniao.blog.util.StringUtil; 112 113 public class PermissionProvider { 114 115 private static final String table = "permissions"; 116 117 public String insert(Permission permission) { 118 SQL sql = new SQL().INSERT_INTO(table).INTO_COLUMNS("name", "alias", "description", "available", "parentid"); 119 sql.INTO_VALUES("#{name}", "#{alias}", "#{description}", "#{available}"); 120 if(permission.getParent() != null) { 121 sql.INTO_VALUES("#{parent.id}"); 122 } else { 123 sql.INTO_VALUES("null"); 124 } 125 return sql.toString(); 126 } 127 128 public String update(Permission permission) { 129 SQL sql = new SQL().UPDATE(table); 130 sql.SET("name=#{name}"); 131 sql.SET("alias=#{alias}"); 132 if(permission.isAvailable() != null){ 133 sql.SET("available=#{available}"); 134 } 135 if(!StringUtil.isNullOrEmpty(permission.getDescription())){ 136 sql.SET("description=#{description}"); 137 } 138 if(permission.getParent() != null && permission.getParent().getId() != null){ 139 sql.SET("parentid=#{parent.id}"); 140 } 141 sql.WHERE("id=#{id}"); 142 return sql.toString(); 143 } 144 }
3. 添加Controller
1 package com.lvniao.blog.admin.controller; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.PathVariable; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.ResponseBody; 12 13 import com.lvniao.blog.mapper.PermissionMapper; 14 import com.lvniao.blog.model.Permission; 15 import com.lvniao.blog.util.Constant; 16 import com.lvniao.blog.util.JsonUtil; 17 import com.lvniao.blog.util.StringUtil; 18 import com.lvniao.blog.vmodel.PageModel; 19 20 @Controller 21 @RequestMapping("/permission") 22 public class PermissionController { 23 24 @Autowired 25 private PermissionMapper permissionMapper; 26 27 @RequestMapping("/") 28 public String index(Model model) { 29 return "permission/index"; 30 } 31 32 @RequestMapping("/list") 33 @ResponseBody 34 public String list(Integer page, Integer rows, String query) { 35 PageModel pagemodel = new PageModel(); 36 if(StringUtil.isNullOrEmpty(query)) { 37 pagemodel = new PageModel(permissionMapper.getParentCount(), 38 permissionMapper.getParentPermissionByPage((page-1)*rows, rows)); 39 } else { 40 pagemodel = new PageModel(permissionMapper.getCountByQuery(query), 41 permissionMapper.getPermissionByPage((page-1)*rows, rows, query)); 42 } 43 return JsonUtil.toJson(pagemodel); 44 } 45 46 @RequestMapping("/getparent") 47 @ResponseBody 48 public String getParentPermission() { 49 return JsonUtil.toJson(permissionMapper.getParent()); 50 } 51 52 @RequestMapping("/add") 53 public String add() { 54 return "permission/add"; 55 } 56 57 @RequestMapping("/addmodel") 58 @ResponseBody 59 public String addModel(Permission permission) { 60 permissionMapper.insert(permission); 61 return JsonUtil.toMsgJson(Constant.SUCCESS); 62 } 63 64 @RequestMapping("/modify/{id}") 65 public String modify(@PathVariable Long id, Model model) { 66 model.addAttribute("permission", permissionMapper.getPermissionById(id)); 67 return "permission/modify"; 68 } 69 70 @RequestMapping("/modifymodel") 71 @ResponseBody 72 public String modifyModel(Permission permission) { 73 permissionMapper.update(permission); 74 return JsonUtil.toMsgJson(Constant.SUCCESS); 75 } 76 77 @RequestMapping("/delete/{id}") 78 @ResponseBody 79 public String delete(@PathVariable Long id) { 80 permissionMapper.delete(id); 81 return JsonUtil.toMsgJson(Constant.SUCCESS); 82 } 83 }
在controller如果方法返回的是视图,则直接返回视图名即可;如果方法是供ajax调用的,这样就要添加@ResponseBody,以注明是用于返回json字符串;可以使用@PathVariable来绑定uri中的片段。
4. 添加index视图
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"/> 5 <title></title> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 7 <link rel="stylesheet" th:href="@{/css/page.css}"/> 8 <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}"/> 9 <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}"/> 10 <link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}"/> 11 <link rel="stylesheet" type="text/css" th:href="@{/css/grid.css}"/> 12 <script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script> 13 <script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script> 14 <script type="text/javascript" th:src="@{/layui/layui.js}"></script> 15 <script type="text/javascript" th:src="@{/layui/layui.all.js}"></script> 16 <script type="text/javascript" th:src="@{/js/form.js}"></script> 17 <script> 18 $(function(){ 19 $("#grid").treegrid({url: \'/permission/list\', 20 method: \'get\', 21 rownumbers: true, 22 idField: \'id\', 23 fit:true, 24 treeField: \'name\', 25 pagination: true, 26 pageSize: 10, 27 pageList: [10, 50, 100]}); 28 $("#btnSearch").click(function(){ 29 $("#grid").treegrid({queryParams:form2Json("query")}); 30 }); 31 32 $(".lv-btn-group .lv-btn").click(function(){ 33 var type = $(this).attr("type"); 34 var addr = $(this).attr("addr"); 35 if(type == "refresh"){ 以上是关于6. 添加权限管理模块的主要内容,如果未能解决你的问题,请参考以下文章