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 Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避