您的位置:首页 > 其它

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项目建立的大致流程

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: