新闻管理系统——系统管理员模块(二)

目  录

1、主题模块的增删改查

①在控制层编写主题模块的增删改查

②效果图

主题列表

添加主题

修改主题 

​删除主题 


1、主题模块的增删改查

①在控制层编写主题模块的增删改查

TopicController.java

package com.manong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manong.entity.News;
import com.manong.entity.Topic;
import com.manong.service.NewsService;
import com.manong.service.TopicService;
import com.manong.vo.TopicQueryVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 主题列表
 */
@Controller
@RequestMapping("/topic")
public class TopicController {

    @Resource
    private TopicService topicService;

    @Resource
    private NewsService newsService;

    /**
     * 主题列表
     * @param topicQueryVo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String manager(TopicQueryVo topicQueryVo, Model model){
        IPage<Topic> page = topicService.page(new Page<Topic>(topicQueryVo.getPageNo(),topicQueryVo.getPageSize()));
        model.addAttribute("page",page);
        return "topic/list";
    }

    /**
     * 去到添加主题页面
     * @return
     */
    @RequestMapping("/add.html")
    public String toAdd(){
        return "topic/add";
    }

    /**
     * 添加主题
     * @param topic
     * @return
     */
    @RequestMapping("/add")
    public String add(Topic topic){
        if(topicService.save(topic)){
            return "redirect:/topic/list";
        }
        return "redirect:/topic/add.html";
    }

    /**
     * 去到编辑主题页面
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String toEdit(@PathVariable Integer id, Model model){
        model.addAttribute("topic",topicService.getById(id));
        return "topic/edit";
    }


    /**
     * 编辑主题
     * @param topic
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Topic topic){
        if(topicService.updateById(topic)){
            return "redirect:/topic/list";
        }
        return "redirect:/topic/edit/"+topic.getTid();
    }


    /**
     * 删除主题
     * @param id
     * @param response
     */
    @RequestMapping("/deleteById/{id}")
    public void deleteById(@PathVariable Integer id, HttpServletResponse response){
        try {
            response.setCharacterEncoding("GBK");
            QueryWrapper<News> queryWrapper = new QueryWrapper<News>();
            queryWrapper.eq("ntid",id);
            if(newsService.count(queryWrapper)>0){
                response.getWriter().print("<script>alert('该主题下存在新闻信息,无法删除');location.href='/topic/list';</script>");
            }else{
                //调用删除的方法
                if(topicService.removeById(id)){
                    response.getWriter().print("<script>alert('删除成功');location.href='/topic/list';</script>");
                }else{
                    response.getWriter().print("<script>alert('删除失败');location.href='/topic/list';</script>");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

②效果图

主题列表

添加主题

修改主题 

删除主题 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值