完成后台管理系统功能(十二)Cms系统中内容分类的增删改查

1.jingxi首页大广告位的实现

抽取首页展示内容的共性:

1、有一张图片

2、有一个连接

3、有一个标题

4、有链接的提示

5、价格

需要把内容进行分类,分类应该是一个树形结构。在展示首页时,可以根据分类取内容信息,把内容展示到页面。

在后台管理内容及内容分类的系统就叫做cms系统。

2.Cms系统实现

 

2.1 列表显示功能需求分析

初始化树形视图的url:/content/category/list

参数是id,当前节点id属性,应该根据此id查询子节点列表。

返回值:包含id、text、state三个属性的json数据列表

内容分类的表对应的是数据库中tb_content_category

2.2 dao层

Sql语句:

根据parentid查询节点列表

SELECT * FROM `tb_content_category` WHERE parent_id = 30;

单表查询可以实现逆向工程生成的代码。

 

2.3 service层

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    
    List<Long> list=new ArrayList<Long>();
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    
    @Override
    public List<EUTreeNode> getCategoryList(long parentId) {
        //通过查询后返回一个list 
        TbContentCategoryExample  example =new TbContentCategoryExample();
        Criteria critria =example.createCriteria();
        critria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list=tbContentCategoryMapper.selectByExample(example);
        //循环这个list后将数据赋值给node对象后再将node对象放进 resultlist中
        List<EUTreeNode> resultlist=new ArrayList<EUTreeNode>();
        for(TbContentCategory a:list){
            EUTreeNode node= new EUTreeNode();
            node.setId(a.getId());
            node.setText(a.getName());
            node.setState(a.getIsParent()? "closed":"open");
            resultlist.add(node);
        }
        return resultlist;
    }

2.4 controller层

@Controller
public class ContentCategoryController {
    
    @Autowired
    private ContentCategoryService contentCategoryService;
    
    @RequestMapping("/content/category/list")
    @ResponseBody
    public List<EUTreeNode> getCatList(@RequestParam(value ="id",defaultValue="0")long parentId){
        List<EUTreeNode> list= contentCategoryService.getCategoryList(parentId);
        return list;
        
    }

 

3 增加修改删除功能的实现

3.1 各自的链接

 

3.2 dao层

单表操作,所以使用mapper反向工程生成的代码

 

3.3 service层

@Override
    public JingXiResult insertCategory(long parentId, String name) {
        TbContentCategory tbContentCategory =new TbContentCategory();
        tbContentCategory.setName(name);
        tbContentCategory.setIsParent(false);
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setStatus(1);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        //给自增长的id赋值  前提是赋值必须比前面的id大  所以这里截取了一段时间码的中间部分
        long id=IDUtil.genItemId();
        String ids=id+"";
        String idd=ids.substring(5,9);
        long idds=Long.parseLong(idd);
        tbContentCategory.setId(idds);
        //添加记录
        tbContentCategoryMapper.insert(tbContentCategory);
        //查看父节点的isparent是否为true ,若不是 则改成true
        TbContentCategory parentCat =tbContentCategoryMapper.selectByPrimaryKey(parentId);
        if(!parentCat.getIsParent()){
            parentCat.setIsParent(true);
            //更新父节点
            tbContentCategoryMapper.updateByPrimaryKey(parentCat);
        }
        return JingXiResult.ok(tbContentCategory);
    }

    @Override
    public JingXiResult rename(long id, String name) {
        TbContentCategory tbContentCategory=tbContentCategoryMapper.selectByPrimaryKey(id);
        TbContentCategory a=new TbContentCategory();
        a.setName(name);
        a.setUpdated(new Date());
        a.setId(tbContentCategory.getId());
        a.setIsParent(tbContentCategory.getIsParent());
        a.setCreated(tbContentCategory.getCreated());
        a.setParentId(tbContentCategory.getParentId());
        a.setSortOrder(tbContentCategory.getSortOrder());
        a.setStatus(tbContentCategory.getStatus());
        tbContentCategoryMapper.updateByPrimaryKey(a);
        return JingXiResult.ok();
    }

    @Override
    public JingXiResult delete(long id) {
        
        TbContentCategory Category=tbContentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategoryMapper.deleteByPrimaryKey(id);
        long parentId=Category.getParentId();
        TbContentCategoryExample example=new TbContentCategoryExample();
        Criteria critria=example.createCriteria();
        critria.andParentIdEqualTo(parentId);
        List<TbContentCategory> a =tbContentCategoryMapper.selectByExample(example);
        if(a.size()== 0){
            TbContentCategory Category1=tbContentCategoryMapper.selectByPrimaryKey(parentId);
            Category1.setIsParent(false);
            tbContentCategoryMapper.updateByPrimaryKeySelective(Category1);
        }
        
        isNode(id);
        return JingXiResult.ok();
    }
    private void isNode(long parentId){
        TbContentCategoryExample example=new TbContentCategoryExample();
        Criteria critria=example.createCriteria();
        critria.andParentIdEqualTo(parentId);
        List<TbContentCategory> Category =tbContentCategoryMapper.selectByExample(example);
        if(Category.size()>0){
            for(TbContentCategory a:Category){
                list.add(a.getId());
                this.isNode(a.getId());
            }
        }else{
            for(long a:list){
                tbContentCategoryMapper.deleteByPrimaryKey(a);
            }
        }
    }

 

3.4 controller层

@Controller
public class ContentCategoryController {
    
    @Autowired
    private ContentCategoryService contentCategoryService;
    
    @RequestMapping("/content/category/list")
    @ResponseBody
    public List<EUTreeNode> getCatList(@RequestParam(value ="id",defaultValue="0")long parentId){
        List<EUTreeNode> list= contentCategoryService.getCategoryList(parentId);
        return list;
        
    }
    
    @RequestMapping("/content/category/create")
    @ResponseBody
    public JingXiResult create(long parentId,String name) throws Exception{
        JingXiResult result=contentCategoryService.insertCategory(parentId, name);
        return result;
    }
    
    @RequestMapping("/content/category/update")
    @ResponseBody
    public JingXiResult update(long id,String name) throws Exception{
        JingXiResult result=contentCategoryService.rename(id, name);
        return result;
    }
    
    @RequestMapping("/content/category/delete/")
    @ResponseBody
    public JingXiResult delete(long id)throws Exception{
        JingXiResult result=contentCategoryService.delete(id);
        return result;
    }
     

 

至此~ cms系统的内容分类的增删改查功能实现。

 

posted @ 2017-10-20 22:13  梦想在深圳立足  阅读(7694)  评论(1编辑  收藏  举报