当前位置: 代码迷 >> 综合 >> Spring Boot +mybatis plus 实现权限管理模块(RBAC)----仅后端
  详细解决方案

Spring Boot +mybatis plus 实现权限管理模块(RBAC)----仅后端

热度:41   发布时间:2023-12-02 13:53:35.0

 首先创建项目,在pom文件中导入相关依赖,集成mybatis plus,刷新依赖,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.qcby.boot</groupId><artifactId>jiucai</artifactId><version>0.0.1-SNAPSHOT</version><name>jiucai</name><description>boot项目框架演示</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><!--移除框架中的logback依赖--><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><!-- 支持log4j2的模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency><!-- 创建项目中勾选了  Spring Web 自动添加--><!--  完成了 spring &  spring mvc的整合:我们可以使用spring mvc中的语法了--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--  每次修改pom文件需要   刷新maven!!!  --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--pageHelper分页插件--><!--        <dependency>--><!--            <groupId>com.github.pagehelper</groupId>--><!--            <artifactId>pagehelper-spring-boot-starter</artifactId>--><!--            <version>1.3.0</version>--><!--        </dependency>--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

创建yml文件或者编写properties文件

yml编写

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://********:3306/rbac?useUnicode=true&characterEncoding=utf-8&serverTimezone=CTTusername: rootpassword: ********# mybatis-plus配置
mybatis-plus:configuration:# 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射map-underscore-to-camel-case: true# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用log-impl: org.apache.ibatis.logging.stdout.StdOutImpl#MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名type-aliases-package: com.qcby.jiucai_rbac.entity#xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)mapperLocations: classpath:mapper/*.xml

记得改启动类:

在启动类加

@MapperScan("你所要实现的接口所在的路径")

指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加

这样一个项目框架就搭好了,启动一下测试能跑起来再继续往下

然后我是通过esayCode生成的所有层

(通过自定义模板针对单表生成所有层以及对单表的crud所有方法)

 

 文件太多了我就不一一展示了,登录注册接口就是简单的一个查询和插入;我就把实现层分配角色和分配菜单,以及菜单树的实现分享一下

Serviceimpl事务层

SysUserService

package com.qcby.jiucai_rbac.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.jiucai_rbac.common.web.PageWeb;
import com.qcby.jiucai_rbac.common.web.ResultJson;
import com.qcby.jiucai_rbac.entity.RefUserRole;
import com.qcby.jiucai_rbac.entity.SysUser;
import com.qcby.jiucai_rbac.mapper.RefUserRoleDao;
import com.qcby.jiucai_rbac.mapper.SysUserDao;
import com.qcby.jiucai_rbac.service.SysUserService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;/*** 用户表(SysUser)表服务实现类** @author GLT* @since 2021-12-04 23:24:41*/
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {@Resourceprivate RefUserRoleDao refUserRoleDao;@Overridepublic ResultJson listAll(PageWeb<SysUser> pageWeb) {Page<SysUser> page = new Page<>(pageWeb.getPageNum(), pageWeb.getPageSize());QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();IPage pageInfo = this.page(page, queryWrapper);return ResultJson.ok(PageWeb.build(pageInfo));}@Overridepublic ResultJson login(SysUser sysUser) {QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();queryWrapper.eq("user_name", sysUser.getUserName());queryWrapper.eq("password", sysUser.getPassword());List<SysUser> userList = this.baseMapper.selectList(queryWrapper);if (userList.size() == 1) {return ResultJson.ok("登录成功", userList);}return ResultJson.error("登陆失败");}@Overridepublic boolean register(SysUser sysUser) {if (sysUser.getAgainPassword().equals(sysUser.getPassword())) {return this.save(sysUser);}return false;}@Overridepublic boolean setRole(Long userId, List<Long> roleIdList) {QueryWrapper<RefUserRole> queryWrapper = new QueryWrapper<>();queryWrapper.eq("user_id", userId);this.refUserRoleDao.delete(queryWrapper);int flag = 0;for (Long roleId : roleIdList) {RefUserRole refUserRole = new RefUserRole();refUserRole.setRoleId(roleId);refUserRole.setUserId(userId);flag = this.refUserRoleDao.insert(refUserRole);}return flag == 1;}@Overridepublic ResultJson selectByUserId(Long userId) {QueryWrapper<RefUserRole> queryWrapper = new QueryWrapper<>();queryWrapper.eq("user_id", userId);List<Map<String,String>> roleNameList= this.refUserRoleDao.selectByUserId(userId);List<String> list=new ArrayList<>();for (Map<String,String> map : roleNameList) {Iterator it =map.entrySet().iterator();while (it.hasNext()){Map.Entry entry = (Map.Entry) it.next();list.add(entry.getValue().toString());}}return ResultJson.ok(list);}
}

SysRoleServiceimpl

package com.qcby.jiucai_rbac.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.jiucai_rbac.common.web.PageWeb;
import com.qcby.jiucai_rbac.common.web.ResultJson;
import com.qcby.jiucai_rbac.entity.RefRoleMenu;
import com.qcby.jiucai_rbac.entity.SysRole;
import com.qcby.jiucai_rbac.mapper.RefRoleMenuDao;
import com.qcby.jiucai_rbac.mapper.SysRoleDao;
import com.qcby.jiucai_rbac.service.SysRoleService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;/*** 角色表(SysRole)表服务实现类** @author GLT* @since 2021-12-04 23:24:30*/
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {@Resourceprivate RefRoleMenuDao refRoleMenuDao;@Overridepublic ResultJson listAll(PageWeb<SysRole> pageWeb) {Page<SysRole> page = new Page<>(pageWeb.getPageNum(), pageWeb.getPageSize());QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();IPage pageInfo = this.page(page, queryWrapper);return ResultJson.ok(PageWeb.build(pageInfo));}@Overridepublic boolean setMenu(Long roleId, List<Long> menuIdList) {QueryWrapper<RefRoleMenu> queryWrapper = new QueryWrapper<>();queryWrapper.eq("role_id", roleId);this.refRoleMenuDao.delete(queryWrapper);int flag = 0;for (Long menuId : menuIdList) {RefRoleMenu refRloeMenu = new RefRoleMenu();refRloeMenu.setRoleId(roleId);refRloeMenu.setMenuId(menuId);flag = this.refRoleMenuDao.insert(refRloeMenu);}return flag == 1;}@Overridepublic ResultJson selectByRoleId(Long roleId) {QueryWrapper<RefRoleMenu> queryWrapper = new QueryWrapper<>();queryWrapper.eq("role_id", roleId);List<Map<String, String>> menuNameList = this.refRoleMenuDao.selectByRoleId(roleId);List<String> list = new ArrayList<>();for (Map<String, String> map : menuNameList) {Iterator it = map.entrySet().iterator();while (it.hasNext()) {Map.Entry entry = (Map.Entry) it.next();list.add(entry.getValue().toString());}}return ResultJson.ok(list);}
}

SysMenuServiceimpl

package com.qcby.jiucai_rbac.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.jiucai_rbac.common.web.PageWeb;
import com.qcby.jiucai_rbac.common.web.ResultJson;
import com.qcby.jiucai_rbac.entity.SysMenu;
import com.qcby.jiucai_rbac.mapper.SysMenuDao;
import com.qcby.jiucai_rbac.service.SysMenuService;
import org.springframework.stereotype.Service;import java.util.List;/*** 菜单表(SysMenu)表服务实现类** @author GLT* @since 2021-12-04 23:23:30*/
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {@Overridepublic ResultJson listAll(PageWeb<SysMenu> pageWeb) {Page<SysMenu> page = new Page<>(pageWeb.getPageNum(), pageWeb.getPageSize());QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();IPage pageInfo = this.page(page, queryWrapper);return ResultJson.ok(PageWeb.build(pageInfo));}@Overridepublic ResultJson menuTree() {List<SysMenu> menuList=this.list();QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();queryWrapper.eq("menu_parent_id",0);List<SysMenu> firstLevelMenuList=this.baseMapper.selectList(queryWrapper);for (SysMenu sysMenu : firstLevelMenuList) {getChildList(sysMenu,menuList);}return ResultJson.ok(firstLevelMenuList);}private SysMenu getChildList(SysMenu sysMenu, List<SysMenu> menuList) {for (SysMenu menu : menuList) {if (menu.getMenuParentId().equals(sysMenu.getId())) {sysMenu.getChildList().add(getChildList(menu, menuList));}}return sysMenu;}
}

利用递归实现菜单树

  public ResultJson menuTree() {List<SysMenu> menuList=this.list();QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();queryWrapper.eq("menu_parent_id",0);List<SysMenu> firstLevelMenuList=this.baseMapper.selectList(queryWrapper);for (SysMenu sysMenu : firstLevelMenuList) {getChildList(sysMenu,menuList);}return ResultJson.ok(firstLevelMenuList);}private SysMenu getChildList(SysMenu sysMenu, List<SysMenu> menuList) {for (SysMenu menu : menuList) {if (menu.getMenuParentId().equals(sysMenu.getId())) {sysMenu.getChildList().add(getChildList(menu, menuList));}}return sysMenu;}
}

忘记讲了项目中用到的的统一返回结构封装---ResultJson的编写,以及项目中的权限验证和登录拦截器的介绍,下期文章讲解

  相关解决方案