IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Java实战:微服务多模块项目搭建(五) -> 正文阅读

[Java知识库]Java实战:微服务多模块项目搭建(五)

用户权限(一)

VO

菜单并不是通用的,根据前端需求而定

@Data
public class RoleVO {

    private Integer id;

    /**
     * 角色code
     */
    private String code;

    /**
     * 角色名
     */
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("create_time")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("update_time")
    private Date updateTime;

    /**
     * 每个角色分配的菜单权限
     */
    @JsonProperty("menu_list")
    private List<MenuVO> menuList;

    /**
     * 每个角色分配的指令权限
     */
    @JsonProperty("permission_list")
    private List<PermissionVO> permissionList;

}
@Data
public class UserVO {

    private Integer id;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 用户名
     */
    private String username;

    /**
     * 头像
     */
    @JsonProperty("head_img")
    private String headImg;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("create_time")
    private Date createTime;

    /**
     * 用户角色列表
     */
    @JsonProperty("role_list")
    private List<RoleVO> roleList;

}
@Data
public class PermissionVO {

    private Integer id;

    /**
     * 指令权限
     */
    private String permission;

    /**
     * 权限名称
     */
    private String name;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("create_time")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("update_time")
    private Date updateTime;

}

@Data
public class MenuVO {

    private Integer id;

    /**
     * 上级菜单
     */
    @JsonProperty("parent_id")
    private Integer parentId;

    /**
     * 菜单名称
     */
    private String name;

    /**
     * 菜单URL
     */
    private String url;

    /**
     * 菜单路由
     */
    private String path;

    /**
     * 菜单标题
     */
    private String title;

    /**
     * 图标
     */
    private String icon;

    /**
     * 排序
     */
    private Integer sort;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("create_time")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JsonProperty("update_time")
    private Date updateTime;

    /**
     * 子菜单
     */
    private List<MenuVO> children;

}

Request对象

前端传递
?

@Data
@ApiModel(value = "菜单", description = "菜单请求对象")
public class MenuReq {

    @ApiModelProperty(value = "菜单id", example = "1")
    private Integer id;

    @ApiModelProperty(value = "上级菜单id", example = "-1", required = true)
    @JsonProperty(value = "parent_id")
    private Integer parentId;

    @ApiModelProperty(value = "菜单名称", example = "AdminManager", required = true)
    private String name;

    @ApiModelProperty(value = "菜单URL", example = "Layout", required = true)
    private String url;

    @ApiModelProperty(value = "菜单路由", example = "/admin", required = true)
    private String path;

    @ApiModelProperty(value = "菜单标题", example = "后台管理", required = true)
    private String title;

    @ApiModelProperty(value = "菜单图标", example = "lock")
    private String icon;

    @ApiModelProperty(value = "排序", example = "1", required = true)
    private Integer sort;

}
@Data
@ApiModel(value = "指令权限", description = "权限请求对象")
public class PermissionReq {

    @ApiModelProperty(value = "权限id", example = "1")
    private Integer id;

    @ApiModelProperty(value = "指令权限", example = "user-center:user:saveOrUpdate", required = true)
    private String permission;

    @ApiModelProperty(value = "权限名称", example = "用户新增或修改", required = true)
    private String name;

}
@Data
@ApiModel(value = "角色菜单", description = "角色分配菜单请求对象")
public class RoleMenuReq {

    @ApiModelProperty(value = "角色id", example = "1")
    @JsonProperty("role_id")
    private int roleId;

    @ApiModelProperty(value = "菜单列表", example = "[1,2,3]")
    @JsonProperty("menu_ids")
    private List<Integer> menuIds;

}
@Data
@ApiModel(value = "角色权限", description = "角色分配指令权限请求对象")
public class RolePermissionReq {

    @ApiModelProperty(value = "角色id", example = "1")
    @JsonProperty("role_id")
    private int roleId;

    @ApiModelProperty(value = "权限列表", example = "[1,2,3]")
    @JsonProperty("permission_ids")
    private List<Integer> permissionIds;

}
@Data
@ApiModel(value = "角色", description = "角色请求对象")
public class RoleReq {

    @ApiModelProperty(value = "角色id", example = "1")
    private Integer id;

    @ApiModelProperty(value = "角色编码", example = "admin", required = true)
    private String code;

    @ApiModelProperty(value = "角色名称", example = "管理员", required = true)
    private String name;

}
@Data
@ApiModel(value = "用户登录", description = "用户登录请求对象")
public class UserLoginReq {

    @ApiModelProperty(value = "用户名", example = "admin")
    private String username;

    @ApiModelProperty(value = "密码", example = "123456")
    private String password;

    @ApiModelProperty(value = "验证码", example = "123456")
    private String captcha;

}
@Data
@ApiModel(value = "用户", description = "用户请求对象")
public class UserReq {

    @ApiModelProperty(value = "用户id", example = "1")
    private Integer id;

    @ApiModelProperty(value = "用户名", example = "admin", required = true)
    private String username;

    @ApiModelProperty(value = "昵称", example = "管理员", required = true)
    private String nickname;

}
@Data
@ApiModel(value = "用户角色", description = "用户分配角色请求对象")
public class UserRoleReq {

    @ApiModelProperty(value = "用户id", example = "1")
    @JsonProperty("user_id")
    private int userId;

    @ApiModelProperty(value = "角色列表", example = "[1,2]")
    @JsonProperty("role_ids")
    private List<Integer> roleIds;

}

角色

RoleService

public interface RoleService {

    /**
     * 新增或修改
     *
     * @param roleReq
     */
    void saveOrUpdate(RoleReq roleReq);

    /**
     * 删除
     *
     * @param roleId
     */
    void delete(int roleId);

    /**
     * 分页列表
     *
     * @param params
     * @return
     */
    PageResult pageList(Map<String, Object> params);

    /**
     * 角色配置菜单列表
     *
     * @param roleMenuReq
     */
    void addRoleMenus(RoleMenuReq roleMenuReq);

    /**
     * 角色配置权限列表
     *
     * @param rolePermissionReq
     */
    void addRolePermissions(RolePermissionReq rolePermissionReq);

    /**
     * 获取用户角色列表
     *
     * @param userId
     * @return
     */
    List<RoleVO> roleList(int userId);
}

RoleServiceImpl


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void saveOrUpdate(RoleReq roleReq) {
        SysRoleDO sysRoleDO = new SysRoleDO();
        BeanUtils.copyProperties(roleReq, sysRoleDO);
        if (roleReq.getId() != null && roleReq.getId() > 0) {
            // 修改
            sysRoleDO.setUpdateTime(CommonUtil.getCurrentDate());
            roleMapper.updateById(sysRoleDO);
        } else {
            // 新增
            sysRoleDO.setCreateTime(CommonUtil.getCurrentDate());
            roleMapper.insert(sysRoleDO);
        }
    }

    /**
     * 删除角色
     * 1.删除角色信息
     * 2.解除角色菜单关系
     * 3.解除角色权限关系
     * 4.接触用户角色关系
     *
     * @param roleId
     */
    @Transactional(rollbackFor = BizException.class)
    @Override
    public void delete(int roleId) {
        roleMapper.deleteById(roleId);
        roleMapper.deleteRoleMenu(roleId);
        roleMapper.deleteRolePermission(roleId);
        roleMapper.deleteRoleUser(roleId);
    }

    /**
     * 分页列表,不返回菜单和权限信息
     *
     * @param params
     * @return
     */
    @Override
    public PageResult pageList(Map<String, Object> params) {
        if (CommonUtil.validatePageSize(params)) {
            throw new BizException(BizCodeEnum.REQUEST_PARAMS_ERROR);
        }
        int page = MapUtils.getInteger(params, "page");
        int size = MapUtils.getInteger(params, "size");

        String name = (String) params.get("name");
        String code = (String) params.get("code");

        Page<SysRoleDO> pageInfo = new Page<>(page, size);
        IPage<SysRoleDO> roleDOIPage = roleMapper.selectPage(pageInfo, new QueryWrapper<SysRoleDO>().orderByDesc("create_time")
                .like(StringUtils.isNotBlank(name), "name", name)
                .like(StringUtils.isNotBlank(code), "code", code));

        long total = roleDOIPage.getTotal();
        long pages = roleDOIPage.getPages();
        Object data = roleDOIPage.getRecords().stream().map(m -> ConvertBean.getProcess(m, RoleVO.class)).collect(Collectors.toList());

        return PageResult.builder().totalRecord(total).totalPage(pages).currentData(data).build();
    }

    /**
     * 给角色分配菜单
     *
     * @param roleMenuReq
     */
    @Transactional(rollbackFor = BizException.class)
    @Override
    public void addRoleMenus(RoleMenuReq roleMenuReq) {
        roleMapper.deleteRoleMenu(roleMenuReq.getRoleId());
        if (roleMenuReq.getMenuIds().size() > 0) {
            roleMapper.addRoleMenus(roleMenuReq.getRoleId(), roleMenuReq.getMenuIds());
        }
    }

    /**
     * 给角色分配指令权限
     *
     * @param rolePermissionReq
     */
    @Transactional(rollbackFor = BizException.class)
    @Override
    public void addRolePermissions(RolePermissionReq rolePermissionReq) {
        roleMapper.deleteRolePermission(rolePermissionReq.getRoleId());
        if (rolePermissionReq.getPermissionIds().size() > 0) {
            roleMapper.addRolePermissions(rolePermissionReq.getRoleId(), rolePermissionReq.getPermissionIds());
        }
    }

    /**
     * 用户角色列表
     *
     * @param userId 小于等于-1时获取全部角色列表
     * @return
     */
    @Override
    public List<RoleVO> roleList(int userId) {
        List<SysRoleDO> sysRoleDOList;
        if (userId <= -1) {
            sysRoleDOList = roleMapper.selectList(new QueryWrapper<SysRoleDO>().orderByDesc("create_time"));
        } else {
            sysRoleDOList = roleMapper.findRoleListByUserId(userId);
        }

        return sysRoleDOList.stream().map(m -> ConvertBean.getProcess(m, RoleVO.class)).collect(Collectors.toList());
    }
}

RoleMapper

public interface RoleMapper extends BaseMapper<SysRoleDO> {

    List<SysRoleDO> findRoleListByUserId(int userId);

    SysRoleDO findRoleByCode(String code);

    void deleteRoleMenu(int roleId);

    void deleteRolePermission(int roleId);

    void deleteRoleUser(int roleId);

    void addRoleMenus(int roleId, @Param("list") List<Integer> menuIds);

    void addRolePermissions(int roleId, @Param("list") List<Integer> permissionIds);

}
    <insert id="addRoleMenus">
        insert into role_menu(role_id, menu_id) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{arg0},#{item})
        </foreach>
    </insert>
    <insert id="addRolePermissions">
        insert into role_permission(role_id, permission_id) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{arg0},#{item})
        </foreach>
    </insert>
    <delete id="deleteRoleMenu">
        delete
        from role_menu
        where role_id = #{arg0}
    </delete>
    <delete id="deleteRolePermission" parameterType="int">
        delete
        from role_permission
        where role_id = #{arg0}
    </delete>
    <delete id="deleteRoleUser" parameterType="int">
        delete
        from user_role
        where role_id = #{arg0}
    </delete>
    <select id="findRoleListByUserId" resultType="net.work.model.SysRoleDO" parameterType="int">
        select r.*
        from user_role ur
                 left join sys_role r on ur.role_id = r.id
        where ur.user_id = #{arg0}
    </select>
    <select id="findRoleByCode" resultType="net.work.model.SysRoleDO" parameterType="java.lang.String">
        select *
        from sys_role
        where code = #{arg0} limit 1
    </select>

RoleController


@Api(tags = "角色模块")
@RestController
@RequestMapping("/api/role/v1")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("角色分页列表")
    @GetMapping("page_list")
    public JsonData pageList(@RequestParam Map<String, Object> params) {
        return JsonData.buildSuccess(roleService.pageList(params));
    }

    @ApiOperation("获取用户角色列表")
    @GetMapping("role_list")
    public JsonData pageList(@RequestParam("user_id") int userId) {
        return JsonData.buildSuccess(roleService.roleList(userId));
    }

    @ApiOperation("角色新增或修改")
    @PostMapping("saveOrUpdate")
    public JsonData saveOrUpdate(@RequestBody RoleReq request) {
        try {
            roleService.saveOrUpdate(request);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation("删除角色")
    @DeleteMapping("delete/{role_id}")
    public JsonData delete(@PathVariable("role_id") int roleId) {
        try {
            roleService.delete(roleId);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation("角色分配菜单")
    @PostMapping("addRoleMenus")
    public JsonData addRoleMenus(@RequestBody RoleMenuReq request) {
        try {
            roleService.addRoleMenus(request);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation("角色分配权限")
    @PostMapping("addRolePermissions")
    public JsonData addRolePermissions(@RequestBody RolePermissionReq request) {
        try {
            roleService.addRolePermissions(request);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

}

菜单

MenuService

public interface MenuService {

    /**
     * 新增或修改
     *
     * @param menuReq
     */
    void saveOrUpdate(MenuReq menuReq);

    /**
     * 删除
     *
     * @param id
     */
    void delete(int id);

    /**
     * 菜单列表
     *
     * @return
     */
    List<MenuVO> menuList();

    /**
     * 角色下菜单列表
     *
     * @param roleId
     * @return
     */
    List<MenuVO> roleList(int roleId);

    /**
     * 角色下菜单列表(不包含父节点)
     *
     * @param roleId
     * @return
     */
    List<MenuVO> roleListNoParent(int roleId);
}

MenuServiceImpl


@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 菜单新增或修改
     *
     * @param menuReq
     */
    @Override
    public void saveOrUpdate(MenuReq menuReq) {
        SysMenuDO sysMenuDO = new SysMenuDO();
        BeanUtils.copyProperties(menuReq, sysMenuDO);
        if (menuReq.getId() != null && menuReq.getId() > 0) {
            // 修改
            sysMenuDO.setUpdateTime(CommonUtil.getCurrentDate());
            menuMapper.updateById(sysMenuDO);
        } else {
            // 新增
            sysMenuDO.setCreateTime(CommonUtil.getCurrentDate());
            menuMapper.insert(sysMenuDO);
        }
    }

    /**
     * 删除菜单项,包括子项
     *
     * @param id
     */
    @Transactional(rollbackFor = BizException.class)
    @Override
    public void delete(int id) {
        menuMapper.deleteById(id);
        menuMapper.delete(new QueryWrapper<SysMenuDO>().eq("parent_id", id));
        menuMapper.deleteRoleMenuById(id);
    }

    /**
     * 菜单项列表(树状结构)
     *
     * @return
     */
    @Override
    public List<MenuVO> menuList() {
        // 根菜单
        List<SysMenuDO> sysMenuDOList = menuMapper.selectList(new QueryWrapper<SysMenuDO>().eq("parent_id", -1).orderByAsc("sort"));
        List<MenuVO> menuVOList = sysMenuDOList.stream().map(m -> ConvertBean.getProcess(m, MenuVO.class)).collect(Collectors.toList());
        // 遍历目录菜单
        menuVOList.forEach(menuVO -> {
            List<MenuVO> children = new ArrayList<>();

            getMenuChildren(menuVO, children);
        });

        return menuVOList;
    }

    /**
     * 菜单项列表(普通列表,不是树状结构)
     *
     * @param roleId
     * @return
     */
    @Override
    public List<MenuVO> roleList(int roleId) {
        List<MenuVO> menuVOList = new ArrayList<>();
        List<SysMenuDO> sysMenuDOList;
        if (roleId <= -1) {
            sysMenuDOList = menuMapper.selectList(new QueryWrapper<SysMenuDO>().orderByAsc("sort"));
            // 创建一个顶级菜单
            MenuVO menuVO = new MenuVO();
            menuVO.setId(-1);
            menuVO.setTitle("顶级菜单");
            menuVOList.add(menuVO);
            menuVOList.addAll(sysMenuDOList.stream().map(m -> ConvertBean.getProcess(m, MenuVO.class)).collect(Collectors.toList()));
        } else {
            // 不返回父节点
            sysMenuDOList = menuMapper.findMenuListByRoleId(roleId);
            menuVOList = sysMenuDOList.stream().map(m -> ConvertBean.getProcess(m, MenuVO.class)).collect(Collectors.toList());
        }

        return menuVOList;
    }

    /**
     * 角色下的菜单列表,没有父节点
     *
     * @param roleId
     * @return
     */
    @Override
    public List<MenuVO> roleListNoParent(int roleId) {
        List<SysMenuDO> sysMenuDOList = menuMapper.findMenuListByRoleIdNoParent(roleId);
        return sysMenuDOList.stream().map(m -> ConvertBean.getProcess(m, MenuVO.class)).collect(Collectors.toList());
    }

    /**
     * 获取子菜单
     *
     * @param menuVO
     * @param children
     */
    private void getMenuChildren(MenuVO menuVO, List<MenuVO> children) {
        // 查询当前菜单的子菜单列表
        List<SysMenuDO> sysMenuDOList = menuMapper.selectList(new QueryWrapper<SysMenuDO>().eq("parent_id", menuVO.getId()).orderByAsc("sort"));
        List<MenuVO> menuVOList = sysMenuDOList.stream().map(m -> ConvertBean.getProcess(m, MenuVO.class)).collect(Collectors.toList());
        if (menuVOList.size() > 0) {
            children.addAll(menuVOList);
            menuVO.setChildren(children);
            menuVOList.forEach(menu -> getMenuChildren(menu, new ArrayList<>()));
        }
    }
}

MenuMapper

public interface MenuMapper extends BaseMapper<SysMenuDO> {

    List<SysMenuDO> findMenuListByRoleId(int roleId);

    List<SysMenuDO> findMenuListByRoleIdNoParent(int roleId);

    void deleteRoleMenuById(int id);
}
    <delete id="deleteRoleMenuById" parameterType="int">
        delete
        from role_menu
        where menu_id = #{arg0}
    </delete>

    <select id="findMenuListByRoleId" resultType="net.work.model.SysMenuDO" parameterType="int">
        select p.*
        from role_menu rp
                 left join sys_menu p on rp.menu_id = p.id
        where rp.role_id = #{arg0}
    </select>

    <select id="findMenuListByRoleIdNoParent" resultType="net.work.model.SysMenuDO" parameterType="int">
        select p.*
        from role_menu rp
                 left join sys_menu p on rp.menu_id = p.id
        where rp.role_id = #{arg0}
          and p.parent_id in (select id from sys_menu)
    </select>

MenuController


@Api(tags = "菜单模块")
@RestController
@RequestMapping("/api/menu/v1")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("菜单列表(包含子菜单,树状结构)")
    @GetMapping("list")
    public JsonData menuList() {
        return JsonData.buildSuccess(menuService.menuList());
    }

    @ApiOperation("获取角色所有菜单项(普通列表)")
    @GetMapping("role_list")
    public JsonData roleList(@RequestParam("role_id") int roleId) {
        return JsonData.buildSuccess(menuService.roleList(roleId));
    }

    @ApiOperation("获取角色所有菜单项(普通列表,无父节点)")
    @GetMapping("role_list/no_parent")
    public JsonData roleListNoParent(@RequestParam("role_id") int roleId) {
        return JsonData.buildSuccess(menuService.roleListNoParent(roleId));
    }

    @ApiOperation("菜单新增或修改")
    @PostMapping("saveOrUpdate")
    public JsonData saveOrUpdate(@RequestBody MenuReq menuReq) {
        try {
            menuService.saveOrUpdate(menuReq);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("delete/{menu_id}")
    public JsonData delete(@PathVariable("menu_id") int menuId) {
        try {
            menuService.delete(menuId);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

}

权限指令

PermissionService

public interface PermissionService {

    /**
     * 新增或修改
     *
     * @param permissionReq
     */
    void saveOrUpdate(PermissionReq permissionReq);

    /**
     * 删除
     *
     * @param id
     */
    void delete(int id);

    /**
     * 分页列表
     *
     * @param params
     * @return
     */
    PageResult pageList(Map<String, Object> params);

    /**
     * 所有权限列表
     *
     * @param roleId
     * @return
     */
    List<PermissionVO> roleList(int roleId);
}

PermissionServiceImpl


@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 新增或修改
     *
     * @param permissionReq
     */
    @Override
    public void saveOrUpdate(PermissionReq permissionReq) {
        SysPermissionDO sysPermissionDO = new SysPermissionDO();
        BeanUtils.copyProperties(permissionReq, sysPermissionDO);
        if (permissionReq.getId() != null && permissionReq.getId() > 0) {
            // 修改
            sysPermissionDO.setUpdateTime(CommonUtil.getCurrentDate());
            permissionMapper.updateById(sysPermissionDO);
        } else {
            // 新增
            sysPermissionDO.setCreateTime(CommonUtil.getCurrentDate());
            permissionMapper.insert(sysPermissionDO);
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Transactional(rollbackFor = BizException.class)
    @Override
    public void delete(int id) {
        permissionMapper.deleteById(id);
        permissionMapper.delete(new QueryWrapper<SysPermissionDO>().eq("permission_id", id));
    }

    /**
     * 指令权限分页列表
     *
     * @param params
     * @return
     */
    @Override
    public PageResult pageList(Map<String, Object> params) {
        if (CommonUtil.validatePageSize(params)) {
            throw new BizException(BizCodeEnum.REQUEST_PARAMS_ERROR);
        }
        int page = MapUtils.getInteger(params, "page");
        int size = MapUtils.getInteger(params, "size");

        String name = (String) params.get("name");
        String permission = (String) params.get("permission");

        Page<SysPermissionDO> pageInfo = new Page<>(page, size);
        IPage<SysPermissionDO> permissionDOIPage = permissionMapper.selectPage(pageInfo, new QueryWrapper<SysPermissionDO>().orderByDesc("create_time")
                .like(StringUtils.isNotBlank(name), "name", name)
                .like(StringUtils.isNotBlank(permission), "permission", permission));

        long total = permissionDOIPage.getTotal();
        long pages = permissionDOIPage.getPages();
        Object data = permissionDOIPage.getRecords().stream().map(m -> ConvertBean.getProcess(m, PermissionVO.class)).collect(Collectors.toList());

        return PageResult.builder().totalRecord(total).totalPage(pages).currentData(data).build();
    }

    /**
     * 所有权限列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<PermissionVO> roleList(int roleId) {
        List<SysPermissionDO> sysPermissionDOList;
        if (roleId <= -1) {
            sysPermissionDOList = permissionMapper.selectList(new QueryWrapper<SysPermissionDO>().orderByDesc("create_time"));
        } else {
            sysPermissionDOList = permissionMapper.findPermissionListByRoleId(roleId);
        }

        return sysPermissionDOList.stream().map(m -> ConvertBean.getProcess(m, PermissionVO.class)).collect(Collectors.toList());
    }
}

PermissionMapper

public interface PermissionMapper extends BaseMapper<SysPermissionDO> {

    List<SysPermissionDO> findPermissionListByRoleId(int roleId);

}
    <select id="findPermissionListByRoleId" resultType="net.work.model.SysPermissionDO" parameterType="int">
        select p.*
        from role_permission rp
                 left join sys_permission p on rp.permission_id = p.id
        where rp.role_id = #{arg0}
    </select>

PermissionController


@Api(tags = "权限模块")
@RestController
@RequestMapping("/api/permission/v1")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("权限分页列表")
    @GetMapping("page_list")
    public JsonData pageList(@RequestParam Map<String, Object> params) {
        return JsonData.buildSuccess(permissionService.pageList(params));
    }

    @ApiOperation("角色权限列表")
    @GetMapping("role_list")
    public JsonData roleList(@RequestParam("role_id") int roleId) {
        return JsonData.buildSuccess(permissionService.roleList(roleId));
    }

    @ApiOperation("权限新增或修改")
    @PostMapping("saveOrUpdate")
    public JsonData saveOrUpdate(@RequestBody PermissionReq permissionReq) {
        try {
            permissionService.saveOrUpdate(permissionReq);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation("删除权限")
    @DeleteMapping("delete/{permission_id}")
    public JsonData delete(@PathVariable("permission_id") int permissionId) {
        try {
            permissionService.delete(permissionId);
            return JsonData.buildSuccess();
        } catch (Exception e) {
            return JsonData.buildError("操作失败:" + e.getMessage());
        }
    }

}

下一次来实现角色、权限的接口访问控制,不整合shiro,结合redis自己实现!会做一些说明

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-13 12:40:04  更:2021-12-13 12:42:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:32:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码