目录
- 知识点介绍
- 项目演示
- 项目搭建
- 动态菜单查询分析(权限表分析)
- 权限系统表分析
- 角色模块
- pageInfo
- pageHelper
- 实现前端动态分页
- 高级查询
- 新增与修改
- 删除角色
- 分配权限-表分析
- 角色授权数据-一级和二级权限查询
知识点介绍
项目演示
准备数据库
准备工程auth_new
tips:
基于springboot的ssm架构
角色是权限系统的核心
项目搭建
一:SSM整合1.导包 tips:lombok在编译时生成get,setter ,构造器,toString等方法2.准备目录结构3.核心配置文件 yml4.启动类 加上mapper接口的扫描5.测试(SpringBoot测试)
动态菜单查询分析(权限表分析)
权限系统表分析
角色模块
见代码
tips:
lombok只在编译时生成get set方法等,写代码时不提示有对应方法,可通过lombok插件来解决此问题
类名右键go to- test,快速在测试包生成对应测试方法
pageInfo
在pageHelper里mybatis提供了pageInfo类
role_list.html
<th:block th:if="${pageInfo.list != null}">......</th:block><th:block th:if="${!pageInfo.isFirstPage}"><li><a href="javascript:void(0);" aria-label="Previous" th:href="@{/role/index(number=${pageInfo.prePage})}" ><span aria-hidden="true">«</span></a></li>
</th:block>
<th:block th:each="nums:${pageInfo.navigatepageNums}"><li th:class="${nums==pageInfo.pageNum? 'active' : ''}"><a href="javascript:void(0);" th:href="@{/role/index(number=${nums})}" th:text="${nums}" >1</a></li>
</th:block><th:block th:if="${!pageInfo.isLastPage}"><li><a href="javascript:void(0);" aria-label="Next" th:href="@{/role/index(number=${pageInfo.nextPage})}" ><span aria-hidden="true">»</span></a></li>
</th:block>
RoleController
@RequestMapping("/index")public String toRoleList(Model model){model.addAttribute("pageInfo", roleService.loadAllByQuery());return "views/role/role_list";}
RoleServiceImpl
@Overridepublic PageInfo<Role> loadAllByQuery() {return new PageInfo<>(roleMapper.loadAll());}
RoleMapper
<!--List<Role> loadAll();--><select id="loadAll" resultType="Role">SELECT * FROM role</select>
pageHelper
是mybatis提供的分页插件,这里是基于springboot的pageHelper,底层用aop实现分页,自己算出来
导包
<!-- pagehelper分页插件依赖 -->
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.5</version>
</dependency>
RoleController
@RequestMapping("/index")public String toRoleList(Model model){model.addAttribute("pageInfo", roleService.loadAllByQuery(new RoleQuery()));return "views/role/role_list";}
RoleServiceImpl
@Overridepublic PageInfo<Role> loadAllByQuery(RoleQuery roleQuery) {//使用PageHelper来做分页//1.分页参数准备Integer currentPage = roleQuery.getCurrentPage();Integer pageSize = roleQuery.getPageSize();//2.开启分页PageHelper.startPage(currentPage,pageSize);return new PageInfo<Role>(roleMapper.loadAll());}
RoleMapper
<!--List<Role> loadAll();--><select id="loadAll" resultType="Role">SELECT * FROM role</select>
application.yml
#cn.ming包下所有操作打印详细日志
logging:level:cn:ming: trace
实现前端动态分页
BaseQuery
@Data
public class BaseQuery {private Integer currentPage = 1;private Integer pageSize = 5;// SpringMVC 通过使用set+参数名称作为方法,绑定前端传过来的参数public void setNumber(Integer currentPage) {this.currentPage = currentPage;}
}
RoleController
@RequestMapping("/index")public String toRoleList(RoleQuery roleQuery,Model model){model.addAttribute("pageInfo", roleService.loadAllByQuery(roleQuery));return "views/role/role_list";}
application.yml
#pagehelper分页插件
pagehelper:#分页方言 因为不同数据库拼接分页sql的关键字不同,如mysql分页关键字是limit oracle分页关键字是rownumhelper-dialect: mysql#分页容错 传的当前页码小于1时定位第一页,传的当前页码大于最末页时定位到末页reasonable: true#支持在参数传递分页参数 支持在mapper接口层传分页参数support-methods-arguments: true
高级查询
RoleQuery
@Data
public class RoleQuery extends BaseQuery{private String roleName;
}
RoleServiceImpl
@Overridepublic PageInfo<Role> loadAllByQuery(RoleQuery roleQuery) {//使用PageHelper来做分页//1.分页参数准备Integer currentPage = roleQuery.getCurrentPage();Integer pageSize = roleQuery.getPageSize();//2.开启分页PageHelper.startPage(currentPage,pageSize);return new PageInfo<Role>(roleMapper.loadAll(roleQuery));}
RoleMapper.xml
<!--List<Role> loadAll(RoleQuery roleQuery);--><select id="loadAll" resultType="Role">SELECT * FROM role<where><if test="roleName != null and roleName != ''">AND name like concat("%",#{roleName},"%")</if></where></select>
tips:去除空格一般交给前端或controller处理,避免放到数据层处理,那会引起性能下降
新增与修改
tips:
先看好前端页面请求过来的路径,参数,返回值
查询用只读事务,增删改用默认的需要事务
RoleController
// 添加角色@RequestMapping("/save")@ResponseBodypublic Map<String,Object> save(Role role){Map<String,Object> map = new HashMap<>();try {roleService.save(role);map.put("success", true);map.put("msg","操作成功!");} catch (Exception e) {e.printStackTrace();map.put("success", false);map.put("msg","系统繁忙,稍后重试!!!");}return map;}// 修改角色@RequestMapping("/update")@ResponseBodypublic Map<String,Object> update(Role role){Map<String,Object> map = new HashMap<>();try {roleService.update(role);map.put("success", true);map.put("msg","操作成功!");} catch (Exception e) {e.printStackTrace();map.put("success", false);map.put("msg","系统繁忙,稍后重试!!!");}return map;}
RoleServiceImpl
@Override@Transactionalpublic void save(Role role) {roleMapper.save(role);}@Override@Transactionalpublic void update(Role role) {roleMapper.update(role);}
RoleMapper.xml
<!--void save(Role role);--><insert id="save">INSERT INTO role(name,sn) VALUES (#{name},#{sn})</insert><!--void update(Role role);--><update id="update">UPDATE role SET name = #{name},sn=#{sn}WHERE id = #{id}</update>
删除角色
tips:中间表不应该有实体类或Mapper
RoleController
// 删除角色@RequestMapping("/delete")@ResponseBodypublic Map<String,Object> delete(Long id){Map<String,Object> map = new HashMap<>();try {roleService.delete(id);map.put("success", true);map.put("msg","操作成功!");} catch (Exception e) {e.printStackTrace();map.put("success", false);map.put("msg","系统繁忙,稍后重试!!!");}return map;}
RoleServiceImpl
/*** 删除角色* 1.删除角色权限中间表数据* 2.删除角色表数据*/@Override@Transactionalpublic void delete(Long roleId) {roleMapper.deleteRolePermission(roleId);roleMapper.delete(roleId);}
RoleMapper.xml
<!--void deleteRolePermission(Long roleId);--><delete id="deleteRolePermission" parameterType="long">DELETE FROM role_permission WHERE role_id = #{roleId}</delete><!--void delete(Long roleId);--><delete id="delete" parameterType="long">DELETE FROM role WHERE id = #{id}</delete>
分配权限-表分析
角色授权数据-一级和二级权限查询
RoleController
// 查询一级和二级权限,并返回页面@RequestMapping("/toPermission")public String toRoleList(Long roleId, Model model){model.addAttribute("map", roleService.loadOneAndTwoLevel(roleId));return "views/role/role_permission";}
domain
@Data
public class Permission {private Long id;private String name;private String url;private Long menu_id;private Permission parent;private List<Permission> children = new ArrayList<>();
}
RoleServiceImpl
@Overridepublic Map<String, Object> loadOneAndTwoLevel(Long roleId) {Map<String, Object> map = new HashMap<>();map.put("permissions", roleMapper.loadOneAndTwoLevel());//角色回显map.put("rid", roleId);return map;}
RoleMapper.xml
<!--自定义结果集映射--><resultMap id="xx" type="Permission"><id column="pid" property="id"/><result column="pname" property="name"/><result column="purl" property="url"/><collection property="children" ofType="Permission"><id column="id" property="id"/><result column="name" property="name"/><result column="url" property="url"/></collection></resultMap><!--List<Permission> loadOneAndTwoLevel();--><select id="loadOneAndTwoLevel" resultMap="xx">SELECTp.id pid,p. NAME pname,p.url purl,c.*FROMpermission pJOIN permission c ON p.id = c.parent_idWHEREp.parent_id IS NULL</select>