上一章我们完成了角色管理模块的功能,这一章我们要做的是部门管理栏目,部门管理栏目核心的操作也是基于CRUD的,所以我们套用用户管理和角色管理就能实现部门管理栏目的功能。
在TbDeptDao.xml
文件中,定义SQL用于查询部门分页数据。看上面的截图可知,在部门管理页面上,只有按照部门名字模糊查询。所以在SQL语句中,WHERE子句里面只有一个查询条件。由于在页面表格中要显示每个部门拥有的员工数量,所以用了COUNT()
汇总函数。
<select id="searchDeptByPage" parameterType="HashMap" resultType="HashMap"> SELECT d.id, d.dept_name AS deptName, d.tel, d.email, d.desc, COUNT(u.id) AS emps FROM tb_dept d LEFT JOIN tb_user u ON u.dept_id=d.id AND u.status=1 WHERE 1=1 <if test="deptName!=null"> AND d.dept_name LIKE '%${deptName}%' </if> GROUP BY d.id LIMIT #{start}, #{length} </select> <select id="searchDeptCount" parameterType="HashMap" resultType="long"> SELECT COUNT(*) FROM ( SELECT d.id FROM tb_dept d LEFT JOIN tb_user u ON u.dept_id=d.id AND u.status=1 WHERE 1=1 <if test="deptName!=null"> AND d.dept_name LIKE '%${deptName}%' </if> GROUP BY d.id ) AS temp </select>
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
在TbDeptDao.java
接口中,声明DAO方法。
public interface TbDeptDao { …… public ArrayList<HashMap> searchDeptByPage(HashMap param); public long searchDeptCount(HashMap param); }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
在DeptService.java
接口中,声明抽象方法。
public interface DeptService { …… public PageUtils searchDeptByPage(HashMap param); }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
在DeptServiceImpl.java
类中,实现抽象方法。
public class DeptServiceImpl implements DeptService { …… @Override public PageUtils searchDeptByPage(HashMap param) { ArrayList<HashMap> list = deptDao.searchDeptByPage(param); long count = deptDao.searchDeptCount(param); int start = (Integer) param.get("start"); int length = (Integer) param.get("length"); PageUtils pageUtils = new PageUtils(list, count, start, length); return pageUtils; } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
创建SearchDeptByPageForm.java
类,保存Ajax提交的数据。
@Data @Schema(description = "查询部门分页表单") public class SearchDeptByPageForm { @Pattern(regexp = "^[0-9a-zA-Z\\u4e00-\\u9fa5]{1,10}$", message = "deptName内容不正确") @Schema(description = "部门名称") private String deptName; @NotNull(message = "page不能为空") @Min(value = 1, message = "page不能小于1") @Schema(description = "页数") private Integer page; @NotNull(message = "length不能为空") @Range(min = 10, max = 50, message = "length必须为10~50之间") @Schema(description = "每页记录数") private Integer length; }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
在DeptController.java
类中,定义Web方法,然后大家就可以在Swagger页面测试Web方法了。
public class DeptController { …… @PostMapping("/searchDeptByPage") @Operation(summary = "查询部门分页数据") @SaCheckPermission(value = {"ROOT", "DEPT:SELECT"}, mode = SaMode.OR) public R searchDeptByPage(@Valid @RequestBody SearchDeptByPageForm form) { int page = form.getPage(); int length = form.getLength(); int start = (page - 1) * length; HashMap param = JSONUtil.parse(form).toBean(HashMap.class); param.put("start", start); PageUtils pageUtils = deptService.searchDeptByPage(param); return R.ok().put("page", pageUtils); } }
代码块预览 复制
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15