EasyUI树形菜单的使用
如图,是我制作淘淘商城时使用EasyUI制作的树形菜单,可添加和删除叶子节点。
Easyui tree的使用教程可查看官网:
http://www.jeasyui.net/tutorial/52.html
这里我主要是结合后端的形式,来实现EasyUi 树形菜单的CRUD操作。
页面代码: content-catrgory.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <div> <ul id="contentCategory" class="easyui-tree"> </ul> </div> <div id="contentCategoryMenu" class="easyui-menu" style="width:120px;" data-options="onClick:menuHandler"> <div data-options="iconCls:'icon-add',name:'add'">添加</div> <div data-options="iconCls:'icon-remove',name:'rename'">重命名</div> <div class="menu-sep"></div> <div data-options="iconCls:'icon-remove',name:'delete'">删除</div> </div> <script type="text/javascript"> //文档加载后处理以下的逻辑 $(function(){ //在#contentCategory 所在的标签中创建一颗树 $("#contentCategory").tree({ url : '/content/category/list', animate: true, method : "GET", //右击鼠标触发 onContextMenu: function(e,node){ //关闭原来的鼠标的默认事件 e.preventDefault(); //选中 右击鼠标的节点 $(this).tree('select',node.target); //展示菜单栏 $('#contentCategoryMenu').menu('show',{ left: e.pageX,//在鼠标的位置显示 top: e.pageY//在鼠标的位置显示 }); }, //在选中的节点被编辑之后触发 onAfterEdit : function(node){ //获取树本身 var _tree = $(this); //表示的是新增的节点 新增 if(node.id == 0){ // 新增节点 //parentId:node.parentId,name:node.text //parentId:就是新增节点的父节点的Id //name:新增节点的文本 $.post("/content/category/create",{parentId:node.parentId,name:node.text},function(data){ if(data.status == 200){ //更新节点 _tree.tree("update",{ target : node.target,//更新哪一个节点 id : data.data.id//更新新增节点的id }); }else{ $.messager.alert('提示','创建'+node.text+' 分类失败!'); } }); }else{ $.post("/content/category/update",{id:node.id,name:node.text}); } } }); }); //处理点击菜单的事件 function menuHandler(item){ //获取树 var tree = $("#contentCategory"); //获取被选中的节点 就是右击鼠标时的所在的节点 var node = tree.tree("getSelected"); //判断选择的是添加还是重命名还是删除 // == 1==1 true 1=="1" true; // === 1===1 true 1==="1" false //点击“添加” if(item.name === "add"){ //在被点击的节点下追加一个子节点 tree.tree('append', { parent: (node?node.target:null), //被添加的子节点的父 // data: [{ text: '新建分类123',//节点的内容 id : 0,//节点的id parentId : node.id//新建的节点的父节点的id }] }); //找到id为0的节点 添加的节点 var _node = tree.tree('find',0); //选中id为0的节点 添加的节点 开启编辑 tree.tree("select",_node.target).tree('beginEdit',_node.target); }else if(item.name === "rename"){ tree.tree('beginEdit',node.target); }else if(item.name === "delete"){ $.messager.confirm('确认','确定删除名为 '+node.text+' 的分类吗?',function(r){ if(r){//如果是true 表示要执行以下的逻辑 $.post("/content/category/delete/",{id:node.id},function(){ //后台删除成功后,删除前端的节点 tree.tree("remove",node.target); }); } }); } } </script>
数据库表设计:
数据表中的数据:
注意字段对应关系。首先根节点从0开始。
根据EasyUI树形菜单的json格式定义节点返回格式
easyUI树形菜单节点:EasyUITreeNode
package com.taotao.common.pojo; import java.io.Serializable; /** * * @ClassName: EasyUITreeNode * @Description:TODO * @author: jp * @date: 2019年5月15日 下午3:42:21 * * @Copyright: 2019 www.tydic.com Inc. All rights reserved. * */ public class EasyUITreeNode implements Serializable{ private long id; //父类目ID=0时,代表的是一级的类目 private String text; //分类名称 private String state; //节点状态。可选值:1(正常),2(删除) public long getId() { return id; } public void setId(long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
自定义响应结构:TaotaoResult
package com.taotao.common.pojo; import java.io.Serializable; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * 自定义响应结构 */ public class TaotaoResult implements Serializable{ // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); // 响应业务状态 private Integer status; // 响应消息 private String msg; // 响应中的数据 private Object data; public static TaotaoResult build(Integer status, String msg, Object data) { return new TaotaoResult(status, msg, data); } public static TaotaoResult ok(Object data) { return new TaotaoResult(data); } public static TaotaoResult ok() { return new TaotaoResult(null); } public TaotaoResult() { } public static TaotaoResult build(Integer status, String msg) { return new TaotaoResult(status, msg, null); } public TaotaoResult(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } public TaotaoResult(Object data) { this.status = 200; this.msg = "OK"; this.data = data; } // public Boolean isOK() { // return this.status == 200; // } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } /** * 将json结果集转化为TaotaoResult对象 * * @param jsonData json数据 * @param clazz TaotaoResult中的object类型 * @return */ public static TaotaoResult formatToPojo(String jsonData, Class<?> clazz) { try { if (clazz == null) { return MAPPER.readValue(jsonData, TaotaoResult.class); } JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (clazz != null) { if (data.isObject()) { obj = MAPPER.readValue(data.traverse(), clazz); } else if (data.isTextual()) { obj = MAPPER.readValue(data.asText(), clazz); } } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } /** * 没有object对象的转化 * * @param json * @return */ public static TaotaoResult format(String json) { try { return MAPPER.readValue(json, TaotaoResult.class); } catch (Exception e) { e.printStackTrace(); } return null; } /** * Object是集合转化 * * @param jsonData json数据 * @param clazz 集合中的类型 * @return */ public static TaotaoResult formatToList(String jsonData, Class<?> clazz) { try { JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (data.isArray() && data.size() > 0) { obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } }
json工具类:JsonUtils
package com.taotao.common.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * 淘淘商城自定义响应结构 */ public class JsonUtils { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */ public static String objectToJson(Object data) { try { String string = MAPPER.writeValueAsString(data); return string; } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsonData json数据 * @param clazz 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } }
pojo:
TbContentCategory.java
package com.taotao.pojo; import java.io.Serializable; import java.util.Date; public class TbContentCategory implements Serializable { private Long id; //类目ID private Long parentId;//父类目ID=0时,代表的是一级的类目 private String name;//分类名称 private Integer status;//状态。可选值:1(正常),2(删除) private Integer sortOrder; //排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数 private Boolean isParent;//该类目是否为父类目,1为true,0为false private Date created; //创建时间 private Date updated; //修改时间 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public Integer getSortOrder() { return sortOrder; } public void setSortOrder(Integer sortOrder) { this.sortOrder = sortOrder; } public Boolean getIsParent() { return isParent; } public void setIsParent(Boolean isParent) { this.isParent = isParent; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } }
TbContentCategoryExample.java
package com.taotao.pojo; import java.util.ArrayList; import java.util.Date; import java.util.List; public class TbContentCategoryExample { protected String orderByClause; protected boolean distinct; protected List<Criteria> oredCriteria; public TbContentCategoryExample() { oredCriteria = new ArrayList<Criteria>(); } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public String getOrderByClause() { return orderByClause; } public void setDistinct(boolean distinct) { this.distinct = distinct; } public boolean isDistinct() { return distinct; } public List<Criteria> getOredCriteria() { return oredCriteria; } public void or(Criteria criteria) { oredCriteria.add(criteria); } public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; } protected Criteria createCriteriaInternal() { Criteria criteria = new Criteria(); return criteria; } public void clear() { oredCriteria.clear(); orderByClause = null; distinct = false; } protected abstract static class GeneratedCriteria { protected List<Criterion> criteria; protected GeneratedCriteria() { super(); criteria = new ArrayList<Criterion>(); } public boolean isValid() { return criteria.size() > 0; } public List<Criterion> getAllCriteria() { return criteria; } public List<Criterion> getCriteria() { return criteria; } protected void addCriterion(String condition) { if (condition == null) { throw new RuntimeException("Value for condition cannot be null"); } criteria.add(new Criterion(condition)); } protected void addCriterion(String condition, Object value, String property) { if (value == null) { throw new RuntimeException("Value for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value)); } protected void addCriterion(String condition, Object value1, Object value2, String property) { if (value1 == null || value2 == null) { throw new RuntimeException("Between values for " + property + " cannot be null"); } criteria.add(new Criterion(condition, value1, value2)); } public Criteria andIdIsNull() { addCriterion("id is null"); return (Criteria) this; } public Criteria andIdIsNotNull() { addCriterion("id is not null"); return (Criteria) this; } public Criteria andIdEqualTo(Long value) { addCriterion("id =", value, "id"); return (Criteria) this; } public Criteria andIdNotEqualTo(Long value) { addCriterion("id <>", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThan(Long value) { addCriterion("id >", value, "id"); return (Criteria) this; } public Criteria andIdGreaterThanOrEqualTo(Long value) { addCriterion("id >=", value, "id"); return (Criteria) this; } public Criteria andIdLessThan(Long value) { addCriterion("id <", value, "id"); return (Criteria) this; } public Criteria andIdLessThanOrEqualTo(Long value) { addCriterion("id <=", value, "id"); return (Criteria) this; } public Criteria andIdIn(List<Long> values) { addCriterion("id in", values, "id"); return (Criteria) this; } public Criteria andIdNotIn(List<Long> values) { addCriterion("id not in", values, "id"); return (Criteria) this; } public Criteria andIdBetween(Long value1, Long value2) { addCriterion("id between", value1, value2, "id"); return (Criteria) this; } public Criteria andIdNotBetween(Long value1, Long value2) { addCriterion("id not between", value1, value2, "id"); return (Criteria) this; } public Criteria andParentIdIsNull() { addCriterion("parent_id is null"); return (Criteria) this; } public Criteria andParentIdIsNotNull() { addCriterion("parent_id is not null"); return (Criteria) this; } public Criteria andParentIdEqualTo(Long value) { addCriterion("parent_id =", value, "parentId"); return (Criteria) this; } public Criteria andParentIdNotEqualTo(Long value) { addCriterion("parent_id <>", value, "parentId"); return (Criteria) this; } public Criteria andParentIdGreaterThan(Long value) { addCriterion("parent_id >", value, "parentId"); return (Criteria) this; } public Criteria andParentIdGreaterThanOrEqualTo(Long value) { addCriterion("parent_id >=", value, "parentId"); return (Criteria) this; } public Criteria andParentIdLessThan(Long value) { addCriterion("parent_id <", value, "parentId"); return (Criteria) this; } public Criteria andParentIdLessThanOrEqualTo(Long value) { addCriterion("parent_id <=", value, "parentId"); return (Criteria) this; } public Criteria andParentIdIn(List<Long> values) { addCriterion("parent_id in", values, "parentId"); return (Criteria) this; } public Criteria andParentIdNotIn(List<Long> values) { addCriterion("parent_id not in", values, "parentId"); return (Criteria) this; } public Criteria andParentIdBetween(Long value1, Long value2) { addCriterion("parent_id between", value1, value2, "parentId"); return (Criteria) this; } public Criteria andParentIdNotBetween(Long value1, Long value2) { addCriterion("parent_id not between", value1, value2, "parentId"); return (Criteria) this; } public Criteria andNameIsNull() { addCriterion("name is null"); return (Criteria) this; } public Criteria andNameIsNotNull() { addCriterion("name is not null"); return (Criteria) this; } public Criteria andNameEqualTo(String value) { addCriterion("name =", value, "name"); return (Criteria) this; } public Criteria andNameNotEqualTo(String value) { addCriterion("name <>", value, "name"); return (Criteria) this; } public Criteria andNameGreaterThan(String value) { addCriterion("name >", value, "name"); return (Criteria) this; } public Criteria andNameGreaterThanOrEqualTo(String value) { addCriterion("name >=", value, "name"); return (Criteria) this; } public Criteria andNameLessThan(String value) { addCriterion("name <", value, "name"); return (Criteria) this; } public Criteria andNameLessThanOrEqualTo(String value) { addCriterion("name <=", value, "name"); return (Criteria) this; } public Criteria andNameLike(String value) { addCriterion("name like", value, "name"); return (Criteria) this; } public Criteria andNameNotLike(String value) { addCriterion("name not like", value, "name"); return (Criteria) this; } public Criteria andNameIn(List<String> values) { addCriterion("name in", values, "name"); return (Criteria) this; } public Criteria andNameNotIn(List<String> values) { addCriterion("name not in", values, "name"); return (Criteria) this; } public Criteria andNameBetween(String value1, String value2) { addCriterion("name between", value1, value2, "name"); return (Criteria) this; } public Criteria andNameNotBetween(String value1, String value2) { addCriterion("name not between", value1, value2, "name"); return (Criteria) this; } public Criteria andStatusIsNull() { addCriterion("status is null"); return (Criteria) this; } public Criteria andStatusIsNotNull() { addCriterion("status is not null"); return (Criteria) this; } public Criteria andStatusEqualTo(Integer value) { addCriterion("status =", value, "status"); return (Criteria) this; } public Criteria andStatusNotEqualTo(Integer value) { addCriterion("status <>", value, "status"); return (Criteria) this; } public Criteria andStatusGreaterThan(Integer value) { addCriterion("status >", value, "status"); return (Criteria) this; } public Criteria andStatusGreaterThanOrEqualTo(Integer value) { addCriterion("status >=", value, "status"); return (Criteria) this; } public Criteria andStatusLessThan(Integer value) { addCriterion("status <", value, "status"); return (Criteria) this; } public Criteria andStatusLessThanOrEqualTo(Integer value) { addCriterion("status <=", value, "status"); return (Criteria) this; } public Criteria andStatusIn(List<Integer> values) { addCriterion("status in", values, "status"); return (Criteria) this; } public Criteria andStatusNotIn(List<Integer> values) { addCriterion("status not in", values, "status"); return (Criteria) this; } public Criteria andStatusBetween(Integer value1, Integer value2) { addCriterion("status between", value1, value2, "status"); return (Criteria) this; } public Criteria andStatusNotBetween(Integer value1, Integer value2) { addCriterion("status not between", value1, value2, "status"); return (Criteria) this; } public Criteria andSortOrderIsNull() { addCriterion("sort_order is null"); return (Criteria) this; } public Criteria andSortOrderIsNotNull() { addCriterion("sort_order is not null"); return (Criteria) this; } public Criteria andSortOrderEqualTo(Integer value) { addCriterion("sort_order =", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderNotEqualTo(Integer value) { addCriterion("sort_order <>", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderGreaterThan(Integer value) { addCriterion("sort_order >", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderGreaterThanOrEqualTo(Integer value) { addCriterion("sort_order >=", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderLessThan(Integer value) { addCriterion("sort_order <", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderLessThanOrEqualTo(Integer value) { addCriterion("sort_order <=", value, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderIn(List<Integer> values) { addCriterion("sort_order in", values, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderNotIn(List<Integer> values) { addCriterion("sort_order not in", values, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderBetween(Integer value1, Integer value2) { addCriterion("sort_order between", value1, value2, "sortOrder"); return (Criteria) this; } public Criteria andSortOrderNotBetween(Integer value1, Integer value2) { addCriterion("sort_order not between", value1, value2, "sortOrder"); return (Criteria) this; } public Criteria andIsParentIsNull() { addCriterion("is_parent is null"); return (Criteria) this; } public Criteria andIsParentIsNotNull() { addCriterion("is_parent is not null"); return (Criteria) this; } public Criteria andIsParentEqualTo(Boolean value) { addCriterion("is_parent =", value, "isParent"); return (Criteria) this; } public Criteria andIsParentNotEqualTo(Boolean value) { addCriterion("is_parent <>", value, "isParent"); return (Criteria) this; } public Criteria andIsParentGreaterThan(Boolean value) { addCriterion("is_parent >", value, "isParent"); return (Criteria) this; } public Criteria andIsParentGreaterThanOrEqualTo(Boolean value) { addCriterion("is_parent >=", value, "isParent"); return (Criteria) this; } public Criteria andIsParentLessThan(Boolean value) { addCriterion("is_parent <", value, "isParent"); return (Criteria) this; } public Criteria andIsParentLessThanOrEqualTo(Boolean value) { addCriterion("is_parent <=", value, "isParent"); return (Criteria) this; } public Criteria andIsParentIn(List<Boolean> values) { addCriterion("is_parent in", values, "isParent"); return (Criteria) this; } public Criteria andIsParentNotIn(List<Boolean> values) { addCriterion("is_parent not in", values, "isParent"); return (Criteria) this; } public Criteria andIsParentBetween(Boolean value1, Boolean value2) { addCriterion("is_parent between", value1, value2, "isParent"); return (Criteria) this; } public Criteria andIsParentNotBetween(Boolean value1, Boolean value2) { addCriterion("is_parent not between", value1, value2, "isParent"); return (Criteria) this; } public Criteria andCreatedIsNull() { addCriterion("created is null"); return (Criteria) this; } public Criteria andCreatedIsNotNull() { addCriterion("created is not null"); return (Criteria) this; } public Criteria andCreatedEqualTo(Date value) { addCriterion("created =", value, "created"); return (Criteria) this; } public Criteria andCreatedNotEqualTo(Date value) { addCriterion("created <>", value, "created"); return (Criteria) this; } public Criteria andCreatedGreaterThan(Date value) { addCriterion("created >", value, "created"); return (Criteria) this; } public Criteria andCreatedGreaterThanOrEqualTo(Date value) { addCriterion("created >=", value, "created"); return (Criteria) this; } public Criteria andCreatedLessThan(Date value) { addCriterion("created <", value, "created"); return (Criteria) this; } public Criteria andCreatedLessThanOrEqualTo(Date value) { addCriterion("created <=", value, "created"); return (Criteria) this; } public Criteria andCreatedIn(List<Date> values) { addCriterion("created in", values, "created"); return (Criteria) this; } public Criteria andCreatedNotIn(List<Date> values) { addCriterion("created not in", values, "created"); return (Criteria) this; } public Criteria andCreatedBetween(Date value1, Date value2) { addCriterion("created between", value1, value2, "created"); return (Criteria) this; } public Criteria andCreatedNotBetween(Date value1, Date value2) { addCriterion("created not between", value1, value2, "created"); return (Criteria) this; } public Criteria andUpdatedIsNull() { addCriterion("updated is null"); return (Criteria) this; } public Criteria andUpdatedIsNotNull() { addCriterion("updated is not null"); return (Criteria) this; } public Criteria andUpdatedEqualTo(Date value) { addCriterion("updated =", value, "updated"); return (Criteria) this; } public Criteria andUpdatedNotEqualTo(Date value) { addCriterion("updated <>", value, "updated"); return (Criteria) this; } public Criteria andUpdatedGreaterThan(Date value) { addCriterion("updated >", value, "updated"); return (Criteria) this; } public Criteria andUpdatedGreaterThanOrEqualTo(Date value) { addCriterion("updated >=", value, "updated"); return (Criteria) this; } public Criteria andUpdatedLessThan(Date value) { addCriterion("updated <", value, "updated"); return (Criteria) this; } public Criteria andUpdatedLessThanOrEqualTo(Date value) { addCriterion("updated <=", value, "updated"); return (Criteria) this; } public Criteria andUpdatedIn(List<Date> values) { addCriterion("updated in", values, "updated"); return (Criteria) this; } public Criteria andUpdatedNotIn(List<Date> values) { addCriterion("updated not in", values, "updated"); return (Criteria) this; } public Criteria andUpdatedBetween(Date value1, Date value2) { addCriterion("updated between", value1, value2, "updated"); return (Criteria) this; } public Criteria andUpdatedNotBetween(Date value1, Date value2) { addCriterion("updated not between", value1, value2, "updated"); return (Criteria) this; } } public static class Criteria extends GeneratedCriteria { protected Criteria() { super(); } } public static class Criterion { private String condition; private Object value; private Object secondValue; private boolean noValue; private boolean singleValue; private boolean betweenValue; private boolean listValue; private String typeHandler; public String getCondition() { return condition; } public Object getValue() { return value; } public Object getSecondValue() { return secondValue; } public boolean isNoValue() { return noValue; } public boolean isSingleValue() { return singleValue; } public boolean isBetweenValue() { return betweenValue; } public boolean isListValue() { return listValue; } public String getTypeHandler() { return typeHandler; } protected Criterion(String condition) { super(); this.condition = condition; this.typeHandler = null; this.noValue = true; } protected Criterion(String condition, Object value, String typeHandler) { super(); this.condition = condition; this.value = value; this.typeHandler = typeHandler; if (value instanceof List<?>) { this.listValue = true; } else { this.singleValue = true; } } protected Criterion(String condition, Object value) { this(condition, value, null); } protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { super(); this.condition = condition; this.value = value; this.secondValue = secondValue; this.typeHandler = typeHandler; this.betweenValue = true; } protected Criterion(String condition, Object value, Object secondValue) { this(condition, value, secondValue, null); } } }
dao层:
TbContentCategoryMapper
package com.taotao.mapper; import com.taotao.pojo.TbContentCategory; import com.taotao.pojo.TbContentCategoryExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface TbContentCategoryMapper { int countByExample(TbContentCategoryExample example); int deleteByExample(TbContentCategoryExample example); int deleteByPrimaryKey(Long id); int insert(TbContentCategory record); int insertSelective(TbContentCategory record); List<TbContentCategory> selectByExample(TbContentCategoryExample example); TbContentCategory selectByPrimaryKey(Long id); int updateByExampleSelective(@Param("record") TbContentCategory record, @Param("example") TbContentCategoryExample example); int updateByExample(@Param("record") TbContentCategory record, @Param("example") TbContentCategoryExample example); int updateByPrimaryKeySelective(TbContentCategory record); int updateByPrimaryKey(TbContentCategory record); }
接口层:
ContentCategoryService.java
package com.taotao.content.service; import java.util.List; import com.taotao.common.pojo.EasyUITreeNode; import com.taotao.common.pojo.TaotaoResult; public interface ContentCategoryService { //通过节点的id查询该节点的哦子节点列表 public List<EasyUITreeNode> getContentCategoryList(Long parentId); //添加内容分类 /** * @param parentId 父节点的id * @param name 新增节点的名称 * @return */ public TaotaoResult createContentCategory(Long parentId,String name); /** * 修改节点名称 * @Title: updateContentCategory * @Description: TODO * @param: @return * @return: TaotaoResult * @throws */ public TaotaoResult updateContentCategory(Long id,String name); /** * 删除节点 * @Title: deleteContentCategory * @Description: TODO * @param: @param id * @param: @return * @return: TaotaoResult * @throws */ public TaotaoResult deleteContentCategory(Long parentId,Long id); }
业务层:
ContentCategoryServiceImpl.java
package com.taotao.content.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.taotao.common.pojo.EasyUITreeNode; import com.taotao.common.pojo.TaotaoResult; import com.taotao.content.service.ContentCategoryService; import com.taotao.mapper.TbContentCategoryMapper; import com.taotao.pojo.TbContentCategory; import com.taotao.pojo.TbContentCategoryExample; import com.taotao.pojo.TbContentCategoryExample.Criteria; /** * 内容分类 * @title ContentCategoryServiceImpl.java * <p>description</p> * <p>company: www.itheima.com</p> * @author ljh * @version 1.0 */ @Service public class ContentCategoryServiceImpl implements ContentCategoryService { @Autowired private TbContentCategoryMapper mapper; @Override public List<EasyUITreeNode> getContentCategoryList(Long parentId) { //1.注入mapper //2.创建example TbContentCategoryExample example = new TbContentCategoryExample(); //3.设置条件 Criteria criteria = example.createCriteria(); criteria.andParentIdEqualTo(parentId);//select * from tbcontentcategory where parent_id=1 //4.执行查询 List<TbContentCategory> list = mapper.selectByExample(example); //5.转成EasyUITreeNode 列表 // List<EasyUITreeNode> nodes = new ArrayList<>(); for (TbContentCategory tbContentCategory : list) { EasyUITreeNode node = new EasyUITreeNode(); node.setId(tbContentCategory.getId()); node.setState(tbContentCategory.getIsParent()?"closed":"open"); node.setText(tbContentCategory.getName());//分类名称 nodes.add(node); } //6.返回 return nodes; } @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") @Override public TaotaoResult createContentCategory(Long parentId, String name) { //1.构建对象 补全其他的属性 TbContentCategory category = new TbContentCategory(); category.setCreated(new Date()); category.setIsParent(false);//新增的节点都是叶子节点 category.setName(name); category.setParentId(parentId); category.setSortOrder(1); category.setStatus(1); category.setUpdated(category.getCreated()); //2.插入contentcategory表数据 mapper.insertSelective(category); //3.返回taotaoresult 包含内容分类的id 需要主键返回 //判断如果要添加的节点的父节点本身叶子节点 需要更新其为父节点 TbContentCategory parent = mapper.selectByPrimaryKey(parentId); if(parent.getIsParent()==false){//原本就是叶子节点 parent.setIsParent(true); mapper.updateByPrimaryKeySelective(parent);//更新节点的is_parent属性为true } return TaotaoResult.ok(category); } @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") @Override public TaotaoResult updateContentCategory(Long id, String name) { // TODO Auto-generated method stub TbContentCategory categor=mapper.selectByPrimaryKey(id); categor.setName(name); mapper.updateByPrimaryKeySelective(categor); return TaotaoResult.ok(categor); } /** * 删除内容节点 * <p>Title: deleteContentCategory</p> * <p>Description: </p> * @param parentId * @param id * @return * @see com.taotao.content.service.ContentCategoryService#deleteContentCategory(java.lang.Long, java.lang.Long) */ @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") @Override public TaotaoResult deleteContentCategory(Long parentId,Long id) { TbContentCategory categor=mapper.selectByPrimaryKey(id); if(categor.getIsParent()) { } mapper.deleteByPrimaryKey(id); return TaotaoResult.ok(); } }
控制层:
ContentCategoryController.java
package com.taotao.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.pojo.EasyUITreeNode; import com.taotao.common.pojo.TaotaoResult; import com.taotao.content.service.ContentCategoryService; /** * 内容分类的处理controller * @title ContentCategoryController.java * <p>description</p> * <p>company: www.itheima.com</p> * @author ljh * @version 1.0 */ @Controller public class ContentCategoryController { @Autowired ContentCategoryService service; /** * url : '/content/category/list', animate: true, method : "GET", 参数: id */ @RequestMapping(value="/content/category/list",method=RequestMethod.GET) @ResponseBody public List<EasyUITreeNode> getContentCategoryList(@RequestParam(value="id",defaultValue="0") Long parentId){ //1.引入服务 //2.注入服务 //3调用 return service.getContentCategoryList(parentId); } /** * 添加节点 * @Title: createContentCategory * @Description: TODO * @param: @param parentId * @param: @param name * @param: @return * @return: TaotaoResult * @throws */ @RequestMapping(value="/content/category/create",method=RequestMethod.POST) @ResponseBody public TaotaoResult createContentCategory(Long parentId, String name){ return service.createContentCategory(parentId, name); } /** * 更新节点名称 * @Title: updateContentCategory * @Description: TODO * @param: @return * @return: TaotaoResult * @throws */ @RequestMapping(value="/content/category/update",method=RequestMethod.POST) @ResponseBody public TaotaoResult updateContentCategory(Long id,String name) { return service.updateContentCategory(id, name); } /** * 删除节点 * @Title: deleteContentCategory * @Description: TODO * @param: @param parentId * @param: @param id * @param: @return * @return: TaotaoResult * @throws */ @RequestMapping(value="/content/category/delete",method=RequestMethod.POST) @ResponseBody public TaotaoResult deleteContentCategory(Long id) { return service.deleteContentCategory(null, id); } }
说明:删除节点我就没有做的那么详细,就直接通过id删除了。
删除节点的业务逻辑应该为:
1、根据id删除记录。
2、判断父节点下是否还有子节点,如果没有需要把父节点的isparent改为false
3、如果删除的是父节点,子节点要级联删除。
两种解决方案:
1)如果判断是父节点不允许删除。
递归删除。(不会推荐使用)