新闻管理系统——新闻详情页面+评论模块

目  录

1、新闻详情页面

①Controller层

②dao层

③service接口

④service实现类

⑤效果图

 2、评论模块添加评论

①直接创建CommentsController控制类

②效果图


1、新闻详情页面

根据新闻的编号去获取新闻的详细信息:

①Controller层

因为新闻News的Mapper类和Service类都已经采用Mybatis Plus的默认方法实现了,所以我们只需要进行Controller层代码编写即可

NewsController.java

package com.gec.newsproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.newsproject.entity.Comments;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.service.CommentsService;
import com.gec.newsproject.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @Autowired
    CommentsService commentsService;

    @RequestMapping("/detail/{nid}")
    public String detail(@PathVariable("nid") Integer nid, Model model){
        //先查询出来左侧的分类标题
        List<News> newsList1 = newsService.getNewsListByTopicId(1, 6);
        List<News> newsList2 = newsService.getNewsListByTopicId(2, 6);
        List<News> newsList3 = newsService.getNewsListByTopicId(3, 6);
        //将数据存入模型中
        model.addAttribute("newsList1",newsList1);
        model.addAttribute("newsList2",newsList2);
        model.addAttribute("newsList3",newsList3);
        //根据id拿出新闻详细信息
        News news = newsService.getById(nid);
        model.addAttribute("news",news);
        //获取评论列表
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        //根据新闻编号查询新闻信息
        wrapper.eq("cnid",nid);
        List<Comments> commentsList = commentsService.list(wrapper);
        model.addAttribute("commentsList",commentsList);
        return "news/detail";
    }
}

②dao层

因为这个页面还需要展示新闻的评论信息 所以我们完成新闻评论模块,CommentsMapper的实现:

CommentsMapper.java

package com.gec.newsproject.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gec.newsproject.entity.Comments;

public interface CommentsMapper extends BaseMapper<Comments> {
}

③service接口

CommentsService.java

package com.gec.newsproject.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.gec.newsproject.entity.Comments;

public interface CommentsService extends IService<Comments> {
}

④service实现类

CommentsServiceImpl.java

package com.gec.newsproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.newsproject.dao.CommentsMapper;
import com.gec.newsproject.entity.Comments;
import com.gec.newsproject.service.CommentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
}

⑤效果图

 2、评论模块添加评论

①直接创建CommentsController控制类

CommentsController.java

package com.gec.newsproject.controller;

import com.gec.newsproject.entity.Comments;
import com.gec.newsproject.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

@Controller
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    CommentsService commentsService;

    @RequestMapping("/add")
    public String add(Comments comments, HttpServletResponse response){
        comments.setCdate(new Date());
        if (commentsService.save(comments)){
                return "redirect:/news/detail/"+comments.getCnid();
            }
            return "redirect:/news/detail/"+comments.getCnid();
    }

}

②效果图

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值