树结构demo

controller:

public class ContentCategoryController {


@Autowired
private ContentCategoryService contentCategoryService;


/**
* 根据父类id查询分类:获取树 /category?id=30

* @param id
* @return
*/
@ResponseBody
@RequestMapping(value = "category")
public List<ContentCategory> list(@RequestParam(value = "id", required = true, defaultValue = "0") Long id) {
List<ContentCategory> list = contentCategoryService.getListByParentId(id);
return list;
}


/***
* 增加

* @param contentCategory
* @return
*/
@ResponseBody
@RequestMapping(value = "/category/add", method = RequestMethod.POST)
public ContentCategory add(ContentCategory contentCategory) {
contentCategory = contentCategoryService.add(contentCategory);
return contentCategory;
}


/***
* 修改

* @param contentCategory
* @return
*/
@ResponseBody
@RequestMapping(value = "/category/update", method = RequestMethod.POST)
public String update(ContentCategory contentCategory) {
int ucount = contentCategoryService.updateContentCategory(contentCategory);
if (ucount > 0) {
return "success";
}
return null;
}

/***
* 删除内容分类
* @param contentCategory
* @return
*/
@ResponseBody
@RequestMapping(value="/category/delete",method=RequestMethod.POST)
public String delete(ContentCategory contentCategory){
int dcount = contentCategoryService.deleteContentCategory(contentCategory);
if (dcount > 0) {
return "success";
}
return null;
}
}

service:

@Service
public class ContentCategoryServiceImpl extends BaseServiceImpl<ContentCategory> implements ContentCategoryService {


@Autowired
private ContentCategoryMapper contentCategoryMapper;

/**
* 根据父节点获取所有子节点
*/
@Override
public List<ContentCategory> getListByParentId(Long parentId) {


return super.getListByParentId(parentId, ContentCategory.ContentCategory_NORMAL);
}


@Override
public ContentCategory add(ContentCategory contentCategory) {
// 补全数据
contentCategory.setStatus(ContentCategory.ContentCategory_NORMAL);
contentCategory.setCreated(new Date());
contentCategory.setUpdated(contentCategory.getCreated());
contentCategory.setIsParent(false);
contentCategory.setSortOrder(1);


int acount = contentCategoryMapper.insertSelective(contentCategory);


// 如果当前子类是在叶子节点添加的,那么需要将它改成非叶子节点状态
ContentCategory parentContentCategory = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
if (!parentContentCategory.getIsParent()) {
// 将它改成非叶子节点
parentContentCategory.setIsParent(true);
contentCategoryMapper.updateByPrimaryKeySelective(parentContentCategory);
}


return contentCategory;
}


@Override
public int updateContentCategory(ContentCategory contentCategory) {
contentCategory.setUpdated(new Date());
return contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
}


/***
* 删除内容类目 同时递归删除它的子类目 还要判断当前被删除的类目的父类下是否还有子类目 如果没有,则需要修改它的状态isParent=false
*/
@Override
public int deleteContentCategory(ContentCategory contentCategory) {
// 1、查找到当前内容分类的所有子类,递归查找
List<Object> ids = new ArrayList<Object>();
allNextNode(ids, contentCategory.getId());//所有子节点的id
ids.add(contentCategory.getId());//当前节点的id


// 2、删除当前内容分类和它所有的子类
int dcount = super.deleteByIds(ids);


// 3、判断当前内容分类的父类是否还有子类,如果没有,则修改它的状态,改为非父类
if (contentCategory.getParentId() > 0) {
// 查询该类的父类下是否还有子类
ContentCategory needCategory = new ContentCategory();
needCategory.setParentId(contentCategory.getParentId());
int count = contentCategoryMapper.selectCount(needCategory);
// 如果没有,则将它额isParent修改成false,即叶子节点
if (count <= 0) {
ContentCategory parentContentCategory = new ContentCategory();
parentContentCategory.setId(contentCategory.getParentId());
parentContentCategory.setIsParent(false);
contentCategoryMapper.updateByPrimaryKey(parentContentCategory);
}
}
return dcount;
}


/**
* 递归获取所有子节点

* @param list
* @param id
*/
public void allNextNode(List<Object> list, Long id) {
List<ContentCategory> allNode = super.getListByParentId(id, ContentCategory.ContentCategory_NORMAL);
// allNode为空的时候,跳出该方法,防止了死循环发生
if (allNode != null && allNode.size() > 0) {
for (ContentCategory contentCategory : allNode) {
list.add(contentCategory.getId());
allNextNode(list, contentCategory.getId());
}
}
}
}

VO

@Entity
@Table(name = "TB_CONTENT_CATEGORY")
public class ContentCategory implements java.io.Serializable {
/** 版本号 */
private static final long serialVersionUID = -85404677744449382905356L;


/**
* 正常状态
*/
public static final Integer ContentCategory_NORMAL = 1;
/**
* 删除状态
*/
public static final Integer ContentCategory_DELETE = 2;


/** 类目ID */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;


/** 父类目ID=0时,代表的是一级的类目 */
@Column(name = "PARENT_ID")
private Long parentId;


/** 分类名称 */
@Column(name = "NAME")
private String name;


/** 状态。可选值:1(正常),2(删除) */
@Column(name = "STATUS")
private Integer status;


/** 排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数 */
@Column(name = "SORT_ORDER")
private Integer sortOrder;


/** 该类目是否为父类目,1为true,0为false */
@Column(name = "IS_PARENT")
private Boolean isParent;


/** 创建时间 */
@Column(name = "CREATED")
private Date created;


/** 创建时间 */
@Column(name = "UPDATED")
private Date updated;


/**
* 获取类目ID

* @return 类目ID
*/
public Long getId() {
return this.id;
}


/**
* 设置类目ID

* @param id
*            类目ID
*/
public void setId(Long id) {
this.id = id;
}


/**
* 获取父类目ID=0时,代表的是一级的类目

* @return 父类目ID=0时
*/
public Long getParentId() {
return this.parentId;
}


/**
* 设置父类目ID=0时,代表的是一级的类目

* @param parentId
*            父类目ID=0时,代表的是一级的类目
*/
public void setParentId(Long parentId) {
this.parentId = parentId;
}


/**
* 获取分类名称

* @return 分类名称
*/
public String getName() {
return this.name;
}


/**
* 设置分类名称

* @param name
*            分类名称
*/
public void setName(String name) {
this.name = name;
}


/**
* 获取状态。可选值:1(正常),2(删除)

* @return 状态。可选值:1(正常)
*/
public Integer getStatus() {
return this.status;
}


/**
* 设置状态。可选值:1(正常),2(删除)

* @param status
*            状态。可选值:1(正常),2(删除)
*/
public void setStatus(Integer status) {
this.status = status;
}


/**
* 获取排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数

* @return 排列序号
*/
public Integer getSortOrder() {
return this.sortOrder;
}


/**
* 设置排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数

* @param sortOrder
*            排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数
*/
public void setSortOrder(Integer sortOrder) {
this.sortOrder = sortOrder;
}


/**
* 获取该类目是否为父类目,1为true,0为false

* @return 该类目是否为父类目
*/
public Boolean getIsParent() {
return this.isParent;
}


/**
* 设置该类目是否为父类目,1为true,0为false

* @param isParent
*            该类目是否为父类目,1为true,0为false
*/
public void setIsParent(Boolean isParent) {
this.isParent = isParent;
}


/**
* 获取创建时间

* @return 创建时间
*/
public Date getCreated() {
return this.created;
}


/**
* 设置创建时间

* @param created
*            创建时间
*/
public void setCreated(Date created) {
this.created = created;
}


/**
* 获取创建时间

* @return 创建时间
*/
public Date getUpdated() {
return this.updated;
}


/**
* 设置创建时间

* @param updated
*            创建时间
*/
public void setUpdated(Date updated) {
this.updated = updated;
}


/***
* 获得树节点的文本信息-->类目的名称

* @return
*/
public String getText() {
return name;
}


/**
* 判断是否是子节点

* @return
*/
public String getState() {
return isParent ? "closed" : "open";
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值