controller
// 管理模块中,总的文章查询(不包括被删除的)@RequestMapping("/articleSelect1")@ResponseBodyprivate Map<String, Object> selectAll1(int limit, int page, String wenzhang, String leixing, String bu,Integer lan) {Map<String, Object> map = new HashMap<String, Object>();
//方法中的wenzhang,leixing,bu是查找条件参数PageHelper.startPage(page, limit);//maven引入pagehelper分页List<Article> list = articleService.getAll(wenzhang, leixing, bu, lan);long count = articleService.getAllCount(wenzhang, leixing, bu, lan);map.put("data", list);map.put("code", 0);map.put("count", count);//layui前台接收的数据格式如上return map;}
//添加一篇文章@RequestMapping("/articleAdd1")@ResponseBodyprivate Map<String, Object> add1(@RequestBody HashMap<String, String> map2) throws Exception {Map<String, Object> map = new HashMap<String, Object>();int caId = Integer.parseInt(map2.get("caId"));String arTitle = map2.get("arTitle");String arContent = map2.get("arContent");String arUser = map2.get("arUser");String arState = map2.get("arState");String arDuty = map2.get("arDuty");String arSpare1 = map2.get("arSpare1");String arSpare2 = map2.get("arSpare2");String arSpare3 = map2.get("arSpare3");String arPosition = map2.get("arPosition");String arImage = map2.get("arImage");String arDetail = map2.get("arDetail");Article article = new Article();long maxcount = articleService.getAllCount();long max = maxcount + 1;/* System.out.println(max); */String arNumber = String.valueOf(max);article.setArNumber(arNumber);//获取文章总量,为文章赋值序列号,便于后期排序Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyMMdd");SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");String da = format.format(date);String da2 = format2.format(date);double random = Math.random() * 10000;String arId = da + (int) random;String arTime = da2;//把日期转化为字符格式,用时间字符串加随机数做文章IdSystem.out.println(arSpare1 + arState);article.setArState(arState);article.setArId(arId);article.setArTime(arTime);article.setArContent(arContent);article.setArDuty(arDuty);article.setArPosition(arPosition);article.setArSpare1(arSpare1);article.setArSpare2(arSpare2);article.setArSpare3(arSpare3);article.setArTitle(arTitle);article.setCaId(caId);article.setArUser(arUser);article.setArImage(arImage);article.setArDetail(arDetail);boolean row = articleService.articleAdd(article);if (row) {List<Article> list = articleService.getAll();long count = articleService.getAllCount();map.put("data", list);map.put("code", 0);map.put("returnCode", "200");map.put("count", count);return map;} else {map.put("code", 1);// 插入失败return map;}}
//修改一篇文章@RequestMapping("/articleUpdate")@ResponseBodyprivate Map<String, Object> update(@RequestBody HashMap<String, String> map2) throws Exception {Map<String, Object> map = new HashMap<String, Object>();int caId = Integer.parseInt(map2.get("caId"));String arId = map2.get("arId");String arTitle = map2.get("arTitle");String arContent = map2.get("arContent");String arState = map2.get("arState");String arDuty = map2.get("arDuty");String arSpare1 = map2.get("arSpare1");String arSpare2 = map2.get("arSpare2");String arSpare3 = map2.get("arSpare3");String arPosition = map2.get("arPosition");String arImage = map2.get("arImage");String arDetail = map2.get("arDetail");Article article = new Article();article.setArId(arId);article.setArContent(arContent);article.setArDuty(arDuty);article.setArPosition(arPosition);article.setArSpare2(arSpare2);article.setArSpare3(arSpare3);article.setArTitle(arTitle);article.setCaId(caId);article.setArImage(arImage);article.setArSpare1(arSpare1);article.setArState(arState);article.setArDetail(arDetail);boolean row = articleService.articleUpdate(article);System.out.println("执行了更新");if (row) {List<Article> list = articleService.getAll();long count = articleService.getAllCount();map.put("data", list);map.put("code", 0);map.put("returnCode", "200");map.put("count", count);return map;} else {map.put("code", 1);return map;}}
//伪删除,获取文章Id,修改状态参数@RequestMapping("/articleDelete")@ResponseBodyprivate Map<String, Object> delete(@RequestBody HashMap<String, String> map2) throws Exception {Map<String, Object> map = new HashMap<String, Object>();String arId = map2.get("arId");Article article = new Article();article.setArId(arId);article.setArState("3");boolean row = articleService.articleDelete(article);if (row) {List<Article> list = articleService.getAll();long count = articleService.getAllCount();map.put("data", list);map.put("code", 0);map.put("returnCode", "200");map.put("count", count);return map;} else {map.put("code", 1);return map;}}
//文件上传后台代码@ResponseBody@RequestMapping(value = "/upload", method = RequestMethod.POST)private Map<String, Object> upload(MultipartFile file, HttpServletRequest request) {String prefix = "";String dateStr = "";// 保存上传OutputStream out = null;InputStream fileInput = null;System.out.println("运行到文件成功0");try {if (file != null) {String originalName = file.getOriginalFilename();prefix = originalName.substring(originalName.lastIndexOf(".") + 1);Date date = new Date();String uuid = UUID.randomUUID() + "";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");dateStr = simpleDateFormat.format(date);/** String filepath =* "D:\\WorkTools\\Tomcat\\apache-tomcat-9.0.22\\webapps\\temp\\" + dateStr + "\* \" + uuid + "." + prefix;*/String filepath = "D:\\gyy\\tomcat8\\webapps\\temp\\" + dateStr + "\\" + uuid + "." + prefix;File files = new File(filepath);// 查看上传路径System.out.println(filepath);if (!files.getParentFile().exists()) {files.getParentFile().mkdirs();}file.transferTo(files);Map<String, Object> map2 = new HashMap<String, Object>();Map<String, Object> map = new HashMap<String, Object>();map.put("code", 0);map.put("msg", "");map.put("data", map2);map2.put("src", "/temp/" + dateStr + "/" + uuid + "." + prefix);map2.put("filename", originalName);return map;}} catch (Exception e) {} finally {try {if (out != null) {out.close();}if (fileInput != null) {fileInput.close();}} catch (IOException e) {}}Map<String, Object> map = new HashMap<String, Object>();map.put("code", 1);map.put("msg", "");return map;}
//新闻展示页面获取后台文章列表
@RequestMapping("")//这里设成空字符,访问Ip会默认执行这个命令public String index(Map<String, Object> map) {long pagesize = 10;// 初始页,被减到零自动跳回1long currentpage = 1;//List<Article> articlelist1 = articleService.selectarticlebyca(0, 5, 1);List<Article> articlelist2 = articleService.selectarticlebyca(0, 5, 2);List<Article> articlelist3 = articleService.selectarticlebyca(0, 5, 3);List<Article> articlelist4 = articleService.selectarticlebyca(0, 5, 4);List<Article> articlelist5 = articleService.selectarticlebyca(0, 5, 5);List<Article> articlelist6 = articleService.selectarticlebyca(0, 5, 6);List<Article> articlelist7 = articleService.selectarticlebyca(0, 5, 7);// 大图List<Article> articlelist8 = articleService.selectarticlebycadatu(0, 6, "2");// 活动掠影List<Article> articlelist9 = articleService.selectarticlebycadatu(0, 8, "3");List<Catalog> catlist = catalogService.getAll();map.put("article1", articlelist1);map.put("article2", articlelist2);map.put("article3", articlelist3);map.put("article4", articlelist4);map.put("article5", articlelist5);map.put("article6", articlelist6);map.put("article7", articlelist7);map.put("article8", articlelist8);map.put("article9", articlelist9);map.put("catlist", catlist);return "pomain";}
Service
public boolean articleAdd(Article article) {// TODO Auto-generated method stubarticleMapper.insertSelective(article);return true;}public boolean articleUpdate(Article article) {// TODO Auto-generated method stubarticleMapper.updateByPrimaryKeySelective(article);return true;}public List<Article> getAll(String wenzhang, String leixing, String bu, Integer lan) {// TODO Auto-generated method stubArticleExample example = new ArticleExample();example.setOrderByClause("ar_number*1 DESC");ArticleExample.Criteria criteria = example.createCriteria();if (null != wenzhang && !"".equals(wenzhang)) {criteria.andArTitleLike("%" + wenzhang + "%");}if (null != leixing && !"".equals(leixing)) {criteria.andArDutyEqualTo(leixing);}if (null != bu && !"".equals(bu)) {criteria.andArUserEqualTo(bu);}if (null != lan && !"".equals(lan)) {criteria.andCaIdEqualTo(lan);}criteria.andArStateNotEqualTo("3");List<Article> articleList1 = articleMapper.selectByExampleByMyself(example);ArticleExample example1 = new ArticleExample();ArticleExample.Criteria criteria1 = example1.createCriteria();criteria1.andArDutyEqualTo("2");// 上传下达类型的,检测未上传文件人员 ,赋值给文章属性List<Article> articleList = articleMapper.selectByExample(example1);for (Article arlist : articleList) {List<Comment> commentList = commentMapper.selectByExampleByMyself(arlist.getArId());String str1 = "xxxx";//如果用户在文章下面回复了,就把他的名字从未回复名单xxxx中删除for (Comment comments : commentList) {String str2 = comments.getComName();boolean result1 = str1.contains(str2);String str3 = str2 + ",";System.out.println(result1);if (result1) {str1 = str1.replace(str3, "");} else {}}/* System.out.println(str1); */arlist.setArNotyet(str1);articleMapper.updateByPrimaryKeySelective(arlist);}
以上实现了新闻网站的基本功能。