1. 需求分析
我们再数据库中添加一条数据,但是页面上没显示出来,需要将所添加的这条数据的父节点的is_parent修改为1
1.添加的时候需要获取到当前选中节点的id
2.获取到当前添加的节点的名称.
3.把名称保存到数据库中, 同时把获取到的节点的id 保存成为parentid
4.判断,判断当前选中节点,是否为父节点. parent状态是否为true
5.如果为父节点,不做任何更新
6.不是话,修改当前选中这个节点的isparent状态,设置为true
修改之前
修改之后
2. 代码编辑
2.1 service
package com.taotao.service;
import java.util.List;
import com.taotao.utils.TaotaoResult;
import com.taotao.utils.TreeResult;
public interface ContentCategoryService {
//根据parentid查询所有的内容分类
public List<TreeResult> getContentCategoryList(long parentId);
//添加内容分类
public TaotaoResult saveContentCategory(long id,String cateName);
}
2.2 ContentCategoryServiceImpl.java
/**
* 添加内容分类
* 1.添加的时候需要获取到当前选中节点的id
2.获取到当前添加的节点的名称.
3.把名称保存到数据库中, 同时把获取到的节点的id 保存成为parentid
4.判断,判断当前选中节点,是否为父节点. parent状态是否为true
5.如果为父节点,不做任何更新
6.不是话,修改当前选中这个节点的isparent状态,设置为true
*/
@Override
public TaotaoResult saveContentCategory(long id, String cateName) {
//1.添加的时候需要获取到当前选中节点的id
//2.获取到当前添加的节点的名称.cateName
//3.把名称保存到数据库中, 同时把获取到的节点的id 保存成为parentid
TbContentCategory category = new TbContentCategory();
category.setName(cateName);
category.setParentId(id);
category.setIsParent(false);//实体类中是Boolean类型
category.setCreated(new Date());
category.setUpdated(new Date());
contentCategoryMapper.saveContentCategory(category);
//4.判断,判断当前选中节点,是否为父节点. parent状态是否为true
TbContentCategory parentId = contentCategoryMapper.getContentCategoryById(id);
if (!parentId.getIsParent()) {
parentId.setIsParent(true);//6.不是话,修改当前选中这个节点的isparent状态,设置为true
contentCategoryMapper.updateContentCategory(parentId);
}
return TaotaoResult.ok(category);
}
2.3 ContentCategoryController.java
/**
* 添加内容分类
* @param parentId
* @param name
* @return
*/
@RequestMapping("/create")
@ResponseBody
public TaotaoResult saveContentCategory(long parentId,String name){
return contentCategoryService.saveContentCategory(parentId, name);
}
2.4 mapper
package com.taotao.mapper;
import java.util.List;
import com.taotao.pojo.TbContentCategory;
public interface ContentCategoryMapper {
//根据parentid查询数据
List<TbContentCategory> getContentCategoryList(long parentId);
//添加内容分类
int saveContentCategory(TbContentCategory category);
//根据id查询出当前对象
TbContentCategory getContentCategoryById(long id);
//修改内容分类
int updateContentCategory(TbContentCategory parentId);
}
2.5 TbContentCategoryMapper.xml
<insert id="insert" parameterType="com.taotao.pojo.TbContentCategory" >
<selectKey keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into tb_content_category (id, parent_id, name,
status, sort_order, is_parent,
created, updated)
values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR},
#{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>
因为要将添加节点的id返回,所以在sql当中添加了如图所示