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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We