新闻管理系统(ssm+jsp+mysql)
作者:mmseoamin日期:2023-12-13

目录

摘要

一、系统开发环境

二、数据库设计

三、实现的功能

四、详细设计

五、效果展示


摘要

  • 游客可以浏览新闻,也可以通过新闻类别或新闻标题对新闻进行查找;
  • 管理员可以增加新闻、修改新闻、根据新闻类别查找新闻、删除新闻,也可以增加类别、修改类别、删除类别;在删除类别时,该类别的新闻会被同时删除。

    一、系统开发环境

    Eclipse2021.03

    jdk1.8.0

    Tomcat9.0

    Mysql8.0.26

    二、数据库设计

    新闻表t_news

    新闻管理系统(ssm+jsp+mysql),第1张

    类型表t_type

    新闻管理系统(ssm+jsp+mysql),第2张

    用户表t_user

    新闻管理系统(ssm+jsp+mysql),第3张

    三、实现的功能

    普通用户(游客):

    (1)显示所有新闻

    (2)浏览新闻

    (3)根据新闻类型对新闻进行查找

    (4)根据新闻标题对新闻进行查找

    管理员用户:

    (1)显示所有新闻

    (2)增加新闻

    (3)修改新闻

    (4)根据新闻类型对新闻进行查找

    (5)删除新闻

    (6)显示所有类别

    (7)增加新闻类别

    (8)修改新闻类别

    (9)删除新闻类别

    四、详细设计

    • 开发步骤和主要代码

      撰写流程:从dao —> service —> controller —> 视图

      NewsDao.java

      package zsls.news.dao;
      import org.apache.ibatis.annotations.Param;
      import zsls.news.pojo.News;
      import java.util.List;
      public interface NewsDao {
          List queryALL(@Param("tid") Integer tid,@Param("title") String title,@Param("num") int num );
          News queryById(Integer id);
          int save(News news);
          int update(News news);
          int deleteById(Integer id);
          int deleteByTid(Integer tid);
      }

      TypeDao.java

      package zsls.news.dao;
      import org.apache.ibatis.annotations.Param;
      import zsls.news.pojo.Type;
      import java.util.List;
      public interface TypeDao {
          public List queryAllTypes();
          public List queryType(@Param("name")String name);
          public int insertType(@Param("name")String name);
          public int deleteByRecid(String id);
          Type queryById(Integer id);
          int update(Type type);
      }

      UserDao.java

      package zsls.news.dao;
      import org.apache.ibatis.annotations.Param;
      import zsls.news.pojo.User;
      public interface UserDao {
          public User queryUserByUsernameAndPassword(@Param("name")String username, @Param("pass")String password);
      }

      NewsService.java

      package zsls.news.service;
      import zsls.news.pojo.News;
      import java.util.List;
      public interface NewsService {
          List queryALL(Integer tid,String title,Integer pageNo);
          News queryById(Integer id);
          int save(News news);
          int update(News news);
          int deleteById(Integer id);
      }

      NewsServiceImpl.java

      package zsls.news.service;
      import zsls.news.dao.NewsDao;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import zsls.news.pojo.News;
      import java.util.List;
      @Service
      public class NewsServiceImpl implements NewsService {
          @Autowired
          private NewsDao newsDao;
          @Override
          public List queryALL(Integer tid,String title,Integer pageNo) {
              if(title != null && !"".equals(title.trim())){
                  title = "%"+title+"%";
              }
             int num = (pageNo - 1) * 10;
              return newsDao.queryALL(tid,title,num);
          }
          @Override
          public News queryById(Integer id) {
              return newsDao.queryById(id);
          }
          @Override
          public int save(News news) {
              return newsDao.save(news);
          }
          @Override
          public int update(News news) {
              return newsDao.update(news);
          }
          @Override
          public int deleteById(Integer id) {
              return newsDao.deleteById(id);
          }
      }

      TypeService.java

      package zsls.news.service;
      import zsls.news.pojo.Type;
      import java.util.List;
      public interface TypeService {
          public List queryAllTypes();
          public int deleteByRecid(String recid);
          int addType(String name);
          Type queryById(Integer id);
          int update(Type type);
      }

      TypeServiceImpl.java

      package zsls.news.service;
      import zsls.news.dao.NewsDao;
      import zsls.news.dao.TypeDao;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import zsls.news.pojo.Type;
      import java.util.List;
      @Service
      public class TypeServiceImpl implements TypeService {
          @Autowired
          private TypeDao typeDao;
          @Autowired
          private NewsDao newsDao;
          @Override
          public List queryAllTypes() {
              // TODO Auto-generated method stub
              return typeDao.queryAllTypes();
          }
          @Override
          public int deleteByRecid(String recid) {
              // TODO Auto-generated method stub
              int i=typeDao.deleteByRecid(recid);
              // 删除类型,把同类的新闻也全部删除
               int deleteByTid = newsDao.deleteByTid(Integer.valueOf(recid));
              return i;
          }
          @Override
          public int addType(String name) {
              return typeDao.insertType(name);
          }
          @Override
          public Type queryById(Integer id) {
              return typeDao.queryById(id);
          }
          @Override
          public int update(Type type) {
              return typeDao.update(type);
          }
      }

      UserService.java

      package zsls.news.service;
      import zsls.news.pojo.User;
      public interface UserService {
          public User queryUserByUsernameAndPassword(String username, String password);
      }

      UserServiceImpl.java

      package zsls.news.service;
      import zsls.news.dao.UserDao;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import zsls.news.pojo.User;
      @Service
      public class UserServiceImpl implements UserService {
          @Autowired
          private UserDao userDao;
          @Override
          public User queryUserByUsernameAndPassword(String username, String password) {
              // TODO Auto-generated method stub
              return userDao.queryUserByUsernameAndPassword(username, password);
          }
      }

      AdminController.java

      package zsls.news.controller;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import zsls.news.pojo.News;
      import zsls.news.pojo.Type;
      import zsls.news.pojo.User;
      import zsls.news.service.NewsService;
      import zsls.news.service.TypeService;
      import zsls.news.service.UserService;
      import javax.servlet.http.HttpSession;
      import java.util.Date;
      import java.util.List;
      @Controller
      public class AdminController {
          @Autowired
          private UserService userService;
          @Autowired
          private TypeService typeService;
          @Autowired
          private NewsService newsService;
          @RequestMapping("login.do")
          public String login(String username, String password, HttpSession session, Model model){
              User user=userService.queryUserByUsernameAndPassword(username, password);
              System.out.println("当前正在执行控制器方法login...");
              if(user!=null){
                  session.setAttribute("loginUser",user);
                  return "/manager/index";
              }else{
                  model.addAttribute("msg","登录失败,请重新登录!");
                  return "/login";
              }
          }
          @RequestMapping("logout.do")
          public String logout(HttpSession session){
              session.invalidate();
              return "/login";
          }
          @RequestMapping("toLogin.do")
          public String toLogin(){
              return "/login";
          }
          @RequestMapping("admin/toIndex.do")
          public String toIndex(){
              return "/manager/index";
          }
          @RequestMapping("admin/queryAllTypes.do")   //管理员方法
          public String AdminQueryAllTypes(Model model){
              List typelist=typeService.queryAllTypes();
              model.addAttribute("typelist",typelist);
              return "/manager/categoryList";
          }
          @RequestMapping("admin/toAddType.do")
          public String toAddType(){
              return "/manager/categoryAdd";
          }
          @RequestMapping("admin/addType.do")
          public String addType(String name,Model model){
              int i=typeService.addType(name);
              if(i>0){
                  return "redirect:queryAllTypes.do";
              }else{
                  model.addAttribute("msg","添加类型不成功!");
                  return "/error";
              }
          }
          @RequestMapping("admin/toModifyType.do")
          public String toModifyType(Integer recid,Model model){
              Type type = typeService.queryById(recid);
              model.addAttribute("type",type);
              return "/manager/categoryModify";
          }
          @RequestMapping("admin/modifyType.do")
          public String modifyType(Type type,Model model){
              int i=typeService.update(type);
              if(i>0){
                  return "redirect:queryAllTypes.do";
              }else{
                  model.addAttribute("msg","修改类型不成功!");
                  return "/error";
              }
          }
          @RequestMapping("admin/delete.do")   //管理员方法
          public String deleteByRecid(String recid,Model model){
              int i=typeService.deleteByRecid(recid);
              if(i>0){
                  return "redirect:queryAllTypes.do";
              }else{
                  model.addAttribute("msg","删除类型不成功!");
                  return "/error";
              }
          }
          @RequestMapping("admin/queryAllNews.do")   //管理员方法
          public String AdminQueryAllNews(Integer tid,Integer pageNo,Model model){
              if(pageNo == null || pageNo < 1){
                  pageNo = 1;
              }
              List newsList = newsService.queryALL(tid,null,pageNo);
              model.addAttribute("newsList",newsList);
              List typelist=typeService.queryAllTypes();
              model.addAttribute("typelist",typelist);
              model.addAttribute("pageNo1",pageNo-1);
              model.addAttribute("pageNo",pageNo);
              model.addAttribute("pageNo2",pageNo+1);
              model.addAttribute("tid",tid);
              return "/manager/newsList";
          }
          @RequestMapping("admin/toAddNews.do")
          public String toAddNews(Model model){
              List typelist=typeService.queryAllTypes();
              model.addAttribute("typelist",typelist);
              return "/manager/newsAdd";
          }
          @RequestMapping("admin/addNews.do")
          public String addNews(News news,Model model){
              if(news.getTid() == null ){
                  model.addAttribute("msg","添加新闻类型不能为空!");
                  return "/error";
              }
              if(news.getTitle() == null || "".equals(news.getTitle())  || news.getContent() == null || "".equals(news.getContent())){
                  model.addAttribute("msg","添加新闻标题或内容不能为空!");
                  return "/error";
              }
              news.setDate(new Date());
              int i=newsService.save(news);
              if(i>0){
                  return "redirect:queryAllNews.do";
              }else{
                  model.addAttribute("msg","添加新闻不成功!");
                  return "/error";
              }
          }
          @RequestMapping("admin/deleteNews.do")
          public String deleteNews(Integer id,Model model){
              int i=newsService.deleteById(id);
              if(i>0){
                  return "redirect:queryAllNews.do";
              }else{
                  model.addAttribute("msg","删除新闻不成功!");
                  return "/error";
              }
          }
          @RequestMapping("admin/toModifyNews.do")
          public String toModifyNews(Integer id,Model model){
              News news = newsService.queryById(id);
              model.addAttribute("news",news);
              return "/manager/newsModify";
          }
          @RequestMapping("admin/modifyNews.do")
          public String modifyNews(News news,Model model){
              news.setDate(new Date());
              int i=  newsService.update(news);
              if(i>0){
                  return "redirect:queryAllNews.do";
              }else{
                  model.addAttribute("msg","修改新闻不成功!");
                  return "/error";
              }
          }
          @RequestMapping("queryAllTypes.do")
          public String queryAllBooks(Model model){
              List typelist=typeService.queryAllTypes();
              model.addAttribute("typelist",typelist);
              return "index";
          }
      }

      VisitorController.java

      package zsls.news.controller;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import zsls.news.pojo.News;
      import zsls.news.pojo.Type;
      import zsls.news.service.NewsService;
      import zsls.news.service.TypeService;
      import java.util.List;
      @Controller
      public class VisitorController {
          @Autowired
          private TypeService typeService;
          @Autowired
          private NewsService newsService;
          @RequestMapping("visitor/toIndex.do")
          public String toIndex(){
              return "/index";
          }
          @RequestMapping("visitor/queryAllNews.do")
          public String queryAllNews(Integer tid,String title,Integer pageNo, Model model){
              if(pageNo == null || pageNo < 1){
                  pageNo = 1;
              }
              List newsList = newsService.queryALL(tid,title,pageNo);
              model.addAttribute("newsList",newsList);
              List typelist=typeService.queryAllTypes();
              model.addAttribute("typelist",typelist);
              model.addAttribute("pageNo1",pageNo-1);
              model.addAttribute("pageNo",pageNo);
              model.addAttribute("pageNo2",pageNo+1);
              model.addAttribute("tid",tid);
              model.addAttribute("title",title);
              return "/search";
          }
          @RequestMapping("visitor/queryNews.do")
          public String queryNews(Integer id, Model model){
              News news = newsService.queryById(id);
              model.addAttribute("news",news);
              Type type = typeService.queryById(news.getTid());
              model.addAttribute("type",type);
              return "/info";
          }
      }

      index.jsp (管理员)

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      

          欢迎来到

      系统的后台管理

      index.jsp (游客)

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      
      
      
          
          Insert title here
      
      
      欢迎您
          主页
          新闻列表
          登录
      

      欢迎来浏览新闻

      LoginInterceptor.java (登录拦截器)

      package zsls.news.interceptor;
      import org.springframework.web.servlet.HandlerInterceptor;
      import org.springframework.web.servlet.ModelAndView;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      public class LoginInterceptor implements HandlerInterceptor {
      	@Override
      	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
      			throws Exception {
      		// 该方法是在整个请求执行即渲染结束后执行,通过此方法实现一些资源清理,记录日志信息等工作
      		System.out.println("执行afterCompletion....");
      	}
      	@Override
      	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mv)
      			throws Exception {
      		该方法是在控制器方法调用之后,且视图解析还没进行之前
      		System.out.println("执行postHandle....");
      	}
      	@Override
      	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
      		// 该方法是在控制器方法执行前执行,如果为真,则继续下一步的操作,也就是接着执行控制器方法或调用下一个拦截器;为假,则中断
      		System.out.println("执行preHandle....");
      		//获取当前请求的地址
      		String uri=request.getRequestURI();
      		if(uri.indexOf("login")>0) {   //当前你的请求是login,放行
      			return true;
      		}
      		HttpSession session=request.getSession();
      		if(session.getAttribute("loginUser")!=null) {
      			return true;
      		}
      		session.setAttribute("errorMsg","您还没有登录,请先登录!");
      		request.getRequestDispatcher("/login.jsp").forward(request, response);
      		return false;
      	}	
      }

      login.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      
      
      
          
          Insert title here
      
      
      ${sessionScope.errorMsg}
      
      username:
      password:

      游客主页

      categoryAdd.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      


      categoryList.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      

      添加类型
      类别编号 类别名称 删除 编辑
      ${type.id} ${type.name} 删除 编辑

      categoryModify.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      



      newsAdd.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      




      newsList.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      

      添加新闻
      序号 标题 日期 编辑 删除
      ${news.id} ${news.title} 编辑 删除
      当前是第${pageNo}页     上一页 下一页

      newsModify.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      
      
      
          
          Insert title here
      
      
      欢迎您,${loginUser.name}
          主页
          新闻列表
          类型列表
          注销
      





      info.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      
      
      
          
          Insert title here
      
      
      欢迎您
          主页
          新闻列表
          登录
      

      ${news.title}

      ${type.name}      

      ${news.content}

      search.jsp

      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      
      
      
          
          Insert title here
      
      
      欢迎您
          主页
          新闻列表
          登录
      


      序号 标题 日期 详情
      ${news.id} ${news.title} 详情
      当前是第${pageNo}页     上一页 下一页

      五、效果展示

      普通用户(游客)

      (1)显示所有新闻

      新闻管理系统(ssm+jsp+mysql),第4张

       

      (2)浏览新闻

      新闻管理系统(ssm+jsp+mysql),第5张

       

      (3)根据新闻类别对新闻进行查找

      新闻管理系统(ssm+jsp+mysql),第6张

       

      (4)根据新闻标题对新闻进行查找

      新闻管理系统(ssm+jsp+mysql),第7张

       

      管理员用户

      (1)显示所有新闻

      新闻管理系统(ssm+jsp+mysql),第8张

       

      (2)增加新闻

      新闻管理系统(ssm+jsp+mysql),第9张

      新闻管理系统(ssm+jsp+mysql),第10张 

       (3)修改新闻

      新闻管理系统(ssm+jsp+mysql),第11张

      新闻管理系统(ssm+jsp+mysql),第12张 

       (4)根据新闻类型对新闻进行查找

      新闻管理系统(ssm+jsp+mysql),第13张

       

      (5)删除新闻

      新闻管理系统(ssm+jsp+mysql),第14张

       

      (6)显示所有类别

      新闻管理系统(ssm+jsp+mysql),第15张

       

      (7)增加新闻类别

      新闻管理系统(ssm+jsp+mysql),第16张

      新闻管理系统(ssm+jsp+mysql),第17张 

       (8)修改新闻类别

      新闻管理系统(ssm+jsp+mysql),第18张新闻管理系统(ssm+jsp+mysql),第19张

       

      (9)删除新闻类别

      新闻管理系统(ssm+jsp+mysql),第20张