Commit 4af53a29 by 吴迪

递归查询栏目分类

parent c6a9f1d4
......@@ -91,32 +91,7 @@ public class NewsColumnController {
return R.ok();
}
/**
* 文章管理新增的时候获取栏目选项
*/
//@Login //标识必须登录状态下才能请求此接口
@RequestMapping("/getNewsClassList")
public R getNewsClassList(){
QueryWrapper<NewsColumnEntity> newsColumnEntityQueryWrapper = new QueryWrapper<>();
//之前前端固定写死的几个
List list = new ArrayList();
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(29);
list.add(32);
list.add(33);
list.add(38);
list.add(42);
newsColumnEntityQueryWrapper.in("columnid",list);
List<NewsColumnEntity> newsColumnEntities = newsColumnService.list(newsColumnEntityQueryWrapper);
return R.ok().put("data",newsColumnEntities);
}
......
package io.office.modules.manage.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.office.modules.manage.entity.NewsColumnEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.office.modules.manage.entity.NewsclassEntity;
import io.office.modules.manage.service.NewsclassService;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-10-12 21:19:14
*/
@RestController
@RequestMapping("/newsclass")
public class NewsclassController {
@Autowired
private NewsclassService newsclassService;
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("manage:newsclass:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = newsclassService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
// @RequiresPermissions("manage:newsclass:info")
public R info(@PathVariable("id") Integer id){
NewsclassEntity newsclass = newsclassService.getById(id);
return R.ok().put("newsclass", newsclass);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("manage:newsclass:save")
public R save(@RequestBody NewsclassEntity newsclass){
newsclassService.save(newsclass);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("manage:newsclass:update")
public R update(@RequestBody NewsclassEntity newsclass){
newsclassService.updateById(newsclass);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("manage:newsclass:delete")
public R delete(@RequestBody Integer[] ids){
newsclassService.removeByIds(Arrays.asList(ids));
return R.ok();
}
/**
* 文章管理新增的时候获取栏目选项
*/
//@Login //标识必须登录状态下才能请求此接口
@RequestMapping("/getNewsClassList")
public R getNewsClassList(){
return R.ok().put("data",newsclassService.getNewsClassList());
}
}
package io.office.modules.manage.dao;
import io.office.modules.manage.entity.NewsclassEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-10-12 21:19:14
*/
@Mapper
public interface NewsclassDao extends BaseMapper<NewsclassEntity> {
}
package io.office.modules.manage.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-10-12 21:19:14
*/
@Data
@TableName("newsClass")
public class NewsclassEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* $column.comments
*/
@TableId
private Integer id;
/**
* $column.comments
*/
private String name;
/**
* $column.comments
*/
private Integer pId;
}
package io.office.modules.manage.service;
import com.baomidou.mybatisplus.extension.service.IService;
import io.office.common.utils.PageUtils;
import io.office.modules.manage.entity.NewsclassEntity;
import io.office.modules.manage.vo.response.NewsclassEntityVo;
import java.util.List;
import java.util.Map;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-10-12 21:19:14
*/
public interface NewsclassService extends IService<NewsclassEntity> {
PageUtils queryPage(Map<String, Object> params);
/**
* 获取树形结构
* @return
*/
List<NewsclassEntityVo> getNewsClassList();
}
package io.office.modules.manage.service.impl;
import io.office.modules.manage.entity.NewsColumnEntity;
import io.office.modules.manage.vo.response.NewsclassEntityVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.office.common.utils.PageUtils;
import io.office.common.utils.Query;
import io.office.modules.manage.dao.NewsclassDao;
import io.office.modules.manage.entity.NewsclassEntity;
import io.office.modules.manage.service.NewsclassService;
@Service("newsclassService")
public class NewsclassServiceImpl extends ServiceImpl<NewsclassDao, NewsclassEntity> implements NewsclassService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<NewsclassEntity> page = this.page(
new Query<NewsclassEntity>().getPage(params),
new QueryWrapper<NewsclassEntity>()
);
return new PageUtils(page);
}
@Override
public List<NewsclassEntityVo> getNewsClassList() {
List<NewsclassEntityVo> newsclassEntityVoList = new ArrayList<>();
List<Integer> list = new ArrayList();
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.add(9);
list.add(29);
list.add(32);
list.add(33);
list.add(38);
list.add(42);
QueryWrapper<NewsclassEntity> newsclassEntityQueryWrapper = new QueryWrapper<>();
newsclassEntityQueryWrapper.in("id",list);
List<NewsclassEntity> newsclassEntities = baseMapper.selectList(newsclassEntityQueryWrapper);
if(CollectionUtils.isNotEmpty(newsclassEntities)) {
//获取全部的栏目
QueryWrapper<NewsclassEntity> newsclassEntityQueryWrapperAll = new QueryWrapper<>();
List<NewsclassEntity> newsclassEntityAll = baseMapper.selectList(newsclassEntityQueryWrapperAll);
if(CollectionUtils.isNotEmpty(newsclassEntityAll)) {
for (NewsclassEntity newsclassEntity : newsclassEntities) {
newsclassEntity.setName(newsclassEntity.getName().trim());
NewsclassEntityVo newsclassEntityVo = new NewsclassEntityVo();
BeanUtils.copyProperties(newsclassEntity,newsclassEntityVo);
newsclassEntityVo.setChild(getChild(newsclassEntity.getId(),newsclassEntityAll));
newsclassEntityVoList.add(newsclassEntityVo);
}
}
}
return newsclassEntityVoList;
}
/**
* 递归调用属性结构
*/
private static List<NewsclassEntityVo> getChild(Integer id, List<NewsclassEntity> rootMenu) {
// 子菜单
List<NewsclassEntityVo> childList = new ArrayList<>();
for (NewsclassEntity newsclassEntity : rootMenu) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (newsclassEntity.getPId()!=null) {
if (newsclassEntity.getPId().equals(id)) {
NewsclassEntityVo newsclassEntityVo =
new NewsclassEntityVo();
newsclassEntityVo.setId(newsclassEntity.getId());
newsclassEntityVo.setName(newsclassEntity.getName().trim());
newsclassEntityVo.setPId(newsclassEntity.getPId());
childList.add(newsclassEntityVo);
}
}
}
if(CollectionUtils.isNotEmpty(childList)) {
// 把子菜单的子菜单再循环一遍
for (NewsclassEntityVo children : childList) {// 没有url子菜单还有子菜单
List<NewsclassEntityVo> child = getChild(children.getId(), rootMenu);
if(CollectionUtils.isNotEmpty(child)){
children.setChild(child);
}
}
}
// 递归退出条件
if (childList.size() == 0) {
return new ArrayList<>();
}
return childList;
}
}
\ No newline at end of file
package io.office.modules.manage.vo.response;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-10-12 21:19:14
*/
@Data
public class NewsclassEntityVo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* $column.comments
*/
private Integer id;
/**
* $column.comments
*/
private String name;
/**
* $column.comments
*/
private Integer pId;
private List<NewsclassEntityVo> child;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment