Java研学-RBAC权限控制(三)

2024-04-13 11:12
文章标签 java 控制 权限 研学 rbac

本文主要是介绍Java研学-RBAC权限控制(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

四 部门管理

1 数据库表

CREATE TABLE `department` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`sn` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

2 实体类

@Data
public class Department {private Long id;private String name;private String sn;
}

3 部门新增

① 前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="utf-8"><title>部门管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
<!--引用外部代码片段 common目录的fragment文件中的link-->
<div th:replace="common/fragment :: link"></div>
<div class="wrapper"><!--navbar 导航--><div th:replace="common/fragment :: navbar"></div><!--menu 菜单--><div th:replace="common/fragment :: menu"></div><div class="content-wrapper"><section class="content-header"><h1>部门管理</h1></section><section class="content"><div class="box"><!--高级查询---><form class="form-inline" id="searchForm" action="/department/list" method="post"><input type="hidden" name="currentPage" id="currentPage" value="1"><a href="#" class="btn btn-success btn-input" style="margin: 10px"><span class="glyphicon glyphicon-plus"></span> 添加</a></form><!--编写内容--><div class="box-body table-responsive "><table class="table table-hover table-bordered table-striped"><thead><tr><th>编号</th><th>部门名称</th><th>部门编号</th><th>操作</th></tr></thead><tbody><tr th:each="department,start:${pageInfo.list}"><td th:text="${start.count}">1</td><td th:text="${department.name}">保安部</td><td th:text="${department.sn}">BA</td><td><a href="#" class="btn btn-info btn-xs btn-input"><span class="glyphicon glyphicon-pencil"></span> 编辑</a><a class="btn btn-danger btn-xs btn-delete"><span class="glyphicon glyphicon-trash"></span> 删除</a></td></tr></tbody></table><div th:replace="common/fragment :: page"></div></div></div></section></div><div th:replace="common/fragment :: footer"></div>
</div>
<div class="modal fade" id="departmentModal" tabindex="-1" role="dialog"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">部门编辑</h4></div><!--单击保存时提交表单--><form action="/department/saveOrUpdate" method="post"><input type="hidden" name="id"><div class="modal-body"><div class="form-group"><label for="name">名称</label><input type="text" class="form-control" name="name" id="name" placeholder="名称"></div><div class="form-group"><label for="sn">缩写</label><input type="text" class="form-control" name="sn" id="sn" placeholder="缩写"></div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">保存</button><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div></form></div></div>
</div>
<!--单击添加时打开模态框-->
<script>$('.btn-input').click(function () {// 测试配置是否成功// alert(1)// 打开模态框$('#departmentModal').modal('show');// 修改模态框名字(头部)$('.modal-title').html("部门新增")})
</script>
</body>
</html>

② controller

@Controller
@RequestMapping("/department")
public class DepartmentController {@Autowiredprivate IDepartmentService departmentService;// 处理部门查询所有方法@RequestMapping("/list")public String list(Model model, QueryObject qo){PageInfo<Department> pageInfo = departmentService.query(qo);model.addAttribute("pageInfo",pageInfo);return "department/list";}// 处理部门删除方法@RequestMapping("/delete")public String delete(Long id){if (id != null) {departmentService.delete(id);}return "redirect:/department/list"; // 再次发起请求 到我们上面的查询所有的控制器方法。}// 进入部门新增/编辑页面方法@RequestMapping("/input")public String input(Long id,Model model){if (id != null) {// 修改Department department = departmentService.get(id);model.addAttribute("department",department);}return "department/input";   // WEB-INF/views/    department/input     .jsp}// 部门新增方法@RequestMapping("/saveOrUpdate")public String saveOrUpdate(Department department){if(department.getId() == null){departmentService.save(department);} else {departmentService.update(department);}return "redirect:/department/list"; // 再次发起请求 到我们上面的查询所有的控制器方法。}
}

③ service接口

public interface IDepartmentService {void save(Department department);void delete(Long id);void update(Department department);Department get(Long id);List<Department> listAll();// 查询分页方法PageInfo<Department> query(QueryObject qo);
}

④ service实现类

@Service
// @Slf4j
public class DepartmentServiceImpl implements IDepartmentService {@Autowiredprivate DepartmentMapper departmentMapper;public void setDepartmentMapper(DepartmentMapper departmentMapper) {this.departmentMapper = departmentMapper;}@Overridepublic void save(Department department) {/* if(department.getName() == null){throw new RuntimeException("非法参数");}*/departmentMapper.insert(department);}@Overridepublic void delete(Long id) {departmentMapper.deleteByPrimaryKey(id);}@Overridepublic void update(Department department) {departmentMapper.updateByPrimaryKey(department);}@Overridepublic Department get(Long id) {return departmentMapper.selectByPrimaryKey(id);}@Overridepublic List<Department> listAll() {return departmentMapper.selectAll();}@Overridepublic PageInfo<Department> query(QueryObject qo) {// 用的技术是线程变量(数据共享 controller service mapper都可以用(一条线程))// 告诉PageHelper当前页 每页显示条数后 PageHelper就会将这两个数据存入线程局部变量中// 传当前页 每页显示条数PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());return new PageInfo<>(departmentMapper.selectForList(qo));}
}

⑤ mapper接口

public interface DepartmentMapper {int deleteByPrimaryKey(Long id);int insert(Department record);Department selectByPrimaryKey(Long id);List<Department> selectAll();int updateByPrimaryKey(Department record);List<Department> selectForList(QueryObject qo);
}

⑥ mapper 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.tj.mapper.DepartmentMapper" ><resultMap id="BaseResultMap" type="cn.tj.domain.Department" ><id column="id" property="id" /><result column="name" property="name" /><result column="sn" property="sn" /></resultMap><delete id="deleteByPrimaryKey" >delete from departmentwhere id = #{id}</delete><insert id="insert" useGeneratedKeys="true" keyProperty="id" >insert into department (name, sn)values (#{name}, #{sn})</insert><update id="updateByPrimaryKey" >update departmentset name = #{name},sn = #{sn}where id = #{id}</update><select id="selectByPrimaryKey" resultMap="BaseResultMap" >select id, name, snfrom departmentwhere id = #{id}</select><select id="selectAll" resultMap="BaseResultMap" >select id, name, snfrom department</select><select id="selectForList" resultMap="BaseResultMap">select id, name, snfrom department</select>
</mapper>

4 部门修改

  为完成数据回显而不查询数据库(查询数据库会造成性能消耗),我们需要在数据库添加新的字段,将每个对象的所有属性封装到一个字段中,通过get方法获取该字段就能拿到对应对象的所有属性。

  后端通过map对象将属性以kv键值对的形式存在字段中,用ObjectMapper().writeValueAsString()方法转为json格式传递给前端,前端将数据存入编辑按钮中的自定义属性data-json中,将遍历的每一个对象属性存入对应的自定义对象中,点击编辑时提出自定义对象中的数据,通过点击按钮时有无属性值可判断出是新增还是编辑

  由于共用同一个模态框,单击编辑后,再点新增会发现回显的数据仍在,因此在每次点击新增或编辑时应清空之前的数据。
① 修改实体类

@Data
public class Department {private Long id;private String name;private String sn;// 方法名与前端调用属性的名称要一致 且首字母要大写(属性规范)// 对应<td th:text="${department.xxx}">test</td>public String getJson() throws JsonProcessingException {// Json特点为kv键值对,对应Java中的mapMap<String,Object> param=new HashMap<>();// 将遍历取到的值存入map中param.put("id",id);param.put("name",name);param.put("sn",sn);// map是java的,此处需转json传递给前端(通过ObjectMapper的方法),异常抛出return new ObjectMapper().writeValueAsString(param);}
}

② 修改前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="utf-8"><title>部门管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
<!--引用外部代码片段 common目录的fragment文件中的link-->
<div th:replace="common/fragment :: link"></div>
<div class="wrapper"><!--navbar 导航--><div th:replace="common/fragment :: navbar"></div><!--menu 菜单--><div th:replace="common/fragment :: menu"></div><div class="content-wrapper"><section class="content-header"><h1>部门管理</h1></section><section class="content"><div class="box"><!--高级查询---><form class="form-inline" id="searchForm" action="/department/list" method="post"><input type="hidden" name="currentPage" id="currentPage" value="1"><a href="#" class="btn btn-success btn-input" style="margin: 10px"><span class="glyphicon glyphicon-plus"></span> 添加</a></form><!--编写内容--><div class="box-body table-responsive "><table class="table table-hover table-bordered table-striped"><thead><tr><th>编号</th><th>部门名称</th><th>部门编号</th><th>操作</th></tr></thead><tbody><tr th:each="department,start:${pageInfo.list}"><td th:text="${start.count}">1</td><td th:text="${department.name}">保安部</td><td th:text="${department.sn}">BA</td><td><!--添加字段存储对象属性--><a href="#" class="btn btn-info btn-xs btn-input" th:data-json="${department.json}"><span class="glyphicon glyphicon-pencil"></span> 编辑</a><a class="btn btn-danger btn-xs btn-delete"><span class="glyphicon glyphicon-trash"></span> 删除</a></td></tr></tbody></table><div th:replace="common/fragment :: page"></div></div></div></section></div><div th:replace="common/fragment :: footer"></div>
</div>
<div class="modal fade" id="departmentModal" tabindex="-1" role="dialog"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">部门编辑</h4></div><!--单击保存时提交表单--><form action="/department/saveOrUpdate" method="post" id="departmentForm"><input type="hidden" name="id" id="id"><div class="modal-body"><div class="form-group"><label for="name">名称</label><input type="text" class="form-control" name="name" id="name" placeholder="名称"></div><div class="form-group"><label for="sn">缩写</label><input type="text" class="form-control" name="sn" id="sn" placeholder="缩写"></div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">保存</button><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div></form></div></div>
</div>
<!--单击添加时打开模态框-->
<script>$('.btn-input').click(function () {// 每次点击新增或编辑时应清空之前的数据$('#departmentForm input').val('');// 获取编辑按钮上的 data-json 的属性值(this表示事件源(可区分是新增还是编辑))var data = $(this).data('json');if(data){// 有值 编辑$('.modal-title').html("部门编辑")// 通过id将值回显到对应位置上$('#id').val(data.id);$('#name').val(data.name);$('#sn').val(data.sn);}else{// 无值 新增$('.modal-title').html("部门新增")}// 打开模态框$('#departmentModal').modal('show');})
</script>
</body>
</html>

5 部门删除

  删除分为硬删除(sql语句为delete,直接将数据从数据库中删除)与软删除(sql语句为update,表中含status字段,通过字段的值控制数据的展示,如默认值为1表示有效,改为0表示无效,查询时where条件增加status为1即可),用户确认删除前,应给与一定的提示,防止用户错误删除,这里可以使用SweetAlert2进行美化。由于很多页面都会用到删除功能,所以建议抽取出来。(分页也是)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="utf-8"><title>部门管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
<!--引用外部代码片段 common目录的fragment文件中的link-->
<div th:replace="common/fragment :: link"></div>
<div class="wrapper"><!--navbar 导航--><div th:replace="common/fragment :: navbar"></div><!--menu 菜单--><div th:replace="common/fragment :: menu"></div><div class="content-wrapper"><section class="content-header"><h1>部门管理</h1></section><section class="content"><div class="box"><!--高级查询---><form class="form-inline" id="searchForm" action="/department/list" method="post"><input type="hidden" name="currentPage" id="currentPage" value="1"><a href="#" class="btn btn-success btn-input" style="margin: 10px"><span class="glyphicon glyphicon-plus"></span> 添加</a></form><!--编写内容--><div class="box-body table-responsive "><table class="table table-hover table-bordered table-striped"><thead><tr><th>编号</th><th>部门名称</th><th>部门编号</th><th>操作</th></tr></thead><tbody><tr th:each="department,start:${pageInfo.list}"><td th:text="${start.count}">1</td><td th:text="${department.name}">保安部</td><td th:text="${department.sn}">BA</td><td><!--添加字段存储对象属性--><a href="#" class="btn btn-info btn-xs btn-input" th:data-json="${department.json}"><span class="glyphicon glyphicon-pencil"></span> 编辑</a><!--有字符串拼接所以使用||--><a class="btn btn-danger btn-xs btn-delete" th:data-url="|/department/delete?id=${department.id}|"><span class="glyphicon glyphicon-trash"></span> 删除</a></td></tr></tbody></table><div th:replace="common/fragment :: page"></div></div></div></section></div><div th:replace="common/fragment :: footer"></div>
</div>
<div class="modal fade" id="departmentModal" tabindex="-1" role="dialog"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">部门编辑</h4></div><!--单击保存时提交表单--><form action="/department/saveOrUpdate" method="post" id="departmentForm"><input type="hidden" name="id" id="id"><div class="modal-body"><div class="form-group"><label for="name">名称</label><input type="text" class="form-control" name="name" id="name" placeholder="名称"></div><div class="form-group"><label for="sn">缩写</label><input type="text" class="form-control" name="sn" id="sn" placeholder="缩写"></div></div><div class="modal-footer"><button type="submit" class="btn btn-primary">保存</button><button type="button" class="btn btn-default" data-dismiss="modal">关闭</button></div></form></div></div>
</div>
<!--单击添加时打开模态框-->
<script>//删除$('.btn-delete').click(function (){// 获取传递的urllet url=$(this).data('url')Swal.fire({title: '确认删除吗?',text: "此操作不可逆!",icon: 'warning',// 是否开启第二个按钮showCancelButton: true,// 确认按钮颜色confirmButtonColor: '#3085d6',// 取消按钮颜色cancelButtonColor: '#d33',confirmButtonText: '确定',cancelButtonText: '取消'}).then((result) => {//result是点击操作所传递的值true或falseif(result.value) {// 删除操作/department/delete?id=idlocation.href=url}});})// 新增或编辑$('.btn-input').click(function () {// 每次点击新增或编辑时应清空之前的数据$('#departmentForm input').val('');// 获取编辑按钮上的 data-json 的属性值(this表示事件源(可区分是新增还是编辑))var data = $(this).data('json');if(data){// 有值 编辑$('.modal-title').html("部门编辑")// 通过id将值回显到对应位置上$('#id').val(data.id);$('#name').val(data.name);$('#sn').val(data.sn);}else{// 无值 新增$('.modal-title').html("部门新增")}// 打开模态框$('#departmentModal').modal('show');})
</script>
</body>
</html>

这篇关于Java研学-RBAC权限控制(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/899977

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注