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实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2