SSM项目添加权限控制(接上水文)
2020-07-26 23:59
337 查看
接上:
https://blog.csdn.net/qq_43923042/article/details/107561103
准备:新建两个表
1.首先添加两个bean类Role和UserRole
2.编写dao层接口
3.编写mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.zhongruan.dao.RoleDao" > <select id="findRoleIdByUserId" parameterType="int" resultType="int"> select roleId from xiaolaji_role_user where userId=#{id} </select> <select id="findRoleByUserId" parameterType="int" resultType="role"> select * from xiaolaji_role where id not in(select roleId from xiaolaji_role_user where userId=#{id}) </select> <insert id="addRole" parameterType="role"> insert into xiaolaji_Role(rolename,roledesc) values (#{rolename},#{roledesc}); </insert> </mapper>
4.完成service层的编写
package com.zhongruan.service.Impl; import com.zhongruan.beans.Role; import com.zhongruan.beans.UserRole; import com.zhongruan.dao.RoleDao; import com.zhongruan.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.xml.ws.ServiceMode; import java.util.List; @Service public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao; @Override public List<Integer> findRoleId(int userId) { return roleDao.findRoleIdByUserId(userId); } @Override public List<Role> findRoleByUserId(int id) { return roleDao.findRoleByUserId(id); } @Override public void add(List<Integer> ids, String userId) { for (int roleId:ids){ UserRole userRole=new UserRole(); userRole.setUserId(Integer.parseInt(userId)); userRole.setRoleId(roleId); roleDao.addRole(userRole); } } }
5.完善Controller层:
@RequestMapping("toAddRole.do") public ModelAndView toAddRole(int id){ List<Role> roleList=roleService.findRoleByUserId(id); ModelAndView mv=new ModelAndView(); mv.addObject("roles",roleList); mv.addObject("id",id); mv.setViewName("user-role-add"); return mv; } @RequestMapping("addRole.do") @ResponseBody public String add(String roleList,String userId){ System.out.println("roleList---:"+roleList); System.out.println("userId---:"+userId); String[] strs=roleList.split(","); List<Integer> ids=new ArrayList<>(); for (String i:strs){ ids.add(Integer.parseInt(i)); } roleService.add(ids,userId); return "toAddRole.do"; }
配置结果:
点击添加角色后会出现以下画面:
SSM项目算是结束了,下面为SSM项目建立的大致流程
相关文章推荐
- JavaWeb项目:Shiro实现简单的权限控制(整合SSM)
- SSM项目,使用拦截器进行权限控制
- 给文档库添加项目级权限控制
- 给文档库添加项目级权限控制
- 基于SSM框架的Web项目开发之添加权限
- Spring-security在SSM项目中实现简单的权限控制及用户登录退出(未完善)
- XCode8的一些权限控制,添加以免引起崩溃
- SSM整合shiro进行权限控制以及shiro的一些特殊功能实现
- magento|后台新增自定义菜单并添加子账户的权限控制
- shiro权限控制(一):shiro介绍以及整合SSM框架
- MongoDB 3.x 中添加用户和权限控制
- SSM项目练习笔记(企业权限权限管理)第八天
- vue实现权限控制路由(vue-router 动态添加路由)
- Vue项目的前端权限控制
- restful项目的权限控制实现技巧
- Shiro + SSM(框架) + Freemarker(jsp)讲解的权限控制
- SpringMVC给控制器添加自定义注解控制访问权限
- CXF-06:Web Service暴露后如何进行权限控制,CXF拦截器的理论以及如何为CXF的客户端和服务器端添加拦截器
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】
- java项目权限控制的理解和示例(基于shiro和传统拦截器filter两种方式)