标签:builder splay 码云 公告 方法 setname not delete 文件中
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
在游览器地址栏输入:http://localhost:8080/index,即访问计科院首页,且必须输入上述地址发出请求才可以实现(即按照实际生活中,游客访问首页的实际行为)。
未登陆的游客仅可以在首页查看新闻,或者点击每一个模块的More>>链接查看更多新闻,以及点击具体的某一条新闻查看它的全部信息。
在工作人员登录后,可以添加。修改以及删除新闻。这些操作和上次的MVC模式下的操作页面相同,这里不再介绍。
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); } } 数据库操作
检索的时候减少遍历的长度以及条件的判断,以及将新闻分类创建多个表,且应将新闻的类别作为Bean的一个属性。
标签:builder splay 码云 公告 方法 setname not delete 文件中
原文地址:https://www.cnblogs.com/tianhaoqi/p/13197240.html