目录
学习目标:
资源列表的增删改查
学习内容:
资源列表增删改查
按照思路书写代码即可
思路:
添加资源 1. 参数校验 模块名称 moduleName 非空,同一层级下模块名称唯一 地址 url 二级菜单(grade=1),非空且同一层级下不可重复 父级菜单 parentId 一级菜单(目录 grade=0) -1 二级|三级菜单(菜单|按钮 grade=1或2) 非空,父级菜单必须存在 层级 grade 非空,0|1|2 权限码 optValue 非空,不可重复 2. 设置参数的默认值 是否有效 isValid 1 创建时间createDate 系统当前时间 修改时间updateDate 系统当前时间 3. 执行添加操作,判断受影响的行数 修改资源 1. 参数校验 id 非空,数据存在 层级 grade 非空 0|1|2 模块名称 moduleName 非空,同一层级下模块名称唯一 (不包含当前修改记录本身) 地址 url 二级菜单(grade=1),非空且同一层级下不可重复(不包含当前修改记录本身) 权限码 optValue 非空,不可重复(不包含当前修改记录本身) 2. 设置参数的默认值 修改时间updateDate 系统当前时间 3. 执行更新操作,判断受影响的行数 删除资源 1. 判断删除的记录是否存在 2. 如果当前资源存在子记录,则不可删除 3. 删除资源时,将对应的权限表的记录也删除(判断权限表中是否存在关联数据,如果存在,则删除) 4. 执行删除(更新)操作,判断受影响的行数
dao层
书写几个查询用于资源名称、URL和权限吗唯一的判断,删除操作下需要判断是否存在子记录
新思想:
需要把对应的权限表中的记录也删除,所以需要在对应的权限表中加入搜索和删除的SQL语句
//查询所有资源数据
List<Module> queryModuleList();
//查询该层级是否有这个资源名称
Module queryModuleByGradeAndModuleName(@Param("grade") Integer grade, @Param("moduleName")String moduleName);
//查询该层级是否有这个URL
Module queryModuleByGradeAndUrl(@Param("grade")Integer grade, @Param("url")String url);
//通过权限吗查找资源
Module queryModuleByOptValue(String optValue);
// 查询指定资源是否存在子记录
Integer queryModuleByParentId(Integer id);
<!-- 查询所有资源列表 -->
<select id="queryAllModules" resultType="com.xxxx.crm.model.TreeModel">
select id,
parent_id as pId,
module_name as name
from t_module
where is_valid = 1
</select>
<select id="queryModuleList" resultType="com.xxxx.crm.vo.Module">
select
<include refid="Base_Column_List"/>
from t_module
where is_valid = 1
</select>
<!-- 查询该层级是否有这个资源名称 -->
<select id="queryModuleByGradeAndModuleName" resultType="com.xxxx.crm.vo.Module" >
select
<include refid="Base_Column_List"/>
from
t_module
where
is_valid = 1 and grade = #{grade} and module_name = #{moduleName}
</select>
<!-- 查询该层级是否有这个URL -->
<select id="queryModuleByGradeAndUrl" resultType="com.xxxx.crm.vo.Module">
select
<include refid="Base_Column_List"/>
from
t_module
where
is_valid = 1 and grade = #{grade} and url = #{url}
</select>
<!-- 通过权限吗查找资源 -->
<select id="queryModuleByOptValue" resultType="com.xxxx.crm.vo.Module" parameterType="string">
select
<include refid="Base_Column_List"/>
from
t_module
where
is_valid = 1 and opt_value = #{optValue}
</select>
<!-- 查询指定资源是否存在子记录 -->
<select id="queryModuleByParentId" parameterType="int" resultType="java.lang.Integer">
select
count(1)
from
t_module
where
is_valid = 1 and parent_id = #{id}
</select>
// 通过资源ID查询权限记录
Integer countPermissionByModuleId(Integer id);
// 通过资源ID删除权限记录
Integer deletePermissionByModuleId(Integer id);
<!-- 通过资源ID查询权限记录 -->
<select id="countPermissionByModuleId" parameterType="int" resultType="java.lang.Integer">
select
count(1)
from
t_permission
where
module_id=#{id}
</select>
<!-- 通过资源ID删除权限记录 -->
<delete id="deletePermissionByModuleId" parameterType="int">
delete from
t_permission
where
module_id=#{id}
</delete>
service层
@Service
public class ModuleService extends BaseService<Module, Integer> {
@Resource
ModuleMapper moduleMapper;
@Resource
PermissionMapper permissionMapper;
/**
* 查询所有的资源列表
*
* @param
* @return java.util.List<com.xxxx.crm.model.TreeModel>
* @author QQ星
* @Date 2022/3/28 18:00
*/
public List<TreeModel> queryAllModules(Integer roleId) {
// 查询所有的资源列表
List<TreeModel> treeModelList = moduleMapper.queryAllModules();
//查询指定角色已经授权过的资源列表(查询角色拥有的资源ID)
List<Integer> permissionIds = permissionMapper.queryRoleHasModuleIdsByRoleId(roleId);
//判断角色是否拥有资源ID
if (permissionIds != null && permissionIds.size() > 0) {
//循环所有资源列表,判断用户拥有的资源ID中是否有匹配的,如果有,则设置checked属性为true
treeModelList.forEach(treeModel -> {
//判断角色拥有的资源ID是狗有当前遍历的资源ID
if (permissionIds.contains(treeModel.getId())) {
//包含说明已授权
treeModel.setChecked(true);
}
});
}
return treeModelList;
}
/**
* 查询资源数据
*
* @param
* @return java.util.Map<java.lang.String, java.lang.Object>
* @author QQ星
* @Date 2022/3/29 22:43
*/
public Map<String, Object> queryModuleList() {
Map<String, Object> map = new HashMap<>();
//查询资源列表
List<Module> moduleList = moduleMapper.queryModuleList();
map.put("code", 0);
map.put("msg", "success");
map.put("count", moduleList.size());
map.put("data", moduleList);
return map;
}
/**
* 添加资源
* 1. 参数校验
* 模块名称 moduleName
* 非空,同一层级下模块名称唯一
* 地址 url
* 二级菜单(grade=1/2),非空且同一层级下不可重复
* 父级菜单 parentId
* 一级菜单(目录 grade=0) -1
* 二级|三级菜单(菜单|按钮 grade=1或2) 非空,父级菜单必须存在
* 层级 grade
* 非空,0|1|2
* 权限码 optValue
* 非空,不可重复
* 2. 设置参数的默认值
* 是否有效 isValid 1
* 创建时间createDate 系统当前时间
* 修改时间updateDate 系统当前时间
* 3. 执行添加操作,判断受影响的行数
*
* @param module
* @return void
* @author QQ星
* @Date 2022/3/30 14:33
*/
@Transactional(propagation = Propagation.REQUIRED)
public void addModule(Module module) {
/* 1.参数校检 */
//获取层级,根据层级来分类作判断
Integer grade = module.getGrade();
AssertUtil.isTrue(grade == null || !(grade == 0 || grade == 1 || grade == 2), "层级不正确!");
//判断名称非空
AssertUtil.isTrue(StringUtils.isBlank(module.getModuleName()), "资源名称不能为空!");
//同意层级下模块名称唯一
AssertUtil.isTrue(null != moduleMapper.queryModuleByGradeAndModuleName(grade, module.getModuleName()), "该层级下此名称已存在!");
//判断URL
if (grade == 1 || grade == 2) {
//判断名称非空
AssertUtil.isTrue(StringUtils.isBlank(module.getUrl()), "资源的URL不能为空!");
//同意层级下模块名称唯一
AssertUtil.isTrue(null != moduleMapper.queryModuleByGradeAndUrl(grade, module.getUrl()), "该层级下此URL已存在!");
}
//判断parentId,0需要把父ID设置为-1
if (grade == 0) {
module.setParentId(-1);
}
//为1/2的情况下
if (grade != 0) {
//非空
AssertUtil.isTrue(null == module.getParentId(), "父级菜单不能为空");
父级菜单必须存在 (将父级菜单的ID作为主键,查询资源记录)
AssertUtil.isTrue(null == moduleMapper.selectByPrimaryKey(module.getParentId()), "父级菜单不存在,请重试!");
}
//权限码 optValue
//非空
AssertUtil.isTrue(StringUtils.isBlank(module.getOptValue()), "权限码不能为空!");
//不可重复
AssertUtil.isTrue(null != moduleMapper.queryModuleByOptValue(module.getOptValue()), "权限码已存在!");
/* 2. 设置参数的默认值 */
module.setIsValid(1);
module.setCreateDate(new Date());
module.setUpdateDate(new Date());
/* 3. 执行添加操作,判断受影响的行数 */
AssertUtil.isTrue(moduleMapper.insertSelective(module) < 1, "添加资源失败!");
}
/**
* 修改资源
* 1. 参数校验
* id
* 非空,数据存在
* 层级 grade
* 非空 0|1|2
* 模块名称 moduleName
* 非空,同一层级下模块名称唯一 (不包含当前修改记录本身)
* 地址 url
* 二级菜单(grade=1),非空且同一层级下不可重复(不包含当前修改记录本身)
* 权限码 optValue
* 非空,不可重复(不包含当前修改记录本身)
* 2. 设置参数的默认值
* 修改时间updateDate 系统当前时间
* 3. 执行更新操作,判断受影响的行数
* @author QQ星
*
* @param module
* @return void
* @Date 2022/3/30 23:05
*/
@Transactional(propagation = Propagation.REQUIRED)
public void updateModule(Module module) {
/* 1.参数校检 */
//id 非空
AssertUtil.isTrue(null==module.getId(),"资源id不能为空!");
//存在
Module temp = moduleMapper.selectByPrimaryKey(module.getId());
AssertUtil.isTrue(null==temp,"待更新记录不存在!");
//grade
//获取层级,根据层级来分类作判断
Integer grade = module.getGrade();
AssertUtil.isTrue(grade == null || !(grade == 0 || grade == 1 || grade == 2), "层级不正确!");
//判断名称非空
AssertUtil.isTrue(StringUtils.isBlank(module.getModuleName()), "资源名称不能为空!");
//通过层级和名称查询对象
temp = moduleMapper.queryModuleByGradeAndModuleName(module.getGrade(),module.getModuleName());
if(temp != null){
AssertUtil.isTrue(!(temp.getId().equals(module.getId())),"该层级下此惨淡名称已存在!");
}
// 地址 url
if (grade == 1 || grade == 2) {
AssertUtil.isTrue(StringUtils.isBlank(module.getUrl()), "菜单URL不能为空!");
// 通过层级与菜单URl查询资源对象
temp = moduleMapper.queryModuleByGradeAndUrl(grade, module.getUrl());
// 判断是否存在
if (temp != null) {
AssertUtil.isTrue(!(temp.getId()).equals(module.getId()), "该层级下菜单URL已存在!");
}
}
// 权限码 optValue 非空,不可重复(不包含当前修改记录本身)
AssertUtil.isTrue(StringUtils.isBlank(module.getOptValue()), "权限码不能为空!");
// 通过权限码查询资源对象
temp = moduleMapper.queryModuleByOptValue(module.getOptValue());
// 判断是否为空
if (temp != null) {
AssertUtil.isTrue(!(temp.getId()).equals(module.getId()),"权限码已存在!");
}
/* 2. 设置参数的默认值 */
// 修改时间 系统当前时间
module.setUpdateDate(new Date());
/* 3. 执行更新操作,判断受影响的行数 */
AssertUtil.isTrue(moduleMapper.updateByPrimaryKeySelective(module) < 1, "修改资源失败!");
}
/**
* 删除资源
* 1. 判断删除的记录是否存在
* 2. 如果当前资源存在子记录,则不可删除
* 3. 删除资源时,将对应的权限表的记录也删除(判断权限表中是否存在关联数据,如果存在,则删除)
* 4. 执行删除(更新)操作,判断受影响的行数
* @author QQ星
*
* @param id
* @return void
* @Date 2022/3/30 23:35
*/
@Transactional(propagation = Propagation.REQUIRED)
public void deleteModule(Integer id) {
// 判断id是否为空
AssertUtil.isTrue(null == id, "待删除记录不存在!");
// 通过id查询资源对象
Module temp = moduleMapper.selectByPrimaryKey(id);
// 判断资源对象是否为空
AssertUtil.isTrue(null == temp, "待删除记录不存在!");
// 如果当前资源存在子记录(将id当做父Id查询资源记录)
Integer count = moduleMapper.queryModuleByParentId(id);
// 如果存在子记录,则不可删除
AssertUtil.isTrue(count > 0, "该资源存在子记录,不可删除!");
// 通过资源id查询权限表中是否存在数据
count = permissionMapper.countPermissionByModuleId(id);
// 判断是否存在,存在则删除
if (count > 0) {
// 删除指定资源ID的权限记录
permissionMapper.deletePermissionByModuleId(id);
}
// 设置记录无效
temp.setIsValid(0);
temp.setUpdateDate(new Date());
// 执行更新
AssertUtil.isTrue(moduleMapper.updateByPrimaryKeySelective(temp) < 1, "删除资源失败!");
}
}
controller层
@Controller
@RequestMapping("module")
public class ModuleController extends BaseController {
@Resource
ModuleService moduleService;
/**
* 查询所有资源列表
*
* @param
* @return java.util.List<com.xxxx.crm.model.TreeModel>
* @author QQ星
* @Date 2022/3/28 18:01
*/
@RequestMapping("queryAllModules")
@ResponseBody
public List<TreeModel> queryAllModules(Integer roleId) {
return moduleService.queryAllModules(roleId);
}
/**
* 进入授权界面
*
* @param roleId
* @return java.lang.String
* @author QQ星
* @Date 2022/3/28 21:31
*/
@RequestMapping("toAddGrantPage")
public String toAddGrantPage(Integer roleId, HttpServletRequest request) {
request.setAttribute("roleId", roleId);
return "role/grant";
}
/**
* 查询资源列表
* 603001
* @author QQ星
*
* @param
* @return java.util.Map<java.lang.String,java.lang.Object>
* @Date 2022/3/29 22:44
*/
@RequiredPermission(code = "603001")
@RequestMapping("list")
@ResponseBody
public Map<String,Object> queryModuleList(){
return moduleService.queryModuleList();
}
/**
* 进入资源管理界面
* 6030
* @author QQ星
*
* @return java.lang.String
* @Date 2022/3/29 22:41
*/
@RequiredPermission(code = "6030")
@RequestMapping("index")
public String index(){
return "module/module";
}
/**
* 添加资源
* 603002
* @author QQ星
*
* @param module
* @return com.xxxx.crm.base.ResultInfo
* @Date 2022/3/30 15:05
*/
@RequiredPermission(code = "603002")
@ResponseBody
@PostMapping("add")
public ResultInfo addModule(Module module){
moduleService.addModule(module);
return success("添加资源成功");
}
/**
* 打开添加资源界面
* @author QQ星
*
* @param grade
* @param parentId
* @param request
* @return java.lang.String
* @Date 2022/3/30 15:06
*/
@RequestMapping("toAddModulePage")
public String toAddModulePage(Integer grade, Integer parentId, HttpServletRequest request) {
// 将数据设置到请求域中
request.setAttribute("grade", grade);
request.setAttribute("parentId", parentId);
return "module/add";
}
/**
* 修改资源
* 603003
* @author QQ星
*
* @param module
* @return com.xxxx.crm.base.ResultInfo
* @Date 2022/3/30 23:00
*/
@RequiredPermission(code = "603003")
@RequestMapping("update")
@ResponseBody
public ResultInfo updateModule(Module module){
moduleService.updateModule(module);
return success("修改资源成功");
}
/**
* 进入修改界面
* @author QQ星
*
* @param id
* @param model
* @return java.lang.String
* @Date 2022/3/30 23:03
*/
@RequestMapping("toUpdateModulePage")
public String toUpdateModulePage(Integer id, Model model){
//将对象放进去
model.addAttribute("module", moduleService.selectByPrimaryKey(id));
return "module/update";
}
/**
* 删除资源
* 603004
* @author QQ星
*
* @param id
* @return com.xxxx.crm.base.ResultInfo
* @Date 2022/3/30 23:26
*/
@RequiredPermission(code = "603004")
@PostMapping("delete")
@ResponseBody
public ResultInfo deleteModule(Integer id) {
moduleService.deleteModule(id);
return success("删除资源成功!");
}
}
反思:
第三模块基本完成,还有一个字典的增删改查操作,因为还没有设计好,感觉字典和资源有点相似,可能也没什么必要做,先开始下一模块的学习,大不了到最后给他补上字典管理