• 使用SSM重新开发计科院网站


    一、游览

      在游览器地址栏输入:http://localhost:8080/index,即访问计科院首页,由于前期对数据库以及JavaBean的设计考虑不够充分,导致后期的代码臃肿,所以项目启动时对首页进行数据的填充难以实现,必须输入上述地址发出请求才可以实现(即按照实际生活中,游客访问首页的实际行为)。

    二、游客

      未登陆的游客仅可以在首页查看新闻,或者点击每一个模块的More>>链接查看更多新闻,以及点击具体的某一条新闻查看它的全部信息。

    三、登录

    在工作人员登录后,可以添加。修改以及删除新闻

    四、项目部分代码:

      本着文件的安全性考虑,第一次尝试的时候将所以的视图文件放在WEB-INF目录下,但是遇到Ueditor富文本编辑器始终无法正常显示的问题,并且未曾得到解决,无奈将所有文件移到web的根目录下。

    package mySpringMVC;
    
    import javaBean.Employee;
    import javaBean.News;
    import myConnection.DBConn;
    import myConnection.Modify;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.util.Date;
    import java.util.List;
    
    @Controller
    public class MyController {
    
        //登录验证
        @RequestMapping("/login")
        public ModelAndView checkLogin(HttpServletRequest request){
            HttpSession session =  request.getSession();
            Employee employee = new Employee();
            employee.setName(request.getParameter("name"));
            employee.setPassword(request.getParameter("password"));
            Boolean isTrue = DBConn.check(employee);
            session.setAttribute("isTrue",isTrue);
            session.setAttribute("userName",request.getParameter("name"));
            if (isTrue)
                return setIndex();
            else
                return new ModelAndView("login");
        }
    
        //注销
        @RequestMapping("/loginOut")
        public ModelAndView loginOut(HttpServletRequest request){
            HttpSession session = request.getSession();
            session.setAttribute("isTrue",false);
            return setIndex();
        }
    
        //点击单一新闻的请求响应
        @RequestMapping("/info")
        public ModelAndView dispatcherInfo(@RequestParam("type") String type,
                                           @RequestParam("id") String id){
            News news = null;
            if ("imagenews".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            else if ("academiccommunication".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            else if ("couriernews".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            else if ("dynamicofparty".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            else if ("notifydynamic".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            else if ("projectlist".equals(type)){
                news =  DBConn.getNewsById(type,Integer.parseInt(id));
            }
            return getModel(news);
        }
    
        //设置网站的访问路径和全部新闻的显示,以及返回按钮访问新闻主页
        @RequestMapping(value = {"/index","/back"})
        public ModelAndView setIndex(){
            ModelAndView modelAndView = new ModelAndView("index");
            modelAndView.addObject("imagenewsList",DBConn.getNews("imagenews"));
            modelAndView.addObject("academicList",DBConn.getNews("academiccommunication"));
            modelAndView.addObject("couriernewsList",DBConn.getNews("couriernews"));
            modelAndView.addObject("dynamicofpartyList",DBConn.getNews("dynamicofparty"));
            modelAndView.addObject("notifydynamicList",DBConn.getNews("notifydynamic"));
            modelAndView.addObject("projectList",DBConn.getNews("projectlist"));
            return modelAndView;
        }
    
        //显示该类型的所有新闻
        @RequestMapping("/newsPublish")
        public ModelAndView getTypeNews(@RequestParam("type") String databaseName){
            ModelAndView modelAndView = new ModelAndView("newsPublish");
            List<News> list = DBConn.getAllNews(databaseName);
            modelAndView.addObject("list",list);
            modelAndView.addObject("name",databaseName);
            return modelAndView;
        }
    
    
        //设置重定向的视图和数据
        public ModelAndView getModel(News news){
            ModelAndView modelAndView = new ModelAndView("info");
            modelAndView.addObject("oneNews",news);
            return modelAndView;
        }
    
        //新闻的修改
        @RequestMapping("/upData")
        public ModelAndView upData(@RequestParam("type") String databaseName, @RequestParam("id") int id){
            ModelAndView modelAndView = new ModelAndView("addNews");
            //数据库获取待修改的新闻信息
            News news =  DBConn.getNewsById(databaseName,id);
            //将数据发送至新闻编辑页面进行填充
            modelAndView.addObject("updata",news);
            modelAndView.addObject("name",databaseName);
            //设置修改操作的标识
            Modify.modify = true;
            //设置修改的新闻id
            Modify.modifyId = id;
            return modelAndView;
        }
    
        //添加新闻
        @RequestMapping("AddNews")
        public ModelAndView addNews(@RequestParam("title") String title, @RequestParam("type") String type,
                                    @RequestParam("content") String content){
    
            News news = new News();
            news.setTitle(title);
            news.setContent(content);
            news.setTime(new Date());
    
            if ("图片新闻".equals(type))
                type = "imagenews";
            else if ("学术交流".equals(type))
                type = "academiccommunication";
            else if ("新闻速递".equals(type))
                type = "couriernews";
            else if ("党建动态".equals(type))
                type = "dynamicofparty";
            else if ("通知公告".equals(type))
                type = "notifydynamic";
            else if ("专题列表".equals(type))
                type = "projectlist";
            else
                type  = "imagenews";
    
            if (!Modify.modify){
                //向数据库写入数据
                DBConn.insertNews(news,type);
            }
            else{
                news.setId(Modify.modifyId);
                DBConn.upDataNewsById(news,type);
                //重置Modify为默认状态
                Modify.modify = false;
                Modify.modifyId = -1;
            }
            return getTypeNews(type);
        }
    
        //添加新闻按钮事件
        @RequestMapping("addNews")
        public String add(){
            return "addNews";
        }
    
        //删除新闻
        @RequestMapping("/delete")
        public ModelAndView deleteNews(@RequestParam("type") String type, @RequestParam("id") int id){
            DBConn.deleteNews(type,id);
            return getTypeNews(type);
        }
    }
    MyController
    package myConnection;
    
    import javaBean.Database;
    import javaBean.DatabaseName;
    import javaBean.Employee;
    import javaBean.News;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.*;
    
    public final class DBConn {
        //连接数据库
    
        public  static SqlSession getSqlSession() {
            String resource = "myXML/mybatis-config.xml";
            InputStream inputStream = null;
            SqlSession session = null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                session = sqlSessionFactory.openSession();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return session;
        }
    
        //校验用户信息
        public static Boolean check(Employee emp){
            SqlSession sqlSession = getSqlSession();
            try {
                //selectOne的第一个参数为Mapper.xml的mapper的namespace+id
                //参数二为映射的需要传入的参数
                Employee employee = sqlSession.selectOne(
                        "MyMapper.selectEmployee_name", emp.getName());
                if (employee!=null && employee.getPassword().equals(emp.getPassword()))
                    return true;
                else
                    return false;
            } finally {
                free(sqlSession);
            }
        }
    
    
        //通过新闻表名获取该数据库的最新新闻
        public static List<News> getNews(String databaseName){
            int num = 1;
            SqlSession sqlSession = getSqlSession();
            //存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
            DatabaseName name = new DatabaseName();
            name.setDatabaseName(databaseName);
            List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
            //只在首页界面显示最新的8条新闻
            Collections.reverse(list);
            Iterator<News> iterator = list.iterator();
            List<News> temp = new ArrayList<>();
            while (iterator.hasNext() && num<9){
                    temp.add(iterator.next());
                    num++;
            }
            free(sqlSession);
            return temp;
        }
    
        //通过新闻表名获取该数据库的全部新闻
        public static List<News> getAllNews(String databaseName){
            SqlSession sqlSession = getSqlSession();
            //存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
            DatabaseName name = new DatabaseName();
            name.setDatabaseName(databaseName);
            List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
            free(sqlSession);
            return list;
        }
    
    
    
        //通过id获取某一类别的某一条新闻
        public static News getNewsById(String databaseName, int id){
            //设置数据库名和新闻id
            Database database = new Database();
            database.setDatabaseName(databaseName);
            database.setId(id);
    
            SqlSession sqlSession = getSqlSession();
            News news = sqlSession.selectOne("MyMapper.selectById",database);
            free(sqlSession);
            return news;
        }
    
    
        //释放Sqlsession
        public static void free(SqlSession sqlSession){
            sqlSession.close();
        }
    
        //向数据库添加新闻
        public static void insertNews(News news, String type) {
            SqlSession sqlSession = getSqlSession();
            news.setDatabaseName(type);
            System.out.println(news);
            int result = sqlSession.insert("MyMapper.insertNews",news);
            //提交事务
            sqlSession.commit();
            free(sqlSession);
        }
        //修改新闻信息
        public static void upDataNewsById(News news, String type) {
    //        SqlSession sqlSession = getSqlSession();
    //        news.setDatabaseName(type);
    //        System.out.println(news);
    //        int res = sqlSession.update("MyMapper.updateNews",news);
    //        sqlSession.commit();
    //        free(sqlSession);
            deleteNews(type,news.getId());
            insertNews(news,type);
        }
    
        //删除新闻
        public static void deleteNews(String databaseName,int id) {
            SqlSession sqlSession = getSqlSession();
            Database database = new Database();
            database.setDatabaseName(databaseName);
            database.setId(id);
            int result = sqlSession.delete("MyMapper.deleteNews",database);
            sqlSession.commit();
            free(sqlSession);
        }
    }
    数据库操作

    五、数据库设计

    自身观点:检索的时候减少遍历的长度以及条件的判断,以及将新闻分类创建多个表。但是对于新闻的JavaBean类设计存在瑕疵,应将新闻的类别作为Bean的一个属性。

    百度网盘链接:链接: https://pan.baidu.com/s/14uMjrISBK-6jOnU6zCHn_w 提取码: 573u

  • 相关阅读:
    ●BZOJ 3894 文理分科
    ●BZOJ 1797 [Ahoi2009]Mincut 最小割
    ●BZOJ 1934 [Shoi2007]Vote 善意的投票
    ●BZOJ 3996 [TJOI2015]线性代数
    php--->self与static区别
    php--->使用callable强制指定回调类型
    php--->依赖注入(DI)实现控制反转(IOC)
    php--->cookie和session
    php--->注册模式
    linux---> siege压力测试工具使用
  • 原文地址:https://www.cnblogs.com/xjtsh/p/10728984.html
Copyright © 2020-2023  润新知