B059-权限管理系统01

目录

      • 知识点介绍
      • 项目演示
      • 项目搭建
      • 动态菜单查询分析(权限表分析)
      • 权限系统表分析
      • 角色模块
        • pageInfo
        • pageHelper
        • 实现前端动态分页
        • 高级查询
        • 新增与修改
        • 删除角色
      • 分配权限-表分析
      • 角色授权数据-一级和二级权限查询

知识点介绍

B059-权限管理系统01_第1张图片

项目演示

准备数据库
在这里插入图片描述
准备工程auth_new

tips:
基于sspringboot的ssm架构
角色是权限系统的核心

项目搭建

一:SSM整合
  1.导包									tips:lombok在编译时生成get,setter ,构造器,toString等方法
  2.准备目录结构
  3.核心配置文件  yml
  4.启动类  加上mapper接口的扫描
  5.测试(SpringBoot测试)

动态菜单查询分析(权限表分析)

B059-权限管理系统01_第2张图片

权限系统表分析

B059-权限管理系统01_第3张图片

角色模块

见代码

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}"  >1a>
    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

    @Override
    public PageInfo<Role> loadAllByQuery() {
        return new PageInfo<>(roleMapper.loadAll());
    }

RoleMapper

    
    <select id="loadAll" resultType="Role">
        SELECT * FROM role
    select>
pageHelper

是mybatis提供的分页插件,这里是基于springboot的pageHelper,底层用aop实现分页,自己算出来
导包


<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelper-spring-boot-starterartifactId>
    <version>1.2.5version>
dependency>

RoleController

    @RequestMapping("/index")
    public String toRoleList(Model model){
        model.addAttribute("pageInfo", roleService.loadAllByQuery(new RoleQuery()));
        return "views/role/role_list";
    }

RoleServiceImpl

    @Override
    public 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

    
    <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分页关键字是rownum
  helper-dialect: mysql
  #分页容错  传的当前页码小于1时定位第一页,传的当前页码大于最末页时定位到末页
  reasonable: true
  #支持在参数传递分页参数  支持在mapper接口层传分页参数
  support-methods-arguments: true
高级查询

RoleQuery

@Data
public class RoleQuery extends BaseQuery{
    private String roleName;
}

RoleServiceImpl

    @Override
    public 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

    
    <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")
    @ResponseBody
    public 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")
    @ResponseBody
    public 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
    @Transactional
    public void save(Role role) {
        roleMapper.save(role);
    }

    @Override
    @Transactional
    public void update(Role role) {
        roleMapper.update(role);
    }

RoleMapper.xml

    
    <insert id="save">
      INSERT INTO role(name,sn) VALUES (#{name},#{sn})
    insert>

    
    <update id="update">
        UPDATE role SET name = #{name},sn=#{sn}
        WHERE id = #{id}
    update>
删除角色

tips:中间表不应该有实体类或Mapper
RoleController

    // 删除角色
    @RequestMapping("/delete")
    @ResponseBody
    public 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
    @Transactional
    public void delete(Long roleId) {
        roleMapper.deleteRolePermission(roleId);
        roleMapper.delete(roleId);
    }

RoleMapper.xml

    
    <delete id="deleteRolePermission" parameterType="long">
        DELETE FROM role_permission WHERE role_id = #{roleId}
    delete>

    
    <delete id="delete" parameterType="long">
        DELETE FROM role WHERE id = #{id}
    delete>

分配权限-表分析

B059-权限管理系统01_第4张图片

角色授权数据-一级和二级权限查询

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

    @Override
    public 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>

    
    <select id="loadOneAndTwoLevel" resultMap="xx">
        SELECT
            p.id pid,
            p. NAME pname,
            p.url purl,
            c.*
        FROM
            permission p
        JOIN permission c ON p.id = c.parent_id
        WHERE
            p.parent_id IS NULL
    select>

你可能感兴趣的:(笔记总结,权限管理系统)