SpringBoot +LayUI+thymeleaf 实现一个功能(三) 新增

tech2024-01-29  74

前端 dept.html 后端 dao层

package com.flo.dao; import com.flo.po.Dept; import org.springframework.stereotype.Component; import java.util.List; @Component("deptDao") public interface IDeptDao { /** * 查询部门信息 */ List<Dept> findAll(); void addDept(Dept dept); }

mapper(新增部分)

<insert id="addDept" parameterType="com.flo.po.Dept"> insert into dept(parent_id,name,type,sort,status,create_time) values (#{parentId},#{name},#{type},#{sort},#{status},#{createTime}) </insert>

Service 接口

void addDept(Dept dept);

接口实现层

@Override public void addDept(Dept dept) { iDeptDao.addDept(dept); }

封装后端类

网络状态常量类

package com.flo.util; /** * 常量类 */ public class Constants { public final static int OK_CODE = 200; public final static int FAIL_CODE = 400; public final static int OTHER_FAIL_CODE = 333; // 其它错误 public final static String OK_MSG = "请求成功"; public final static String FAIL_MSG = "请求失败"; public final static int STATUS_0 = 0; // 可用状态 public final static int STATUS_1 = 1; // 禁用状态 public final static String CACHE_NAME = "KACache"; }

消息

package com.flo.util; import java.io.Serializable; import java.util.List; /** * 返回前端 数据封闭类 */ public class R implements Serializable { private static final long serialVersionUID = 1L; private Integer code; private String msg; private Object data; private Long count; // 分页信息:总条数 public R() { } private R(int code, String msg, Object data) { this.code = code; this.msg = msg; this.data=data; if(data!=null){ List list= (List) data; this.count=(long)list.size(); } } public static com.flo.util.R ok() { return new com.flo.util.R(Constants.OK_CODE, Constants.OK_MSG, null); } public static com.flo.util.R ok(Object data) { return new com.flo.util.R(Constants.OK_CODE, Constants.OK_MSG, data); } public static com.flo.util.R ok(String msg, Object data) { return new com.flo.util.R(Constants.OK_CODE, msg, data); } public static com.flo.util.R fail(String msg) { return new com.flo.util.R(Constants.FAIL_CODE, msg, null); } public static com.flo.util.R fail(int errorCode, String msg) { return new com.flo.util.R(errorCode, msg, null); } public int getCode() { return code; } public String getMsg() { return msg; } public Object getData() { return data; } public Long getTotal() { return count; } public com.flo.util.R setTotal(Long total) { this.count = total; return this; } }

树类

package com.flo.util; import com.alibaba.fastjson.JSON; import com.flo.po.Node; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component("treeBuilder") public class TreeBuilder { List<Node> nodes = new ArrayList<>(); public String buildTree(List<Node> nodes) { com.flo.util.TreeBuilder treeBuilder = new com.flo.util.TreeBuilder(nodes); return treeBuilder.buildJSONTree(); } public TreeBuilder() {} public TreeBuilder(List<Node> nodes) { super(); this.nodes = nodes; } // 构建JSON树形结构 public String buildJSONTree() { List<Node> nodeTree = buildTree(); String jsonStr = JSON.toJSONString(nodeTree); return jsonStr; } // 构建树形结构 public List<Node> buildTree() { List<Node> treeNodes = new ArrayList<>(); List<Node> rootNodes = getRootNodes(); for (Node rootNode : rootNodes) { buildChildNodes(rootNode); treeNodes.add(rootNode); } return treeNodes; } // 递归子节点 public void buildChildNodes(Node node) { List<Node> children = getChildNodes(node); if (!children.isEmpty()) { for (Node child : children) { buildChildNodes(child); } node.setChildren(children); } } // 获取父节点下所有的子节点 public List<Node> getChildNodes(Node pnode) { List<Node> childNodes = new ArrayList<>(); for (Node n : nodes) { if (pnode.getId().equals(n.getParentId())) { childNodes.add(n); } } return childNodes; } // 判断是否为根节点 public boolean rootNode(Node node) { boolean isRootNode = true; for (Node n : nodes) { if (node.getParentId().equals(n.getId())) { isRootNode = false; break; } } return isRootNode; } // 获取集合中所有的根节点 public List<Node> getRootNodes() { List<Node> rootNodes = new ArrayList<>(); for (Node n : nodes) { if (rootNode(n)) { rootNodes.add(n); } } return rootNodes; } }

控制层

//跳转接口 @RequestMapping("addDept") public String addDept(Integer type, Integer parentId, Model model){ model.addAttribute("type",type+1); model.addAttribute("parentId",parentId); return "pages/addDept"; } //添加接口 @RequestMapping("dept/addDeptSubmit") @ResponseBody public R addDept(Dept dept){ dept.setCreateTime(new Date()); deptService.addDept(dept); return R.ok(); }

前端添加页面

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>添加部门</title> <link rel="stylesheet" th:href="@{lib/layui-v2.5.5/css/layui.css}" media="all"> <link rel="stylesheet" th:href="@{css/public.css}" media="all"> <style> body { background-color: #ffffff; } </style> </head> <body> <div class="layui-form layuimini-form"> <input type="hidden" name="parentId" th:value="${parentId}"> <div class="layui-form-item"> <label class="layui-form-label required">用户名</label> <div class="layui-input-block"> <input type="text" name="name" lay-verify="required" lay-reqtext="部门名不能为空" placeholder="请输入部门名称" value="" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label ">排序号</label> <div class="layui-input-block"> <input type="number" name="sort" placeholder="请输入排序号" value="" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">状态</label> <div class="layui-input-block"> <input type="checkbox" name="status" lay-skin="switch" lay-text="ON|OFF"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">类型</label> <div class="layui-input-block" th:if="${type eq 1}"> <input type="radio" name="type" value="1" title="部门" > </div> <div class="layui-input-block" th:if="${type eq 2}"> <input type="radio" name="type" value="2" title="科室/小组" > </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button> </div> </div> </div> <script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script> <script> layui.use(['form'], function () { var form = layui.form, layer = layui.layer, $ = layui.$; //监听提交 form.on('submit(saveBtn)', function (data) { console.log(data.field); var datas =data.field; if(datas.status=='on'){ datas.status=0;//正常状态 }else{ datas.status=1; } //向后台发送数据并进行添加操作 $.ajax({ url:"dept/addDeptSubmit", type:"POST", data:datas, success:function(result){ //把json对象转string // result=JSON.parse(result); if(result.code==200){ layer.msg("添加成功",{ icon:6, time:500 },function(){ parent.window.location.reload();//重新页面 var iframeIndex = parent.layer.getFrameIndex(window.name); parent.layer.close(iframeIndex); }); }else{ layer.msg("添加失败"); } } }) return false; }); }); </script> </body> </html>

测试 后记

最新回复(0)