B059-权限管理系统01

目录

知识点介绍

项目演示

准备数据库

准备工程auth_new

tips:

基于springboot的ssm架构

角色是权限系统的核心

项目搭建

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

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

权限系统表分析

角色模块

见代码

tips:

lombok只在编译时生成get set方法等,写代码时不提示有对应方法,可通过lombok插件来解决此问题

类名右键go to- test,快速在测试包生成对应测试方法

pageInfo

在pageHelper里mybatis提供了pageInfo类

role_list.html

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

java 复制代码
    @RequestMapping("/index")
    public String toRoleList(Model model){
        model.addAttribute("pageInfo", roleService.loadAllByQuery());

        return "views/role/role_list";
    }

RoleServiceImpl

java 复制代码
    @Override
    public PageInfo<Role> loadAllByQuery() {
        return new PageInfo<>(roleMapper.loadAll());
    }

RoleMapper

xml 复制代码
    <!--List<Role> loadAll();-->
    <select id="loadAll" resultType="Role">
        SELECT * FROM role
    </select>
pageHelper

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

导包

xml 复制代码
<!-- pagehelper分页插件依赖 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

RoleController

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

RoleServiceImpl

java 复制代码
    @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

xml 复制代码
    <!--List<Role> loadAll();-->
    <select id="loadAll" resultType="Role">
        SELECT * FROM role
    </select>

application.yml

yml 复制代码
#cn.ming包下所有操作打印详细日志
logging:
  level:
    cn:
      ming: trace
实现前端动态分页

BaseQuery

java 复制代码
@Data
public class BaseQuery {
    private Integer currentPage = 1;
    private Integer pageSize = 5;

	// SpringMVC  通过使用set+参数名称作为方法,绑定前端传过来的参数
    public void setNumber(Integer currentPage) {
        this.currentPage = currentPage;
    }
}

RoleController

java 复制代码
    @RequestMapping("/index")
    public String toRoleList(RoleQuery roleQuery,Model model){
        model.addAttribute("pageInfo", roleService.loadAllByQuery(roleQuery));
        return "views/role/role_list";
    }

application.yml

yml 复制代码
#pagehelper分页插件
pagehelper:
  #分页方言  因为不同数据库拼接分页sql的关键字不同,如mysql分页关键字是limit   oracle分页关键字是rownum
  helper-dialect: mysql
  #分页容错  传的当前页码小于1时定位第一页,传的当前页码大于最末页时定位到末页
  reasonable: true
  #支持在参数传递分页参数  支持在mapper接口层传分页参数
  support-methods-arguments: true
高级查询

RoleQuery

java 复制代码
@Data
public class RoleQuery extends BaseQuery{
    private String roleName;
}

RoleServiceImpl

java 复制代码
    @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

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

java 复制代码
    // 添加角色
    @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

java 复制代码
    @Override
    @Transactional
    public void save(Role role) {
        roleMapper.save(role);
    }

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

RoleMapper.xml

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

java 复制代码
    // 删除角色
    @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

java 复制代码
    /**
     * 删除角色
     *   1.删除角色权限中间表数据
     *   2.删除角色表数据
     */
    @Override
    @Transactional
    public void delete(Long roleId) {
        roleMapper.deleteRolePermission(roleId);
        roleMapper.delete(roleId);
    }

RoleMapper.xml

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

java 复制代码
    // 查询一级和二级权限,并返回页面
    @RequestMapping("/toPermission")
    public String toRoleList(Long roleId, Model model){
        model.addAttribute("map", roleService.loadOneAndTwoLevel(roleId));
        return "views/role/role_permission";
    }

domain

java 复制代码
@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

java 复制代码
    @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

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">
        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>
相关推荐
Crazy Struggle2 个月前
.NET 8.0 前后分离快速开发框架
vue.js·开源项目·.net 8.0·权限管理系统
Crazy Struggle3 个月前
.NET 8 + Vue 3 极简 RABC 权限管理系统
vue.js·.net 8.0·权限管理系统
Crazy Struggle3 个月前
.NET 开源权限认证项目 MiniAuth上线
.net·.net core·权限管理系统
Crazy Struggle3 个月前
推荐一款界面优雅、功能强大的 .NET + Vue 权限管理系统
.net·开源项目·权限管理系统