只讲解关键部分,详细看源码,文章下方捐赠或QQ联系捐赠获取。
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_dept")
public class SysDept implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 上级部门ID
*/
private Integer pid;
/**
* 所有上级部门ID, 0为根,格式:0,1,2,
*/
private String pids;
/**
* 部门名称
*/
private String deptName;
/**
* 部门编码
*/
private String deptCode;
@TableField(exist=false)
private String pname;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 创建人
*/
private String createUser;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
/**
* 修改人
*/
private String updateUser;
}
@Controller
@RequestMapping("/sysDept/")
public class SysDeptController {
@Autowired
private SysDeptService sysDeptService;
@GetMapping("listUI")
public String listUI() {
return "dept/list";
}
@RequestMapping("form")
public String form(Map<String,Object> map) {
return "dept/form";
}
@GetMapping("toSelectTree")
public String toSelectTree() {
return "dept/selectTree";
}
@PostMapping("list")
@ResponseBody
public Result<IPage<SysDept>> list(@RequestParam(defaultValue = "1") Integer pageNo,
@RequestParam(defaultValue = "10") Integer pageSize) {
// 构造分页查询条件
QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
Page<SysDept> page = new Page<>(pageNo,pageSize);
IPage<SysDept> result = sysDeptService.selectDeptList(page, null);
// 设置总记录数
result.setTotal(sysDeptService.count(queryWrapper));
sysDeptService.count(queryWrapper);
return ResultUtil.ok(result);
}
@GetMapping("listTree")
@ResponseBody
public Object listTree() {
// User user = getUserEntity();
// 构建查询条件,从根查找
QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
// queryWrapper.like("pids", user.getDept().getPids()+user.getDeptId()).or().eq("id",user.getDeptId());
queryWrapper.like("pids", 0);
List<SysDept> list = sysDeptService.list(queryWrapper);
return list;
}
@OperLog(operModule = "部门管理",operType = "修改",operDesc = "修改部门")
@PostMapping("save")
@ResponseBody
public Result<String> add(@RequestBody SysDept dept){
// 新增
if(dept.getId()==null){
dept.setCreateTime(new Date());
dept.setCreateUser("TODO");
}
// 设置上级部门ID
if(dept.getPid()!=0){
// 获取上级部门的ids然后拼接上级部门id
SysDept parentDept = sysDeptService.getById(dept.getPid());
dept.setPids(parentDept.getPids()+dept.getPid()+",");
}
if(!sysDeptService.saveOrUpdate(dept)){
return ResultUtil.fail("添加失败");
}
return ResultUtil.ok("添加成功");
}
@OperLog(operModule = "部门管理",operType = "删除",operDesc = "删除部门")
@PostMapping("/remove")
@ResponseBody
public Result<String> remove(@RequestParam Integer id){
sysDeptService.removeById(id);
return ResultUtil.ok("删除成功!");
}
@RequestMapping(value="{id}/select",method= RequestMethod.GET)
public String select(Map<String,Object> map,@PathVariable Integer id) {
SysDept sysDept = sysDeptService.getById(id);
map.put("record",sysDept);
return "sysDept/edit";
}
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="utf-8">
<title>部门列表title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" th:href="@{/static/plugin/layui/css/layui.css}" media="all">
head>
<body>
<table class="layui-hide" id="SysDept" lay-filter="SysDept">table>
<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">
<div class="layui-row" id="editForm" style="display:none;">
<div class="layui-col-md10">
<form class="layui-form layui-from-pane" action="" style="margin-top:20px">
<input type="text" id="id" name="id" hidden="hidden">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">上级部门label>
<div class="layui-input-inline">
<input type="text" id="pname" name="pname" required lay-verify="required" autocomplete="off"
placeholder="" class="layui-input">
div>
<button class="layui-btn layui-btn-sm" id="chooseDeptBtn" type="button">
<i class="layui-icon">i>
button>
<input type="hidden" id="pid" name="pid"/>
div>
<div class="layui-inline">
<label class="layui-form-label">部门名称label>
<div class="layui-input-inline">
<input type="text" id="deptName" name="deptName" required lay-verify="required" autocomplete="off"
placeholder="" class="layui-input">
div>
div>
<div class="layui-inline">
<label class="layui-form-label">部门编码label>
<div class="layui-input-inline">
<input type="text" id="deptCode" name="deptCode" required lay-verify="required" autocomplete="off"
placeholder="" class="layui-input">
div>
div>
div>
<div class="layui-form-item" style="margin-top:40px">
<div class="layui-input-block">
<button class="layui-btn layui-btn-submit " lay-submit="" lay-filter="confirm">确认button>
<button type="button" class="layui-btn layui-btn-primary" id="back">关闭button>
div>
div>
form>
div>
div>
<script type="text/html" id="deptToolBar">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="dept:add">新增</button>
</div>
script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="dept:edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="dept:remove">删除</a>
script>
打赏>1元留言数字3,评论区留下邮箱发送源码 :)