角色管理
角色查询
角色查询直接查询即可
角色查询执行流程
编写RoleMapper接口
public interface RoleMapper {
/**
* 查询角色
*/
List<Role> findAll();
}
编写RoleService
public interface RoleService {
/**
* 查询角色
* @param page
* @param size
* @return
*/
List<Role> findAll(Integer page, Integer size);
}
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
/**
* 查询角色
* @param page
* @param size
* @return
*/
@Override
public List<Role> findAll(Integer page, Integer size) {
PageHelper.startPage(page,size);
return roleMapper.findAll();
}
}
编写RoleController
@Controller
@RequestMapping("role")
public class RoleController {
@Autowired
private RoleService roleService;
/**
* 查询所有
* @param page
* @param size
* @param model
* @return
*/
@GetMapping("findAll")
public String findAll(@RequestParam(value = "page", defaultValue = "1") Integer page ,
@RequestParam(value = "size",defaultValue = "5") Integer size,
Model model){
PageHelper.startPage(page,size);
List<Role> list = this.roleService.findAll();
PageInfo pageInfo = new PageInfo(list);
model.addAttribute("pageInfo",pageInfo);
return "role-list";
}
}
编写RoleMapper.xml
<?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="cn.yanqi.ssm.mapper.RoleMapper">
<select id="findAll" resultType="Role">
select * from role
</select>
</mapper>
编写role-list页面
将资料中的role-list.jsp文件复制到当前项目中即可
修改左侧导航链接
<li id="system-setting2">
<a rel="nofollow" href="${pageContext.request.contextPath}/role/findAll.do?page=1&size=5">
<span class="glyphicon glyphicon-object-align-bottom"></span> 角色管理
</a>
</li>
测试
角色添加
角色添加,直接添加即可
角色添加执行流程
编写RoleMapper接口
/**
* 添加角色
* @param role
*/
void save(Role role);
编写RoleService
/**
* 添加角色
* @param role
*/
void save(Role role);
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
/**
* 添加角色
* @param role
*/
@Override
public void save(Role role) {
this.roleMapper.save(role);
}
}
编写RoleController
@Controller
@RequestMapping("role")
public class RoleController {
@Autowired
private RoleService roleService;
/**
* 新增角色
* @param role
* @return
*/
@PostMapping("save")
public String save(Role role){
this.roleService.save(role);
return "redirect:findAll";
}
}
编写RoleMapper.xml
<!--添加角色-->
<insert id="save">
insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})
</insert>
编写role-add页面
将资料中的role-add.jsp文件复制到当前项目中即可
测试
角色详情
查询角色详情,包含角色的对应的资源权限,(角色表,资源权限表)
编写RoleController
/**
* 角色详情
* @param id
* @param model
* @return
*/
@GetMapping("findById")
public String findById(@RequestParam("id") Integer id , Model model){
Role role = this.roleService.findById(id);
model.addAttribute("role",role);
return "role-show";
}
编写RoleService
/**
* 角色详情
* @param id
* @return
*/
Role findById(Integer id);
/**
* 角色详情
* @param id
* @return
*/
@Override
public Role findById(Integer id) {
return this.roleMapper.findById(id);
}
编写RoleMapper
/**
* 角色详情
* @param id
* @return
*/
Role findById(Integer id);
编写RoleMapper.xml
<!--角色详情-->
<resultMap id="findByIdMap" type="Role" autoMapping="true">
<id column="rid" property="id"/>
<!--角色对应的关系-->
<collection property="permissions" javaType="List" ofType="Permission" autoMapping="true">
<id column="pid" property="id"/>
</collection>
</resultMap>
<select id="findById" resultMap="findByIdMap">
SELECT
*,
r.id rid,
p.id pid
FROM
role r
LEFT JOIN role_permission rp ON rp.roleId = r.id
LEFT JOIN permission p ON rp.permissionId = p.id
WHERE
r.id = #{id}
</select>
测试
查询哪些资源权限可以添加
编写RoleController
/**
* 添加资源权限前-查询哪些资源权限可以添加
* @param id
* @return
*/
@GetMapping("findRoleByIdAndAllPermission")
public String findRoleByIdAndAllPermission(@RequestParam("id") Integer id, Model model){
//查询当前角色的是谁
Role role = this.roleService.findById(id);
model.addAttribute("role",role);
//查询当前角色哪些权限可以添加
List<Permission> list = this.roleService.findOtherPermission(id);
model.addAttribute("permissionList", list);
return "role-permission-add";
}
编写RoleService
/**
* 角色详情 (已写过)
* @param id
* @return
*/
Role findById(Integer id);
/**
* 添加资源权限前-查询哪些资源权限可以添加
* @param id
* @return
*/
List<Permission> findOtherPermission(Integer id);
/**
* 角色详情 (已写过)
* @param id
* @return
*/
@Override
public Role findById(Integer id) {
return this.roleMapper.findById(id);
}
/**
* 添加资源权限前-查询哪些资源权限可以添加
* @param id
* @return
*/
@Override
public List<Permission> findOtherPermission(Integer id) {
return this.roleMapper.findOtherPermission(id);
}
编写RoleMapper
/**
* 角色详情 (已写过)
* @param id
* @return
*/
Role findById(Integer id);
/**
* 添加资源权限前-查询哪些资源权限可以添加
* @param id
* @return
*/
List<Permission> findOtherPermission(Integer id);
编写RoleMapper.xml
<!--添加资源权限前-查询哪些资源权限可以添加-->
<select id="findOtherPermission" resultType="Permission">
select * from permission where id not in(select permissionId from role_permission where roleId = #{id})
</select>
<!--角色详情 已写过-->
<resultMap id="findByIdMap" type="Role" autoMapping="true">
<id column="rid" property="id"/>
<!--角色对应的关系-->
<collection property="permissions" javaType="List" ofType="Permission" autoMapping="true">
<id column="pid" property="id"/>
</collection>
</resultMap>
<select id="findById" resultMap="findByIdMap">
SELECT
*,
r.id rid,
p.id pid
FROM
role r,
permission p,
role_permission rp
WHERE
rp.roleId = r.id AND
rp.permissionId = p.id and
r.id = #{id}
</select>
测试
给角色添加资源权限
编写RoleController
@PostMapping("addPermissionToRole")
public String addPermissionToRole(@RequestParam("roleId") Integer roleId ,
@RequestParam("ids") Integer[] ids){
this.roleService.addPermissionToRole(roleId,ids);
return "redirect:findAll";
}
编写RoleService
/**
* 给角色添加资源权限
* @param roleId
* @param permissionIds
* @return
*/
void addPermissionToRole(Integer roleId, Integer[] permissionIds);
/**
* 给角色添加资源权限
* @param roleId
* @return
*/
@Override
public void addPermissionToRole(Integer roleId, Integer[] permissionIds) {
for(Integer permissionId : permissionIds){
this.roleMapper.addPermissionToRole(roleId,permissionId);
}
}
编写RoleMapper
/**
* 给角色添加资源权限
* @param roleId
*/
void addPermissionToRole(@Param("roleId") Integer roleId,@Param("permissionId") Integer permissionId);
编写RoleMapper.xml
<!--给角色添加资源权限-->
<insert id="addPermissionToRole">
INSERT INTO `ssm_crud`.`role_permission`(`permissionId`, `roleId`) VALUES (#{permissionId},#{roleId});
</insert>
删除角色
编写RoleController
/**
* 删除角色
* @param id
* @return
*/
@GetMapping("deleteRole")
public String deleteRole(@RequestParam("id") Integer id){
this.roleService.deleteRole(id);
return "redirect:findAll";
}
编写RoleService
/**
* 删除角色
* @param id
* @return
*/
@Override
public void deleteRole(Integer id) {
//删除角色-从users_role表解关系
this.roleMapper.deleteFrom_users_role_ById(id);
//删除角色-从role_permission表解关系
this.roleMapper.deleteFrom_role_permission_ById(id);
//删除角色-从role表删除
this.roleMapper.deleteRole(id);
}
编写RoleMapper
/**
* 删除角色-从users_role表解关系
* @param id
*/
void deleteFrom_users_role_ById(Integer id);
/**
* 删除角色-从role_permission表解关系
* @param id
*/
void deleteFrom_role_permission_ById(Integer id);
/**
* 删除角色-从role表删除
* @param id
*/
void deleteRole(Integer id);
编写RoleMapper.xml
<!--删除角色-从users_role表解关系-->
<delete id="deleteFrom_users_role_ById">
delete from users_role where roleId = #{id}
</delete>
<!--删除角色-从role_permission表解关系-->
<delete id="deleteFrom_role_permission_ById">
delete from role_permission where roleId = #{id}
</delete>
<!--删除角色-从role表删除-->
<delete id="deleteRole">
delete from role where id = #{id}
</delete>
标签:return,springboot,角色,第五课,param,案例,role,Integer,id
From: https://blog.51cto.com/teayear/7127694