diff --git a/src/java/controller/admin/AdminController.java b/src/java/controller/admin/AdminController.java new file mode 100644 index 0000000..90882f8 --- /dev/null +++ b/src/java/controller/admin/AdminController.java @@ -0,0 +1,248 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import com.liuyanzhao.ssm.blog.dto.JsonResult; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CommentService; +import com.liuyanzhao.ssm.blog.service.UserService; +import org.json.JSONObject; +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 org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.liuyanzhao.ssm.blog.util.MyUtils.getIpAddr; + +/** + * @author liuyanzhao + */ +@Controller +public class AdminController { + @Autowired + private UserService userService; + + @Autowired + private ArticleService articleService; + + @Autowired + private CommentService commentService; + + /** + * 后台首页 + * + * @return + */ + @RequestMapping("/admin") + public String index(Model model, HttpSession session) { + User user = (User) session.getAttribute("user"); + Integer userId = null; + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + userId = user.getUserId(); + } + //文章列表 + List
articleList = articleService.listRecentArticle(userId, 5); + model.addAttribute("articleList", articleList); + + //评论列表 + List commentList = commentService.listRecentComment(userId, 5); + model.addAttribute("commentList", commentList); + return "Admin/index"; + } + + /** + * 登录页面显示 + * + * @return + */ + @RequestMapping("/login") + public String + loginPage() { + return "Admin/login"; + } + + + /** + * 登录页面显示 + * + * @return + */ + @RequestMapping("/register") + public String registerPage() { + return "Admin/register"; + } + + /** + * 登录验证 + * + * @param request + * @param response + * @return + */ + @RequestMapping(value = "/loginVerify", method = RequestMethod.POST, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public String loginVerify(HttpServletRequest request, HttpServletResponse response) { + Map map = new HashMap(); + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + String rememberme = request.getParameter("rememberme"); + User user = userService.getUserByNameOrEmail(username); + if (user == null) { + map.put("code", 0); + map.put("msg", "用户名无效!"); + } else if (!user.getUserPass().equals(password)) { + map.put("code", 0); + map.put("msg", "密码错误!"); + } else if (user.getUserStatus() == 0) { + map.put("code", 0); + map.put("msg", "账号已禁用!"); + } else { + //登录成功 + map.put("code", 1); + map.put("msg", ""); + //添加session + request.getSession().setAttribute("user", user); + //添加cookie + if (rememberme != null) { + //创建两个Cookie对象 + Cookie nameCookie = new Cookie("username", username); + //设置Cookie的有效期为3天 + nameCookie.setMaxAge(60 * 60 * 24 * 3); + Cookie pwdCookie = new Cookie("password", password); + pwdCookie.setMaxAge(60 * 60 * 24 * 3); + response.addCookie(nameCookie); + response.addCookie(pwdCookie); + } + user.setUserLastLoginTime(new Date()); + user.setUserLastLoginIp(getIpAddr(request)); + userService.updateUser(user); + + } + String result = new JSONObject(map).toString(); + return result; + } + + /** + * 登录验证 + * + * @param request + * @return + */ + @RequestMapping(value = "/registerSubmit", method = RequestMethod.POST) + @ResponseBody + public JsonResult registerSubmit(HttpServletRequest request) { + String username = request.getParameter("username"); + String nickname = request.getParameter("nickname"); + String email = request.getParameter("email"); + String password = request.getParameter("password"); + User checkUserName = userService.getUserByName(username); + if (checkUserName != null) { + return new JsonResult().fail("用户名已存在"); + } + User checkEmail = userService.getUserByEmail(username); + if (checkEmail != null) { + return new JsonResult().fail("电子邮箱已存在"); + } + + // 添加用户 + User user = new User(); + user.setUserAvatar("/img/avatar/avatar.png"); + user.setUserName(username); + user.setUserNickname(nickname); + user.setUserPass(password); + user.setUserEmail(email); + user.setUserStatus(1); + user.setArticleCount(0); + user.setUserRole(UserRole.USER.getValue()); + try { + userService.insertUser(user); + } catch (Exception e) { + e.printStackTrace(); + return new JsonResult().fail("系统异常"); + } + return new JsonResult().ok("注册成功"); + } + + /** + * 退出登录 + * + * @param session + * @return + */ + @RequestMapping(value = "/admin/logout") + public String logout(HttpSession session) { + session.removeAttribute("user"); + session.invalidate(); + return "redirect:/login"; + } + + + /** + * 基本信息页面显示 + * + * @return + */ + @RequestMapping(value = "/admin/profile") + public ModelAndView userProfileView(HttpSession session) { + + ModelAndView modelAndView = new ModelAndView(); + User sessionUser = (User) session.getAttribute("user"); + User user = userService.getUserById(sessionUser.getUserId()); + modelAndView.addObject("user", user); + + modelAndView.setViewName("Admin/User/profile"); + return modelAndView; + } + + /** + * 编辑个人信息页面显示 + * + * @param session + * @return + */ + @RequestMapping(value = "/admin/profile/edit") + public ModelAndView editUserView(HttpSession session) { + ModelAndView modelAndView = new ModelAndView(); + User loginUser = (User) session.getAttribute("user"); + User user = userService.getUserById(loginUser.getUserId()); + modelAndView.addObject("user", user); + + modelAndView.setViewName("Admin/User/editProfile"); + return modelAndView; + } + + + /** + * 编辑用户提交 + * + * @param user + * @return + */ + @RequestMapping(value = "/admin/profile/save", method = RequestMethod.POST) + public String saveProfile(User user, HttpSession session) { + User dbUser = (User) session.getAttribute("user"); + + user.setUserId(dbUser.getUserId()); + userService.updateUser(user); + return "redirect:/admin/profile"; + } + + +} diff --git a/src/java/controller/admin/BackArticleController.java b/src/java/controller/admin/BackArticleController.java new file mode 100644 index 0000000..55d82cc --- /dev/null +++ b/src/java/controller/admin/BackArticleController.java @@ -0,0 +1,296 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import cn.hutool.http.HtmlUtil; +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.dto.ArticleParam; +import com.liuyanzhao.ssm.blog.dto.JsonResult; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CategoryService; +import com.liuyanzhao.ssm.blog.service.TagService; + +import com.liuyanzhao.ssm.blog.entity.Category; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.entity.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpSession; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Objects; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/article") +public class BackArticleController { + @Autowired + private ArticleService articleService; + + @Autowired + private TagService tagService; + + @Autowired + private CategoryService categoryService; + + /** + * 后台文章列表显示 + * + * @return modelAndView + */ + @RequestMapping(value = "") + public String index(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + @RequestParam(required = false) String status, Model model, + HttpSession session) { + HashMap criteria = new HashMap<>(1); + if (status == null) { + model.addAttribute("pageUrlPrefix", "/admin/article?pageIndex"); + } else { + criteria.put("status", status); + model.addAttribute("pageUrlPrefix", "/admin/article?status=" + status + "&pageIndex"); + } + + User user = (User) session.getAttribute("user"); + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + criteria.put("userId", user.getUserId()); + } + PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", articlePageInfo); + return "Admin/Article/index"; + } + + + /** + * 后台添加文章页面显示 + * + * @return + */ + @RequestMapping(value = "/insert") + public String insertArticleView(Model model) { + List categoryList = categoryService.listCategory(); + List tagList = tagService.listTag(); + model.addAttribute("categoryList", categoryList); + model.addAttribute("tagList", tagList); + return "Admin/Article/insert"; + } + + /** + * 后台添加文章提交操作 + * + * @param articleParam + * @return + */ + @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) + public String insertArticleSubmit(HttpSession session, ArticleParam articleParam) { + Article article = new Article(); + //用户ID + User user = (User) session.getAttribute("user"); + if (user != null) { + article.setArticleUserId(user.getUserId()); + } + article.setArticleTitle(articleParam.getArticleTitle()); + //文章摘要 + int summaryLength = 150; + String summaryText = HtmlUtil.cleanHtmlTag(articleParam.getArticleContent()); + if (summaryText.length() > summaryLength) { + String summary = summaryText.substring(0, summaryLength); + article.setArticleSummary(summary); + } else { + article.setArticleSummary(summaryText); + } + article.setArticleThumbnail(articleParam.getArticleThumbnail()); + article.setArticleContent(articleParam.getArticleContent()); + article.setArticleStatus(articleParam.getArticleStatus()); + //填充分类 + List categoryList = new ArrayList<>(); + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleParentCategoryId())); + } + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleChildCategoryId())); + } + article.setCategoryList(categoryList); + //填充标签 + List tagList = new ArrayList<>(); + if (articleParam.getArticleTagIds() != null) { + for (int i = 0; i < articleParam.getArticleTagIds().size(); i++) { + Tag tag = new Tag(articleParam.getArticleTagIds().get(i)); + tagList.add(tag); + } + } + article.setTagList(tagList); + + articleService.insertArticle(article); + return "redirect:/admin/article"; + } + + + /** + * 删除文章 + * + * @param id 文章ID + */ + @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) + public void deleteArticle(@PathVariable("id") Integer id, HttpSession session) { + Article dbArticle = articleService.getArticleByStatusAndId(null, id); + if (dbArticle == null) { + return; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(dbArticle.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return; + } + articleService.deleteArticle(id); + } + + + /** + * 编辑文章页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public String editArticleView(@PathVariable("id") Integer id, Model model, HttpSession session) { + + Article article = articleService.getArticleByStatusAndId(null, id); + if (article == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(article.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } + + model.addAttribute("article", article); + + List categoryList = categoryService.listCategory(); + model.addAttribute("categoryList", categoryList); + + List tagList = tagService.listTag(); + model.addAttribute("tagList", tagList); + + return "Admin/Article/edit"; + } + + + /** + * 编辑文章提交 + * + * @param articleParam + * @return + */ + @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) + public String editArticleSubmit(ArticleParam articleParam, HttpSession session) { + Article dbArticle = articleService.getArticleByStatusAndId(null, articleParam.getArticleId()); + if (dbArticle == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,则跳转403 + if (!Objects.equals(dbArticle.getArticleUserId(), user.getUserId()) && !Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } + Article article = new Article(); + article.setArticleThumbnail(articleParam.getArticleThumbnail()); + article.setArticleId(articleParam.getArticleId()); + article.setArticleTitle(articleParam.getArticleTitle()); + article.setArticleContent(articleParam.getArticleContent()); + article.setArticleStatus(articleParam.getArticleStatus()); + //文章摘要 + int summaryLength = 150; + String summaryText = HtmlUtil.cleanHtmlTag(article.getArticleContent()); + if (summaryText.length() > summaryLength) { + String summary = summaryText.substring(0, summaryLength); + article.setArticleSummary(summary); + } else { + article.setArticleSummary(summaryText); + } + //填充分类 + List categoryList = new ArrayList<>(); + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleParentCategoryId())); + } + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleChildCategoryId())); + } + article.setCategoryList(categoryList); + //填充标签 + List tagList = new ArrayList<>(); + if (articleParam.getArticleTagIds() != null) { + for (int i = 0; i < articleParam.getArticleTagIds().size(); i++) { + Tag tag = new Tag(articleParam.getArticleTagIds().get(i)); + tagList.add(tag); + } + } + article.setTagList(tagList); + articleService.updateArticleDetail(article); + return "redirect:/admin/article"; + } + + /** + * 后台添加文章提交操作 + * + * @param articleParam + * @return + */ + @RequestMapping(value = "/insertDraftSubmit", method = RequestMethod.POST) + public String insertDraftSubmit(HttpSession session, ArticleParam articleParam) { + Article article = new Article(); + //用户ID + User user = (User) session.getAttribute("user"); + if (user != null) { + article.setArticleUserId(user.getUserId()); + } + article.setArticleTitle(articleParam.getArticleTitle()); + //文章摘要 + int summaryLength = 150; + String summaryText = HtmlUtil.cleanHtmlTag(articleParam.getArticleContent()); + if (summaryText.length() > summaryLength) { + String summary = summaryText.substring(0, summaryLength); + article.setArticleSummary(summary); + } else { + article.setArticleSummary(summaryText); + } + article.setArticleThumbnail(articleParam.getArticleThumbnail()); + article.setArticleContent(articleParam.getArticleContent()); + article.setArticleStatus(articleParam.getArticleStatus()); + //填充分类 + List categoryList = new ArrayList<>(); + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleParentCategoryId())); + } + if (articleParam.getArticleChildCategoryId() != null) { + categoryList.add(new Category(articleParam.getArticleChildCategoryId())); + } + article.setCategoryList(categoryList); + //填充标签 + List tagList = new ArrayList<>(); + if (articleParam.getArticleTagIds() != null) { + for (int i = 0; i < articleParam.getArticleTagIds().size(); i++) { + Tag tag = new Tag(articleParam.getArticleTagIds().get(i)); + tagList.add(tag); + } + } + article.setTagList(tagList); + + articleService.insertArticle(article); + return "redirect:/admin"; + } + +} + + + diff --git a/src/java/controller/admin/BackCategoryController.java b/src/java/controller/admin/BackCategoryController.java new file mode 100644 index 0000000..2ec0751 --- /dev/null +++ b/src/java/controller/admin/BackCategoryController.java @@ -0,0 +1,110 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import com.liuyanzhao.ssm.blog.entity.Category; + +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import java.util.List; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/category") +public class BackCategoryController { + + @Autowired + private ArticleService articleService; + + + @Autowired + private CategoryService categoryService; + + /** + * 后台分类列表显示 + * + * @return + */ + @RequestMapping(value = "") + public ModelAndView categoryList() { + ModelAndView modelandview = new ModelAndView(); + List categoryList = categoryService.listCategoryWithCount(); + modelandview.addObject("categoryList",categoryList); + modelandview.setViewName("Admin/Category/index"); + return modelandview; + + } + + + /** + * 后台添加分类提交 + * + * @param category + * @return + */ + @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST) + public String insertCategorySubmit(Category category) { + categoryService.insertCategory(category); + return "redirect:/admin/category"; + } + + /** + * 删除分类 + * + * @param id + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deleteCategory(@PathVariable("id") Integer id) { + //禁止删除有文章的分类 + int count = articleService.countArticleByCategoryId(id); + + if (count == 0) { + categoryService.deleteCategory(id); + } + return "redirect:/admin/category"; + } + + /** + * 编辑分类页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editCategoryView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + Category category = categoryService.getCategoryById(id); + modelAndView.addObject("category",category); + + List categoryList = categoryService.listCategoryWithCount(); + modelAndView.addObject("categoryList",categoryList); + + modelAndView.setViewName("Admin/Category/edit"); + return modelAndView; + } + + /** + * 编辑分类提交 + * + * @param category 分类 + * @return 重定向 + */ + @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) + public String editCategorySubmit(Category category) { + categoryService.updateCategory(category); + return "redirect:/admin/category"; + } +} diff --git a/src/java/controller/admin/BackCommentController.java b/src/java/controller/admin/BackCommentController.java new file mode 100644 index 0000000..ebf5e59 --- /dev/null +++ b/src/java/controller/admin/BackCommentController.java @@ -0,0 +1,218 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import cn.hutool.http.HtmlUtil; +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.dto.JsonResult; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.enums.Role; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import com.liuyanzhao.ssm.blog.util.MyUtils; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CommentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.*; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/comment") +public class BackCommentController { + + @Autowired + private CommentService commentService; + + @Autowired + private ArticleService articleService; + + /** + * 评论页面 + * 我发送的评论 + * + * @param pageIndex 页码 + * @param pageSize 页大小 + * @return modelAndView + */ + @RequestMapping(value = "") + public String commentList(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + HttpSession session, + Model model) { + User user = (User) session.getAttribute("user"); + HashMap criteria = new HashMap<>(); + if (!UserRole.ADMIN.getValue().equals(user.getUserRole())) { + // 用户查询自己的文章, 管理员查询所有的 + criteria.put("userId", user.getUserId()); + } + PageInfo commentPageInfo = commentService.listCommentByPage(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", commentPageInfo); + model.addAttribute("pageUrlPrefix", "/admin/comment?pageIndex"); + return "Admin/Comment/index"; + } + + + /** + * 评论页面 + * 我收到的评论 + * + * @param pageIndex 页码 + * @param pageSize 页大小 + * @return modelAndView + */ + @RequestMapping(value = "/receive") + public String myReceiveComment(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + HttpSession session, + Model model) { + User user = (User) session.getAttribute("user"); + PageInfo commentPageInfo = commentService.listReceiveCommentByPage(pageIndex, pageSize, user.getUserId()); + model.addAttribute("pageInfo", commentPageInfo); + model.addAttribute("pageUrlPrefix", "/admin/comment?pageIndex"); + return "Admin/Comment/index"; + } + + + /** + * 添加评论 + * + * @param request + * @param comment + */ + @RequestMapping(value = "/insert", method = {RequestMethod.POST}, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public void insertComment(HttpServletRequest request, Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId()); + if (article == null) { + return; + } + + //添加评论 + comment.setCommentUserId(user.getUserId()); + comment.setCommentIp(MyUtils.getIpAddr(request)); + comment.setCommentCreateTime(new Date()); + commentService.insertComment(comment); + //更新文章的评论数 + articleService.updateCommentCount(article.getArticleId()); + } + + /** + * 删除评论 + * + * @param id 批量ID + */ + @RequestMapping(value = "/delete/{id}") + public void deleteComment(@PathVariable("id") Integer id, HttpSession session) { + Comment comment = commentService.getCommentById(id); + User user = (User) session.getAttribute("user"); + // 如果不是管理员,访问其他用户的数据,没有权限 + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue()) && !Objects.equals(comment.getCommentUserId(), user.getUserId())) { + return; + } + //删除评论 + commentService.deleteComment(id); + //删除其子评论 + List childCommentList = commentService.listChildComment(id); + for (int i = 0; i < childCommentList.size(); i++) { + commentService.deleteComment(childCommentList.get(i).getCommentId()); + } + //更新文章的评论数 + Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId()); + articleService.updateCommentCount(article.getArticleId()); + } + + /** + * 编辑评论页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public String editCommentView(@PathVariable("id") Integer id, Model model, HttpSession session) { + // 没有权限操作,只有管理员可以操作 + User user = (User) session.getAttribute("user"); + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } + Comment comment = commentService.getCommentById(id); + model.addAttribute("comment", comment); + return "Admin/Comment/edit"; + } + + + /** + * 编辑评论提交 + * + * @param comment + * @return + */ + @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) + public String editCommentSubmit(Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + // 没有权限操作,只有管理员可以操作 + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return "redirect:/403"; + } + commentService.updateComment(comment); + return "redirect:/admin/comment"; + } + + + /** + * 回复评论页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/reply/{id}") + public String replyCommentView(@PathVariable("id") Integer id, Model model) { + Comment comment = commentService.getCommentById(id); + model.addAttribute("comment", comment); + return "Admin/Comment/reply"; + } + + /** + * 回复评论提交 + * + * @param request + * @param comment + * @return + */ + @RequestMapping(value = "/replySubmit", method = RequestMethod.POST) + public String replyCommentSubmit(HttpServletRequest request, Comment comment, HttpSession session) { + //文章评论数+1 + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId()); + if (article == null) { + return "redirect:/404"; + } + User user = (User) session.getAttribute("user"); + comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent())); + comment.setCommentAuthorName(user.getUserNickname()); + comment.setCommentAuthorEmail(user.getUserEmail()); + comment.setCommentAuthorUrl(user.getUserUrl()); + article.setArticleCommentCount(article.getArticleCommentCount() + 1); + articleService.updateArticle(article); + //添加评论 + comment.setCommentCreateTime(new Date()); + comment.setCommentIp(MyUtils.getIpAddr(request)); + if (Objects.equals(user.getUserId(), article.getArticleUserId())) { + comment.setCommentRole(Role.OWNER.getValue()); + } else { + comment.setCommentRole(Role.VISITOR.getValue()); + } + commentService.insertComment(comment); + return "redirect:/admin/comment"; + } + +} diff --git a/src/java/controller/admin/BackLinkController.java b/src/java/controller/admin/BackLinkController.java new file mode 100644 index 0000000..65bba8e --- /dev/null +++ b/src/java/controller/admin/BackLinkController.java @@ -0,0 +1,121 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import com.liuyanzhao.ssm.blog.entity.Link; +import com.liuyanzhao.ssm.blog.service.LinkService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import java.util.Date; +import java.util.List; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/link") +public class BackLinkController { + + @Autowired + private LinkService linkService; + + /** + * 后台链接列表显示 + * + * @return modelAndView + */ + @RequestMapping(value = "") + public ModelAndView linkList() { + ModelAndView modelandview = new ModelAndView(); + + List linkList = linkService.listLink(null); + modelandview.addObject("linkList",linkList); + + modelandview.setViewName("Admin/Link/index"); + return modelandview; + + } + + /** + * 后台添加链接页面显示 + * + * @return modelAndView + */ + @RequestMapping(value = "/insert") + public ModelAndView insertLinkView() { + ModelAndView modelAndView = new ModelAndView(); + + List linkList = linkService.listLink(null); + modelAndView.addObject("linkList",linkList); + + modelAndView.setViewName("Admin/Link/insert"); + return modelAndView; + } + + /** + * 后台添加链接页面提交 + * + * @param link 链接 + * @return 响应 + */ + @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST) + public String insertLinkSubmit(Link link) { + link.setLinkCreateTime(new Date()); + link.setLinkUpdateTime(new Date()); + link.setLinkStatus(1); + linkService.insertLink(link); + return "redirect:/admin/link/insert"; + } + + /** + * 删除链接 + * + * @param id 链接ID + * @return 响应 + */ + @RequestMapping(value = "/delete/{id}") + public String deleteLink(@PathVariable("id") Integer id) { + + linkService.deleteLink(id); + return "redirect:/admin/link"; + } + + /** + * 编辑链接页面显示 + * + * @param id + * @return modelAndVIew + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editLinkView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + Link linkCustom = linkService.getLinkById(id); + modelAndView.addObject("linkCustom",linkCustom); + + List linkList = linkService.listLink(null); + modelAndView.addObject("linkList",linkList); + + modelAndView.setViewName("Admin/Link/edit"); + return modelAndView; + } + + + /** + * 编辑链接提交 + * + * @param link 链接 + * @return 响应 + */ + @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) + public String editLinkSubmit(Link link) { + link.setLinkUpdateTime(new Date()); + linkService.updateLink(link); + return "redirect:/admin/link"; + } +} diff --git a/src/java/controller/admin/BackMenuController.java b/src/java/controller/admin/BackMenuController.java new file mode 100644 index 0000000..076aae2 --- /dev/null +++ b/src/java/controller/admin/BackMenuController.java @@ -0,0 +1,100 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import com.liuyanzhao.ssm.blog.entity.Menu; +import com.liuyanzhao.ssm.blog.enums.MenuLevel; +import com.liuyanzhao.ssm.blog.service.MenuService; +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 org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/menu") +public class BackMenuController { + + @Autowired + private MenuService menuService; + + /** + * 后台菜单列表显示 + * + * @return + */ + @RequestMapping(value = "") + public String menuList(Model model) { + List menuList = menuService.listMenu(); + model.addAttribute("menuList",menuList); + return "Admin/Menu/index"; + } + + /** + * 添加菜单内容提交 + * + * @param menu + * @return + */ + @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST) + public String insertMenuSubmit(Menu menu) { + if(menu.getMenuOrder() == null) { + menu.setMenuOrder(MenuLevel.TOP_MENU.getValue()); + } + menuService.insertMenu(menu); + return "redirect:/admin/menu"; + } + + /** + * 删除菜单内容 + * + * @param id + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deleteMenu(@PathVariable("id") Integer id) { + menuService.deleteMenu(id); + return "redirect:/admin/menu"; + } + + /** + * 编辑菜单内容显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editMenuView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + Menu menu = menuService.getMenuById(id); + modelAndView.addObject("menu",menu); + + List menuList = menuService.listMenu(); + modelAndView.addObject("menuList",menuList); + + modelAndView.setViewName("Admin/Menu/edit"); + return modelAndView; + } + + + /** + * 编辑菜单内容提交 + * + * @param menu + * @return + */ + @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) + public String editMenuSubmit(Menu menu) { + menuService.updateMenu(menu); + return "redirect:/admin/menu"; + } + + + +} diff --git a/src/java/controller/admin/BackNoticeController.java b/src/java/controller/admin/BackNoticeController.java new file mode 100644 index 0000000..5fdbd1d --- /dev/null +++ b/src/java/controller/admin/BackNoticeController.java @@ -0,0 +1,105 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import com.liuyanzhao.ssm.blog.entity.Notice; +import com.liuyanzhao.ssm.blog.enums.NoticeStatus; +import com.liuyanzhao.ssm.blog.service.NoticeService; +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 org.springframework.web.bind.annotation.RequestMethod; + +import java.util.Date; +import java.util.List; + + +@Controller +@RequestMapping("/admin/notice") +public class BackNoticeController { + + @Autowired + private NoticeService noticeService; + + /** + * 后台公告列表显示 + * + * @return + */ + @RequestMapping(value = "") + public String index(Model model) { + List noticeList = noticeService.listNotice(null); + model.addAttribute("noticeList", noticeList); + return "Admin/Notice/index"; + + } + + /** + * 添加公告显示 + * + * @return + */ + @RequestMapping(value = "/insert") + public String insertNoticeView() { + return "Admin/Notice/insert"; + } + + /** + * 添加公告提交 + * + * @param notice + * @return + */ + @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) + public String insertNoticeSubmit(Notice notice) { + notice.setNoticeCreateTime(new Date()); + notice.setNoticeUpdateTime(new Date()); + notice.setNoticeStatus(NoticeStatus.NORMAL.getValue()); + notice.setNoticeOrder(1); + noticeService.insertNotice(notice); + return "redirect:/admin/notice"; + } + + /** + * 删除公告 + * + * @param id + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deleteNotice(@PathVariable("id") Integer id) { + noticeService.deleteNotice(id); + + return "redirect:/admin/notice"; + } + + /** + * 编辑公告页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public String editNoticeView(@PathVariable("id") Integer id, Model model) { + Notice notice = noticeService.getNoticeById(id); + model.addAttribute("notice", notice); + return "Admin/Notice/edit"; + } + + + /** + * 编辑公告页面显示 + * + * @param notice + * @return + */ + @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) + public String editNoticeSubmit(Notice notice) { + notice.setNoticeUpdateTime(new Date()); + noticeService.updateNotice(notice); + return "redirect:/admin/notice"; + } + + +} diff --git a/src/java/controller/admin/BackOptionsController.java b/src/java/controller/admin/BackOptionsController.java new file mode 100644 index 0000000..7d98fc1 --- /dev/null +++ b/src/java/controller/admin/BackOptionsController.java @@ -0,0 +1,71 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import com.liuyanzhao.ssm.blog.entity.Options; +import com.liuyanzhao.ssm.blog.service.OptionsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/options") +public class BackOptionsController { + + @Autowired + private OptionsService optionsService; + + + /** + * 基本信息显示 + * + * @return + */ + @RequestMapping(value = "") + public ModelAndView index() { + ModelAndView modelAndView = new ModelAndView(); + Options option = optionsService.getOptions(); + modelAndView.addObject("option",option); + + modelAndView.setViewName("Admin/Options/index"); + return modelAndView; + } + + /** + * 编辑基本信息显示 + * + * @return + */ + @RequestMapping(value = "/edit") + public ModelAndView editOptionView() { + ModelAndView modelAndView = new ModelAndView(); + Options option = optionsService.getOptions(); + modelAndView.addObject("option",option); + + modelAndView.setViewName("Admin/Options/edit"); + return modelAndView; + } + + /** + * 编辑基本信息提交 + * + * @param options + * @return + */ + @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) + public String editOptionSubmit(Options options) { + //如果记录不存在,那就新建 + Options optionsCustom = optionsService.getOptions(); + if(optionsCustom.getOptionId()==null) { + optionsService.insertOptions(options); + } else { + optionsService.updateOptions(options); + } + return "redirect:/admin/options"; + } + +} diff --git a/src/java/controller/admin/BackPageController.java b/src/java/controller/admin/BackPageController.java new file mode 100644 index 0000000..0205694 --- /dev/null +++ b/src/java/controller/admin/BackPageController.java @@ -0,0 +1,128 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import com.liuyanzhao.ssm.blog.entity.Page; +import com.liuyanzhao.ssm.blog.enums.PageStatus; +import com.liuyanzhao.ssm.blog.service.PageService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import java.util.Date; +import java.util.List; +import java.util.Objects; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/page") +public class BackPageController { + + @Autowired + private PageService pageService; + + /** + * 后台页面列表显示 + * + * @return + */ + @RequestMapping(value = "") + public ModelAndView index() { + ModelAndView modelAndView = new ModelAndView(); + List pageList = pageService.listPage(null); + modelAndView.addObject("pageList", pageList); + modelAndView.setViewName("Admin/Page/index"); + return modelAndView; + } + + + /** + * 后台添加页面页面显示 + * + * @return + */ + @RequestMapping(value = "/insert") + public ModelAndView insertPageView() { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("Admin/Page/insert"); + return modelAndView; + } + + /** + * 后台添加页面提交操作 + * + * @param page + * @return + */ + @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) + public String insertPageSubmit(Page page) { + + //判断别名是否存在 + Page checkPage = pageService.getPageByKey(null, page.getPageKey()); + if (checkPage == null) { + page.setPageCreateTime(new Date()); + page.setPageUpdateTime(new Date()); + page.setPageStatus(PageStatus.NORMAL.getValue()); + pageService.insertPage(page); + } + return "redirect:/admin/page"; + } + + /** + * 删除页面 + * + * @param id + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deletePage(@PathVariable("id") Integer id) { + //调用service批量删除 + pageService.deletePage(id); + return "redirect:/admin/page"; + } + + + /** + * 编辑页面页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editPageView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + Page page = pageService.getPageById(id); + modelAndView.addObject("page", page); + + modelAndView.setViewName("Admin/Page/edit"); + return modelAndView; + } + + + /** + * 编辑页面提交 + * + * @param page + * @return + */ + @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) + public String editPageSubmit(Page page) { + Page checkPage = pageService.getPageByKey(null, page.getPageKey()); + //判断别名是否存在且不是这篇文章 + if (Objects.equals(checkPage.getPageId(), page.getPageId())) { + page.setPageUpdateTime(new Date()); + pageService.updatePage(page); + } + return "redirect:/admin/page"; + } + + +} + + + diff --git a/src/java/controller/admin/BackTagController.java b/src/java/controller/admin/BackTagController.java new file mode 100644 index 0000000..4ec4051 --- /dev/null +++ b/src/java/controller/admin/BackTagController.java @@ -0,0 +1,105 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.TagService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import java.util.List; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/tag") +public class BackTagController { + + @Autowired + private ArticleService articleService; + + @Autowired + private TagService tagService; + + /** + * 后台标签列表显示 + * @return + */ + @RequestMapping(value = "") + public ModelAndView index() { + ModelAndView modelandview = new ModelAndView(); + List tagList = tagService.listTagWithCount(); + modelandview.addObject("tagList",tagList); + + modelandview.setViewName("Admin/Tag/index"); + return modelandview; + + } + + + /** + * 后台添加分类页面显示 + * + * @param tag + * @return + */ + @RequestMapping(value = "/insertSubmit",method = RequestMethod.POST) + public String insertTagSubmit(Tag tag) { + tagService.insertTag(tag); + return "redirect:/admin/tag"; + } + + /** + * 删除标签 + * + * @param id 标签ID + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deleteTag(@PathVariable("id") Integer id) { + Integer count = articleService.countArticleByTagId(id); + if (count == 0) { + tagService.deleteTag(id); + } + return "redirect:/admin/tag"; + } + + /** + * 编辑标签页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editTagView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + Tag tag = tagService.getTagById(id); + modelAndView.addObject("tag",tag); + + List tagList = tagService.listTagWithCount(); + modelAndView.addObject("tagList",tagList); + + modelAndView.setViewName("Admin/Tag/edit"); + return modelAndView; + } + + + /** + * 编辑标签提交 + * + * @param tag + * @return + */ + @RequestMapping(value = "/editSubmit",method = RequestMethod.POST) + public String editTagSubmit(Tag tag) { + tagService.updateTag(tag); + return "redirect:/admin/tag"; + } +} diff --git a/src/java/controller/admin/BackUserController.java b/src/java/controller/admin/BackUserController.java new file mode 100644 index 0000000..852b438 --- /dev/null +++ b/src/java/controller/admin/BackUserController.java @@ -0,0 +1,176 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + + +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import com.liuyanzhao.ssm.blog.service.UserService; +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * @author liuyanzhao + */ +@Controller +@RequestMapping("/admin/user") +public class BackUserController { + + @Autowired + private UserService userService; + + /** + * 后台用户列表显示 + * + * @return + */ + @RequestMapping(value = "") + public ModelAndView userList() { + ModelAndView modelandview = new ModelAndView(); + + List userList = userService.listUser(); + modelandview.addObject("userList", userList); + + modelandview.setViewName("Admin/User/index"); + return modelandview; + + } + + /** + * 后台添加用户页面显示 + * + * @return + */ + @RequestMapping(value = "/insert") + public ModelAndView insertUserView() { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("Admin/User/insert"); + return modelAndView; + } + + /** + * 检查用户名是否存在 + * + * @param request + * @return + */ + @RequestMapping(value = "/checkUserName", method = RequestMethod.POST, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public String checkUserName(HttpServletRequest request) { + Map map = new HashMap(); + String username = request.getParameter("username"); + User user = userService.getUserByName(username); + int id = Integer.valueOf(request.getParameter("id")); + //用户名已存在,但不是当前用户(编辑用户的时候,不提示) + if (user != null) { + if (user.getUserId() != id) { + map.put("code", 1); + map.put("msg", "用户名已存在!"); + } + } else { + map.put("code", 0); + map.put("msg", ""); + } + String result = new JSONObject(map).toString(); + return result; + } + + /** + * 检查Email是否存在 + * + * @param request + * @return + */ + @RequestMapping(value = "/checkUserEmail", method = RequestMethod.POST, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public String checkUserEmail(HttpServletRequest request) { + Map map = new HashMap(); + String email = request.getParameter("email"); + User user = userService.getUserByEmail(email); + int id = Integer.valueOf(request.getParameter("id")); + //用户名已存在,但不是当前用户(编辑用户的时候,不提示) + if (user != null) { + if (user.getUserId() != id) { + map.put("code", 1); + map.put("msg", "电子邮箱已存在!"); + } + } else { + map.put("code", 0); + map.put("msg", ""); + } + String result = new JSONObject(map).toString(); + return result; + } + + + /** + * 后台添加用户页面提交 + * + * @param user + * @return + */ + @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) + public String insertUserSubmit(User user) { + User user2 = userService.getUserByName(user.getUserName()); + User user3 = userService.getUserByEmail(user.getUserEmail()); + if (user2 == null && user3 == null) { + user.setUserRegisterTime(new Date()); + user.setUserStatus(1); + user.setUserRole(UserRole.USER.getValue()); + userService.insertUser(user); + } + return "redirect:/admin/user"; + } + + /** + * 删除用户 + * + * @param id + * @return + */ + @RequestMapping(value = "/delete/{id}") + public String deleteUser(@PathVariable("id") Integer id) { + userService.deleteUser(id); + return "redirect:/admin/user"; + } + + /** + * 编辑用户页面显示 + * + * @param id + * @return + */ + @RequestMapping(value = "/edit/{id}") + public ModelAndView editUserView(@PathVariable("id") Integer id) { + ModelAndView modelAndView = new ModelAndView(); + + User user = userService.getUserById(id); + modelAndView.addObject("user", user); + + modelAndView.setViewName("Admin/User/edit"); + return modelAndView; + } + + + /** + * 编辑用户提交 + * + * @param user + * @return + */ + @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) + public String editUserSubmit(User user) { + userService.updateUser(user); + return "redirect:/admin/user"; + } + +} diff --git a/src/java/controller/admin/UploadFileController.java b/src/java/controller/admin/UploadFileController.java new file mode 100644 index 0000000..7431f47 --- /dev/null +++ b/src/java/controller/admin/UploadFileController.java @@ -0,0 +1,93 @@ +package com.liuyanzhao.ssm.blog.controller.admin; + +import com.liuyanzhao.ssm.blog.dto.JsonResult; +import com.liuyanzhao.ssm.blog.dto.UploadFileVO; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + + +import java.io.File; +import java.io.IOException; +import java.util.Calendar; + +/** + * @author liuyanzhao + */ +@Slf4j +@RestController +@RequestMapping("/admin/upload") +public class UploadFileController { + + /** + * 文件保存目录,物理路径 + */ +// public final String rootPath = "/Users/liuyanzhao/Documents/uploads"; + public final String rootPath = "D:\\uploads"; + + public final String allowSuffix = ".bmp.jpg.jpeg.png.gif.pdf.doc.zip.rar.gz"; + + /** + * 上传文件 + * + * @param file + * @return + * @throws IOException + */ + @RequestMapping(value = "/img", method = RequestMethod.POST) + public JsonResult uploadFile(@RequestParam("file") MultipartFile file) { + + //1.文件后缀过滤,只允许部分后缀 + //文件的完整名称,如spring.jpeg + String filename = file.getOriginalFilename(); + //文件名,如spring + String name = filename.substring(0, filename.indexOf(".")); + //文件后缀,如.jpeg + String suffix = filename.substring(filename.lastIndexOf(".")); + + if (allowSuffix.indexOf(suffix) == -1) { + return new JsonResult().fail("不允许上传该后缀的文件!"); + } + + + //2.创建文件目录 + //创建年月文件夹 + Calendar date = Calendar.getInstance(); + File dateDirs = new File(date.get(Calendar.YEAR) + + File.separator + (date.get(Calendar.MONTH) + 1)); + + //目标文件 + File descFile = new File(rootPath + File.separator + dateDirs + File.separator + filename); + int i = 1; + //若文件存在重命名 + String newFilename = filename; + while (descFile.exists()) { + newFilename = name + "(" + i + ")" + suffix; + String parentPath = descFile.getParent(); + descFile = new File(parentPath + File.separator + newFilename); + i++; + } + //判断目标文件所在的目录是否存在 + if (!descFile.getParentFile().exists()) { + //如果目标文件所在的目录不存在,则创建父目录 + descFile.getParentFile().mkdirs(); + } + + //3.存储文件 + //将内存中的数据写入磁盘 + try { + file.transferTo(descFile); + } catch (Exception e) { + e.printStackTrace(); + log.error("上传失败,cause:{}", e); + } + //完整的url + String fileUrl = "/uploads/" + dateDirs + "/" + newFilename; + + //4.返回URL + UploadFileVO uploadFileVO = new UploadFileVO(); + uploadFileVO.setTitle(filename); + uploadFileVO.setSrc(fileUrl); + return new JsonResult().ok(uploadFileVO); + } +} \ No newline at end of file diff --git a/src/java/controller/home/ArticleController.java b/src/java/controller/home/ArticleController.java new file mode 100644 index 0000000..38c78ff --- /dev/null +++ b/src/java/controller/home/ArticleController.java @@ -0,0 +1,134 @@ +package com.liuyanzhao.ssm.blog.controller.home; + + +import com.alibaba.fastjson.JSON; + +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.service.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 文章的controller + * + * @author 言曌 + * @date 2017/8/24 + */ +@Controller +public class ArticleController { + + @Autowired + private ArticleService articleService; + + @Autowired + private CommentService commentService; + + @Autowired + private UserService userService; + + @Autowired + private TagService tagService; + + @Autowired + private CategoryService categoryService; + + /** + * 文章详情页显示 + * + * @param articleId 文章ID + * @return modelAndView + */ + @RequestMapping(value = "/article/{articleId}") + public String getArticleDetailPage(@PathVariable("articleId") Integer articleId, Model model) { + + //文章信息,分类,标签,作者,评论 + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), articleId); + if (article == null) { + return "Home/Error/404"; + } + + //用户信息 + User user = userService.getUserById(article.getArticleUserId()); + article.setUser(user); + + //文章信息 + model.addAttribute("article", article); + + //评论列表 + List commentList = commentService.listCommentByArticleId(articleId); + model.addAttribute("commentList", commentList); + + //相关文章 + List categoryIds = articleService.listCategoryIdByArticleId(articleId); + List
similarArticleList = articleService.listArticleByCategoryIds(categoryIds, 5); + model.addAttribute("similarArticleList", similarArticleList); + + //猜你喜欢 + List
mostViewArticleList = articleService.listArticleByViewCount(5); + model.addAttribute("mostViewArticleList", mostViewArticleList); + + //获取下一篇文章 + Article afterArticle = articleService.getAfterArticle(articleId); + model.addAttribute("afterArticle", afterArticle); + + //获取上一篇文章 + Article preArticle = articleService.getPreArticle(articleId); + model.addAttribute("preArticle", preArticle); + + //侧边栏 + //标签列表显示 + List allTagList = tagService.listTag(); + model.addAttribute("allTagList", allTagList); + //获得随机文章 + List
randomArticleList = articleService.listRandomArticle(8); + model.addAttribute("randomArticleList", randomArticleList); + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + + return "Home/Page/articleDetail"; + } + + /** + * 点赞增加 + * + * @param id 文章ID + * @return 点赞量数量 + */ + @RequestMapping(value = "/article/like/{id}", method = {RequestMethod.POST}, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public String increaseLikeCount(@PathVariable("id") Integer id) { + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), id); + Integer articleCount = article.getArticleLikeCount() + 1; + article.setArticleLikeCount(articleCount); + articleService.updateArticle(article); + return JSON.toJSONString(articleCount); + } + + /** + * 文章访问量数增加 + * + * @param id 文章ID + * @return 访问量数量 + */ + @RequestMapping(value = "/article/view/{id}", method = {RequestMethod.POST}, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public String increaseViewCount(@PathVariable("id") Integer id) { + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), id); + Integer articleCount = article.getArticleViewCount() + 1; + article.setArticleViewCount(articleCount); + articleService.updateArticle(article); + return JSON.toJSONString(articleCount); + } + + +} diff --git a/src/java/controller/home/CategoryController.java b/src/java/controller/home/CategoryController.java new file mode 100644 index 0000000..310c361 --- /dev/null +++ b/src/java/controller/home/CategoryController.java @@ -0,0 +1,83 @@ +package com.liuyanzhao.ssm.blog.controller.home; + + +import com.github.pagehelper.PageInfo; + +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; + + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Category; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CategoryService; +import com.liuyanzhao.ssm.blog.service.TagService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; + + +/** + * 文章分类目录的controller + * + * @author 言曌 + * @date 2017/8/24 + */ +@Controller +public class CategoryController { + + @Autowired + private CategoryService categoryService; + + @Autowired + private ArticleService articleService; + + @Autowired + private TagService tagService; + + /** + * 根据分类查询文章 + * + * @param cateId 分类ID + * @return 模板 + */ + @RequestMapping("/category/{cateId}") + public String getArticleListByCategory(@PathVariable("cateId") Integer cateId, + @RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + Model model) { + + //该分类信息 + Category category = categoryService.getCategoryById(cateId); + if (category == null) { + return "redirect:/404"; + } + model.addAttribute("category", category); + + //文章列表 + HashMap criteria = new HashMap<>(2); + criteria.put("categoryId", cateId); + criteria.put("status", ArticleStatus.PUBLISH.getValue()); + PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", articlePageInfo); + + //侧边栏 + //标签列表显示 + List allTagList = tagService.listTag(); + model.addAttribute("allTagList", allTagList); + //获得随机文章 + List
randomArticleList = articleService.listRandomArticle(8); + model.addAttribute("randomArticleList", randomArticleList); + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + model.addAttribute("pageUrlPrefix", "/category/"+cateId+"?pageIndex"); + return "Home/Page/articleListByCategory"; + } + + +} diff --git a/src/java/controller/home/CommentController.java b/src/java/controller/home/CommentController.java new file mode 100644 index 0000000..9e76d92 --- /dev/null +++ b/src/java/controller/home/CommentController.java @@ -0,0 +1,85 @@ +package com.liuyanzhao.ssm.blog.controller.home; + +import cn.hutool.http.HtmlUtil; +import com.liuyanzhao.ssm.blog.dto.JsonResult; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.enums.Role; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CommentService; +import com.liuyanzhao.ssm.blog.util.MyUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.Date; +import java.util.Objects; + +/** + * @author 言曌 + * @date 2017/9/10 + */ + +@Controller +@RestController +public class CommentController { + @Autowired + private CommentService commentService; + + @Autowired + private ArticleService articleService; + + /** + * 添加评论 + * + * @param request + * @param comment + */ + @RequestMapping(value = "/comment", method = {RequestMethod.POST}) + public JsonResult insertComment(HttpServletRequest request, Comment comment, HttpSession session) { + User user = (User) session.getAttribute("user"); + if (user == null) { + return new JsonResult().fail("请先登录"); + } + Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId()); + if (article == null) { + return new JsonResult().fail("文章不存在"); + } + + + //添加评论 + comment.setCommentUserId(user.getUserId()); + comment.setCommentCreateTime(new Date()); + comment.setCommentIp(MyUtils.getIpAddr(request)); + if (Objects.equals(user.getUserId(), article.getArticleUserId())) { + comment.setCommentRole(Role.OWNER.getValue()); + } else { + comment.setCommentRole(Role.VISITOR.getValue()); + } + comment.setCommentAuthorAvatar(user.getUserAvatar()); + + //过滤字符,防止XSS攻击 + comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent())); + + comment.setCommentAuthorName(user.getUserNickname()); + comment.setCommentAuthorEmail(user.getUserEmail()); + comment.setCommentAuthorUrl(user.getUserUrl()); + try { + commentService.insertComment(comment); + //更新文章的评论数 + articleService.updateCommentCount(article.getArticleId()); + } catch (Exception e) { + e.printStackTrace(); + return new JsonResult().fail(); + } + return new JsonResult().ok(); + } + + +} diff --git a/src/java/controller/home/IndexController.java b/src/java/controller/home/IndexController.java new file mode 100644 index 0000000..aec1455 --- /dev/null +++ b/src/java/controller/home/IndexController.java @@ -0,0 +1,124 @@ +package com.liuyanzhao.ssm.blog.controller.home; + +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.entity.Link; + +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.enums.LinkStatus; +import com.liuyanzhao.ssm.blog.enums.NoticeStatus; + +import com.liuyanzhao.ssm.blog.entity.*; +import com.liuyanzhao.ssm.blog.service.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; + +/** + * 用户的controller + * + * @author 言曌 + * @date 2017/8/24 + */ +@Controller +public class IndexController { + + @Autowired + private ArticleService articleService; + + @Autowired + private LinkService linkService; + + @Autowired + private NoticeService noticeService; + + @Autowired + private TagService tagService; + + @Autowired + private CommentService commentService; + + @RequestMapping(value = {"/", "/article"}) + public String index(@RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, Model model) { + HashMap criteria = new HashMap<>(1); + criteria.put("status", ArticleStatus.PUBLISH.getValue()); + //文章列表 + PageInfo
articleList = articleService.pageArticle(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", articleList); + + //公告 + List noticeList = noticeService.listNotice(NoticeStatus.NORMAL.getValue()); + model.addAttribute("noticeList", noticeList); + //友情链接 + List linkList = linkService.listLink(LinkStatus.NORMAL.getValue()); + model.addAttribute("linkList", linkList); + + //侧边栏显示 + //标签列表显示 + List allTagList = tagService.listTag(); + model.addAttribute("allTagList", allTagList); + //最新评论 + List recentCommentList = commentService.listRecentComment(null, 10); + model.addAttribute("recentCommentList", recentCommentList); + model.addAttribute("pageUrlPrefix", "/article?pageIndex"); + return "Home/index"; + } + + @RequestMapping(value = "/search") + public String search( + @RequestParam("keywords") String keywords, + @RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, Model model) { + //文章列表 + HashMap criteria = new HashMap<>(2); + criteria.put("status", ArticleStatus.PUBLISH.getValue()); + criteria.put("keywords", keywords); + PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", articlePageInfo); + + //侧边栏显示 + //标签列表显示 + List allTagList = tagService.listTag(); + model.addAttribute("allTagList", allTagList); + //获得随机文章 + List
randomArticleList = articleService.listRandomArticle(8); + model.addAttribute("randomArticleList", randomArticleList); + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + //最新评论 + List recentCommentList = commentService.listRecentComment(null, 10); + model.addAttribute("recentCommentList", recentCommentList); + model.addAttribute("pageUrlPrefix", "/search?keywords=" + keywords + "&pageIndex"); + return "Home/Page/search"; + } + + @RequestMapping("/404") + public String NotFound(@RequestParam(required = false) String message, Model model) { + model.addAttribute("message", message); + return "Home/Error/404"; + } + + + @RequestMapping("/403") + public String Page403(@RequestParam(required = false) String message, Model model) { + model.addAttribute("message", message); + return "Home/Error/403"; + } + + @RequestMapping("/500") + public String ServerError(@RequestParam(required = false) String message, Model model) { + model.addAttribute("message", message); + return "Home/Error/500"; + } + + +} + + + + diff --git a/src/java/controller/home/LinkController.java b/src/java/controller/home/LinkController.java new file mode 100644 index 0000000..f0f07f0 --- /dev/null +++ b/src/java/controller/home/LinkController.java @@ -0,0 +1,49 @@ +package com.liuyanzhao.ssm.blog.controller.home; + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Link; +import com.liuyanzhao.ssm.blog.enums.LinkStatus; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.LinkService; +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 org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.Date; +import java.util.List; + +/** + * + * @author 言曌 + * @date 2017/9/5 + */ +@Controller +public class LinkController { + @Autowired + private LinkService linkService; + + @Autowired + private ArticleService articleService; + + @RequestMapping("/applyLink") + public String applyLinkView(Model model) { + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/applyLink"; + } + + + @RequestMapping(value = "/applyLinkSubmit",method = {RequestMethod.POST}, produces = {"text/plain;charset=UTF-8"}) + @ResponseBody + public void applyLinkSubmit(Link link) { + link.setLinkStatus(LinkStatus.HIDDEN.getValue()); + link.setLinkCreateTime(new Date()); + link.setLinkUpdateTime(new Date()); + linkService.insertLink(link); + } +} diff --git a/src/java/controller/home/NoticeController.java b/src/java/controller/home/NoticeController.java new file mode 100644 index 0000000..1d8e64b --- /dev/null +++ b/src/java/controller/home/NoticeController.java @@ -0,0 +1,46 @@ +package com.liuyanzhao.ssm.blog.controller.home; + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Notice; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.NoticeService; +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; + +/** + * @author liuyanzhao + */ +@Controller +public class NoticeController { + + @Autowired + private NoticeService noticeService; + + @Autowired + private ArticleService articleService; + + /** + * 公告详情页显示 + * + * @param noticeId + * @return + */ + @RequestMapping(value = "/notice/{noticeId}") + public String NoticeDetailView(@PathVariable("noticeId") Integer noticeId, + Model model) { + //公告内容和信息显示 + Notice notice = noticeService.getNoticeById(noticeId); + model.addAttribute("notice",notice); + + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/noticeDetail"; + } +} diff --git a/src/java/controller/home/PageController.java b/src/java/controller/home/PageController.java new file mode 100644 index 0000000..5f4ee19 --- /dev/null +++ b/src/java/controller/home/PageController.java @@ -0,0 +1,119 @@ +package com.liuyanzhao.ssm.blog.controller.home; + + + + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Category; +import com.liuyanzhao.ssm.blog.entity.Page; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.CategoryService; +import com.liuyanzhao.ssm.blog.service.PageService; +import com.liuyanzhao.ssm.blog.service.TagService; +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; + +/** + * @author 言曌 + * @date 2017/9/7 + */ +@Controller +public class PageController { + + @Autowired + private PageService pageService; + + @Autowired + private ArticleService articleService; + + @Autowired + private CategoryService categoryService; + + + @Autowired + private TagService tagService; + + /** + * 页面详情页面 + * + * @param key + * @return + */ + @RequestMapping(value = "/{key}") + public String pageDetail(@PathVariable("key") String key, Model model) { + Page page = pageService.getPageByKey(1, key); + if (page == null) { + return "redirect:/404"; + } + model.addAttribute("page", page); + + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/page"; + + } + + + /** + * 文章归档页面显示 + * + * @return + */ + @RequestMapping(value = "/articleFile") + public String articleFile(Model model) { + List
articleList = articleService.listAllNotWithContent(); + model.addAttribute("articleList", articleList); + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(10); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/articleFile"; + } + + /** + * 站点地图显示 + * + * @return + */ + @RequestMapping(value = "/map") + public String siteMap(Model model) { + //文章显示 + List
articleList = articleService.listAllNotWithContent(); + model.addAttribute("articleList", articleList); + //分类显示 + List categoryList = categoryService.listCategory(); + model.addAttribute("categoryList", categoryList); + //标签显示 + List tagList = tagService.listTag(); + model.addAttribute("tagList", tagList); + + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(10); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/siteMap"; + } + + /** + * 留言板 + * + * @return + */ + @RequestMapping(value = "/message") + public String message(Model model) { + + //侧边栏显示 + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + return "Home/Page/message"; + } +} diff --git a/src/java/controller/home/TagController.java b/src/java/controller/home/TagController.java new file mode 100644 index 0000000..cfa3c3c --- /dev/null +++ b/src/java/controller/home/TagController.java @@ -0,0 +1,72 @@ +package com.liuyanzhao.ssm.blog.controller.home; + +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; + + +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.TagService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; + +/** + * @author 言曌 + * @date 2017/9/2 + */ +@Controller +public class TagController { + + @Autowired + private TagService tagService; + + @Autowired + private ArticleService articleService; + + /** + * 根据标签查询文章 + * + * @param tagId 标签ID + * @return 模板 + */ + @RequestMapping("/tag/{tagId}") + public String getArticleListByTag(@PathVariable("tagId") Integer tagId, + @RequestParam(required = false, defaultValue = "1") Integer pageIndex, + @RequestParam(required = false, defaultValue = "10") Integer pageSize, + Model model) { + //该标签信息 + Tag tag = tagService.getTagById(tagId); + if (tag == null) { + return "redirect:/404"; + } + model.addAttribute("tag", tag); + + //文章列表 + HashMap criteria = new HashMap<>(2); + criteria.put("tagId", tagId); + criteria.put("status", ArticleStatus.PUBLISH.getValue()); + PageInfo
articlePageInfo = articleService.pageArticle(pageIndex, pageSize, criteria); + model.addAttribute("pageInfo", articlePageInfo); + + //侧边栏 + //标签列表显示 + List allTagList = tagService.listTag(); + model.addAttribute("allTagList", allTagList); + //获得随机文章 + List
randomArticleList = articleService.listRandomArticle(8); + model.addAttribute("randomArticleList", randomArticleList); + //获得热评文章 + List
mostCommentArticleList = articleService.listArticleByCommentCount(8); + model.addAttribute("mostCommentArticleList", mostCommentArticleList); + model.addAttribute("pageUrlPrefix", "/tag/"+tagId+"?pageIndex"); + + return "Home/Page/articleListByTag"; + } + +} diff --git a/src/java/dto/ArticleParam.java b/src/java/dto/ArticleParam.java new file mode 100644 index 0000000..835e09f --- /dev/null +++ b/src/java/dto/ArticleParam.java @@ -0,0 +1,32 @@ +package com.liuyanzhao.ssm.blog.dto; + +import lombok.Data; + +import java.util.List; + +/** + * @author 言曌 + * @date 2018/11/25 下午3:56 + */ +@Data +public class ArticleParam { + + private Integer articleId; + + private String articleTitle; + + private String articleContent; + + private Integer articleParentCategoryId; + + private Integer articleChildCategoryId; + + private Integer articleOrder; + + private Integer articleStatus; + + private String articleThumbnail; + + private List articleTagIds; + +} diff --git a/src/java/dto/JsonResult.java b/src/java/dto/JsonResult.java new file mode 100644 index 0000000..fda76fa --- /dev/null +++ b/src/java/dto/JsonResult.java @@ -0,0 +1,56 @@ +package com.liuyanzhao.ssm.blog.dto; + +import lombok.Data; + +/** + * @author 言曌 + * @date 2017/11/30 下午7:04 + */ + +@Data +public class JsonResult { + + + /** + * 错误码 + */ + private Integer code; + + /** + * 提示信息 + */ + private String msg; + + /** + * 返回的具体内容 + */ + private T data; + + + public JsonResult(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public JsonResult() { + } + + + public JsonResult fail() { + return new JsonResult(1, "操作失败", null); + } + + public JsonResult fail(String msg) { + return new JsonResult(1, msg, null); + } + + public JsonResult ok() { + return new JsonResult(0, "操作成功", null); + } + + + public JsonResult ok(T data) { + return new JsonResult(0, "操作成功", data); + } +} diff --git a/src/java/dto/ResultVO.java b/src/java/dto/ResultVO.java new file mode 100644 index 0000000..ce93cb3 --- /dev/null +++ b/src/java/dto/ResultVO.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.dto; + +import lombok.Data; + +/** + * @author 言曌 + * @date 2017/11/30 下午7:04 + */ + +@Data +public class ResultVO { + + /** + * 错误码 + */ + private Integer code; + + /** + * 提示信息 + */ + private String msg; + + /** + * 返回的具体内容 + */ + private T data; + + + public ResultVO(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public ResultVO() { + } +} diff --git a/src/java/dto/UploadFileVO.java b/src/java/dto/UploadFileVO.java new file mode 100644 index 0000000..b05fabd --- /dev/null +++ b/src/java/dto/UploadFileVO.java @@ -0,0 +1,17 @@ +package com.liuyanzhao.ssm.blog.dto; + +import lombok.Data; + +/** + * @author 言曌 + * @date 2017/11/30 下午7:41 + */ + +@Data +public class UploadFileVO { + + private String src; + + private String title; + +} diff --git a/src/java/entity/Article.java b/src/java/entity/Article.java new file mode 100644 index 0000000..2eb44b8 --- /dev/null +++ b/src/java/entity/Article.java @@ -0,0 +1,52 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * @author liuyanzhao + * 定义了一个名为 Article 的 Java 类,该类实现了 Serializable 接口 + */ +@Data +public class Article implements Serializable{ + + private static final long serialVersionUID = 5207865247400761539L; + + private Integer articleId; + + private Integer articleUserId; + + private String articleTitle; + + private Integer articleViewCount; + + private Integer articleCommentCount; + + private Integer articleLikeCount; + + private Date articleCreateTime; + + private Date articleUpdateTime; + + private Integer articleIsComment; + + private Integer articleStatus; + + private Integer articleOrder; + + private String articleContent; + + private String articleSummary; + + private String articleThumbnail; + + private User user; + + private List tagList; + + private List categoryList; + +} \ No newline at end of file diff --git a/src/java/entity/ArticleCategoryRef.java b/src/java/entity/ArticleCategoryRef.java new file mode 100644 index 0000000..04f19c8 --- /dev/null +++ b/src/java/entity/ArticleCategoryRef.java @@ -0,0 +1,28 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 文章分类关联表 + * + * @author liuyanzhao + */ +@Data +public class ArticleCategoryRef implements Serializable{ + + private static final long serialVersionUID = -6809206515467725995L; + + private Integer articleId; + + private Integer categoryId; + + public ArticleCategoryRef() { + } + + public ArticleCategoryRef(Integer articleId, Integer categoryId) { + this.articleId = articleId; + this.categoryId = categoryId; + } +} \ No newline at end of file diff --git a/src/java/entity/ArticleTagRef.java b/src/java/entity/ArticleTagRef.java new file mode 100644 index 0000000..f775f3a --- /dev/null +++ b/src/java/entity/ArticleTagRef.java @@ -0,0 +1,28 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 文章和标签关联 + * + * @author 言曌 + * @date 2018/11/17 下午5:20 + */ +@Data +public class ArticleTagRef implements Serializable { + private static final long serialVersionUID = -5816783232020910492L; + + private Integer articleId; + + private Integer tagId; + + public ArticleTagRef() { + } + + public ArticleTagRef(Integer articleId, Integer tagId) { + this.articleId = articleId; + this.tagId = tagId; + } +} diff --git a/src/java/entity/Category.java b/src/java/entity/Category.java new file mode 100644 index 0000000..904f767 --- /dev/null +++ b/src/java/entity/Category.java @@ -0,0 +1,64 @@ +package com.liuyanzhao.ssm.blog.entity; + + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author liuyanzhao + */ +@Data +public class Category implements Serializable { + + private static final long serialVersionUID = 6687286913317513141L; + + private Integer categoryId; + + private Integer categoryPid; + + private String categoryName; + + private String categoryDescription; + + private Integer categoryOrder; + + private String categoryIcon; + + /** + * 文章数量(非数据库字段) + */ + private Integer articleCount; + + public Category(Integer categoryId, Integer categoryPid, String categoryName, String categoryDescription, Integer categoryOrder, String categoryIcon,Integer articleCount) { + this.categoryId = categoryId; + this.categoryPid = categoryPid; + this.categoryName = categoryName; + this.categoryDescription = categoryDescription; + this.categoryOrder = categoryOrder; + this.categoryIcon = categoryIcon; + this.articleCount = articleCount; + } + + public Category(Integer categoryId, String categoryName) { + this.categoryId = categoryId; + this.categoryName = categoryName; + } + + public Category(Integer categoryId) { + this.categoryId = categoryId; + } + + public Category() {} + + /** + * 未分类 + * + * @return 分类 + */ + public static Category Default() { + return new Category(100000000, "未分类"); + } + + +} diff --git a/src/java/entity/Comment.java b/src/java/entity/Comment.java new file mode 100644 index 0000000..31f1779 --- /dev/null +++ b/src/java/entity/Comment.java @@ -0,0 +1,55 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * 文章评论 + * + * @author liuyanzhao + */ +@Data +public class Comment implements Serializable { + + private static final long serialVersionUID = -1038897351672911219L; + private Integer commentId; + + private Integer commentPid; + + private String commentPname; + + private Integer commentArticleId; + + private String commentAuthorName; + + private String commentAuthorEmail; + + private String commentAuthorUrl; + + private String commentAuthorAvatar; + + private String commentContent; + + private String commentAgent; + + private String commentIp; + + private Date commentCreateTime; + + /** + * 角色(管理员1,访客0) + */ + private Integer commentRole; + + /** + * 评论用户ID + */ + private Integer commentUserId; + + /** + * 非数据库字段 + */ + private Article article; +} \ No newline at end of file diff --git a/src/java/entity/Link.java b/src/java/entity/Link.java new file mode 100644 index 0000000..0af817f --- /dev/null +++ b/src/java/entity/Link.java @@ -0,0 +1,39 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author liuyanzhao + */ +@Data +public class Link implements Serializable{ + + + private static final long serialVersionUID = -259829372268790508L; + + private Integer linkId; + + private String linkUrl; + + private String linkName; + + private String linkImage; + + private String linkDescription; + + private String linkOwnerNickname; + + private String linkOwnerContact; + + private Date linkUpdateTime; + + private Date linkCreateTime; + + private Integer linkOrder; + + private Integer linkStatus; + +} \ No newline at end of file diff --git a/src/java/entity/Menu.java b/src/java/entity/Menu.java new file mode 100644 index 0000000..228a366 --- /dev/null +++ b/src/java/entity/Menu.java @@ -0,0 +1,25 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author liuyanzhao + */ +@Data +public class Menu implements Serializable { + private static final long serialVersionUID = 489914127235951698L; + private Integer menuId; + + private String menuName; + + private String menuUrl; + + private Integer menuLevel; + + private String menuIcon; + + private Integer menuOrder; + +} \ No newline at end of file diff --git a/src/java/entity/Notice.java b/src/java/entity/Notice.java new file mode 100644 index 0000000..7f01130 --- /dev/null +++ b/src/java/entity/Notice.java @@ -0,0 +1,28 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author liuyanzhao + */ +@Data +public class Notice implements Serializable{ + + private static final long serialVersionUID = -4901560494243593100L; + private Integer noticeId; + + private String noticeTitle; + + private String noticeContent; + + private Date noticeCreateTime; + + private Date noticeUpdateTime; + + private Integer noticeStatus; + + private Integer noticeOrder; +} \ No newline at end of file diff --git a/src/java/entity/Options.java b/src/java/entity/Options.java new file mode 100644 index 0000000..ad69ad4 --- /dev/null +++ b/src/java/entity/Options.java @@ -0,0 +1,41 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author liuyanzhao + */ +@Data +public class Options implements Serializable{ + private static final long serialVersionUID = -776792869602511933L; + private Integer optionId; + + private String optionSiteTitle; + + private String optionSiteDescrption; + + private String optionMetaDescrption; + + private String optionMetaKeyword; + + private String optionAboutsiteAvatar; + + private String optionAboutsiteTitle; + + private String optionAboutsiteContent; + + private String optionAboutsiteWechat; + + private String optionAboutsiteQq; + + private String optionAboutsiteGithub; + + private String optionAboutsiteWeibo; + + private String optionTongji; + + private Integer optionStatus; + +} \ No newline at end of file diff --git a/src/java/entity/Page.java b/src/java/entity/Page.java new file mode 100644 index 0000000..16ddd38 --- /dev/null +++ b/src/java/entity/Page.java @@ -0,0 +1,33 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author liuyanzhao + */ +@Data +public class Page implements Serializable{ + + private static final long serialVersionUID = 3927496662110298634L; + private Integer pageId; + + private String pageKey; + + private String pageTitle; + + private String pageContent; + + private Date pageCreateTime; + + private Date pageUpdateTime; + + private Integer pageViewCount; + + private Integer pageCommentCount; + + private Integer pageStatus; + +} \ No newline at end of file diff --git a/src/java/entity/Tag.java b/src/java/entity/Tag.java new file mode 100644 index 0000000..10aa8ba --- /dev/null +++ b/src/java/entity/Tag.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author liuyanzhao + */ +@Data +public class Tag implements Serializable{ + private static final long serialVersionUID = 605449151900057035L; + private Integer tagId; + + private String tagName; + + private String tagDescription; + + /** + * 文章数量(不是数据库字段) + */ + private Integer articleCount; + + public Tag() { + } + + public Tag(Integer tagId) { + this.tagId = tagId; + } + + public Tag(Integer tagId, String tagName, String tagDescription, Integer articleCount) { + this.tagId = tagId; + this.tagName = tagName; + this.tagDescription = tagDescription; + this.articleCount = articleCount; + } +} \ No newline at end of file diff --git a/src/java/entity/User.java b/src/java/entity/User.java new file mode 100644 index 0000000..326ef3c --- /dev/null +++ b/src/java/entity/User.java @@ -0,0 +1,46 @@ +package com.liuyanzhao.ssm.blog.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author liuyanzhao + */ +@Data +public class User implements Serializable{ + private static final long serialVersionUID = -4415517704211731385L; + private Integer userId; + + private String userName; + + private String userPass; + + private String userNickname; + + private String userEmail; + + private String userUrl; + + private String userAvatar; + + private String userLastLoginIp; + + private Date userRegisterTime; + + private Date userLastLoginTime; + + private Integer userStatus; + + /** + * 用户角色:admin/user + */ + private String userRole; + + /** + * 文章数量(不是数据库字段) + */ + private Integer articleCount; + +} \ No newline at end of file diff --git a/src/java/enums/ArticleCommentStatus.java b/src/java/enums/ArticleCommentStatus.java new file mode 100644 index 0000000..0c0718b --- /dev/null +++ b/src/java/enums/ArticleCommentStatus.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum ArticleCommentStatus { + + ALLOW(1, "允许"), + NOT_ALLOW(0, "不允许"); + + private Integer value; + + private String message; + + ArticleCommentStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/ArticleStatus.java b/src/java/enums/ArticleStatus.java new file mode 100644 index 0000000..2e792c5 --- /dev/null +++ b/src/java/enums/ArticleStatus.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum ArticleStatus { + + PUBLISH(1, "已发布"), + DRAFT(0, "草稿"); + + private Integer value; + + private String message; + + ArticleStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/CategoryStatus.java b/src/java/enums/CategoryStatus.java new file mode 100644 index 0000000..1032190 --- /dev/null +++ b/src/java/enums/CategoryStatus.java @@ -0,0 +1,39 @@ +package com.liuyanzhao.ssm.blog.enums; + +import lombok.Data; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum CategoryStatus { + + NORMAL(1, "正常"), + HIDDEN(0, "隐藏"); + + private Integer value; + + private String message; + + CategoryStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/LinkStatus.java b/src/java/enums/LinkStatus.java new file mode 100644 index 0000000..f73f5b4 --- /dev/null +++ b/src/java/enums/LinkStatus.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum LinkStatus { + + NORMAL(1, "显示"), + HIDDEN(0, "隐藏"); + + private Integer value; + + private String message; + + LinkStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/MenuLevel.java b/src/java/enums/MenuLevel.java new file mode 100644 index 0000000..f21f2c7 --- /dev/null +++ b/src/java/enums/MenuLevel.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum MenuLevel { + + TOP_MENU(1, "顶部菜单"), + MAIN_MENU(2, "主体菜单"); + + private Integer value; + + private String message; + + MenuLevel(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/NoticeStatus.java b/src/java/enums/NoticeStatus.java new file mode 100644 index 0000000..fb1e694 --- /dev/null +++ b/src/java/enums/NoticeStatus.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum NoticeStatus { + + NORMAL(1, "显示"), + HIDDEN(0, "隐藏"); + + private Integer value; + + private String message; + + NoticeStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/PageStatus.java b/src/java/enums/PageStatus.java new file mode 100644 index 0000000..fb2c98f --- /dev/null +++ b/src/java/enums/PageStatus.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum PageStatus { + + NORMAL(1, "显示"), + HIDDEN(0, "隐藏"); + + private Integer value; + + private String message; + + PageStatus(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/Role.java b/src/java/enums/Role.java new file mode 100644 index 0000000..a95b01f --- /dev/null +++ b/src/java/enums/Role.java @@ -0,0 +1,37 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * @author 言曌 + * @date 2018/11/17 下午4:47 + */ + +public enum Role { + + OWNER(1, "博主"), + VISITOR(0, "其他用户"); + + private Integer value; + + private String message; + + Role(Integer value, String message) { + this.value = value; + this.message = message; + } + + public Integer getValue() { + return value; + } + + public void setValue(Integer value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/enums/UserRole.java b/src/java/enums/UserRole.java new file mode 100644 index 0000000..3de0d5a --- /dev/null +++ b/src/java/enums/UserRole.java @@ -0,0 +1,40 @@ +package com.liuyanzhao.ssm.blog.enums; + +/** + * 角色枚举 + * + * @author 言曌 + * @date 2021/2/25 2:31 下午 + */ + +public enum UserRole { + + ADMIN("admin", "管理员"), + + USER("user", "用户"); + + private String value; + + private String message; + + UserRole(String value, String message) { + this.value = value; + this.message = message; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/java/interceptor/AdminInterceptor.java b/src/java/interceptor/AdminInterceptor.java new file mode 100644 index 0000000..7c15120 --- /dev/null +++ b/src/java/interceptor/AdminInterceptor.java @@ -0,0 +1,45 @@ +package com.liuyanzhao.ssm.blog.interceptor; + +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.enums.UserRole; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Objects; + + +/** + * @author liuyanzhao + */ +@Component +public class AdminInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws IOException { + //这里可以根据session的用户来判断角色的权限,根据权限来转发不同的页面 + User user = (User) request.getSession().getAttribute("user"); + if (user == null) { + response.sendRedirect("/login"); + return false; + } + if (!Objects.equals(user.getUserRole(), UserRole.ADMIN.getValue())) { + return false; + } + return true; + } + + @Override + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { + + } + + @Override + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { + + } +} + diff --git a/src/java/interceptor/HomeResourceInterceptor.java b/src/java/interceptor/HomeResourceInterceptor.java new file mode 100644 index 0000000..992c1bd --- /dev/null +++ b/src/java/interceptor/HomeResourceInterceptor.java @@ -0,0 +1,87 @@ +package com.liuyanzhao.ssm.blog.interceptor; + +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Options; + +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.enums.LinkStatus; + +import com.liuyanzhao.ssm.blog.entity.Category; +import com.liuyanzhao.ssm.blog.entity.Menu; +import com.liuyanzhao.ssm.blog.service.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author liuyanzhao + */ +@Component +public class HomeResourceInterceptor implements HandlerInterceptor { + @Autowired + private ArticleService articleService; + + @Autowired + private CategoryService categoryService; + + @Autowired + private TagService tagService; + + @Autowired + private LinkService linkService; + + @Autowired + private OptionsService optionsService; + + @Autowired + private MenuService menuService; + + /** + * 在请求处理之前执行,该方法主要是用于准备资源数据的,然后可以把它们当做请求属性放到WebRequest中 + */ + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws IOException { + + // 菜单显示 + List menuList = menuService.listMenu(); + request.setAttribute("menuList", menuList); + + List categoryList = categoryService.listCategory(); + request.setAttribute("allCategoryList", categoryList); + + //获得网站概况 + List siteBasicStatistics = new ArrayList(); + siteBasicStatistics.add(articleService.countArticle(ArticleStatus.PUBLISH.getValue()) + ""); + siteBasicStatistics.add(articleService.countArticleComment() + ""); + siteBasicStatistics.add(categoryService.countCategory() + ""); + siteBasicStatistics.add(tagService.countTag() + ""); + siteBasicStatistics.add(linkService.countLink(LinkStatus.NORMAL.getValue()) + ""); + siteBasicStatistics.add(articleService.countArticleView() + ""); + request.setAttribute("siteBasicStatistics", siteBasicStatistics); + //最后更新的文章 + Article lastUpdateArticle = articleService.getLastUpdateArticle(); + request.setAttribute("lastUpdateArticle", lastUpdateArticle); + + //页脚显示 + //博客基本信息显示(Options) + Options options = optionsService.getOptions(); + request.setAttribute("options", options); + return true; + } + + @Override + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { + } + + @Override + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { + + } +} \ No newline at end of file diff --git a/src/java/interceptor/LoginInterceptor.java b/src/java/interceptor/LoginInterceptor.java new file mode 100644 index 0000000..1201d86 --- /dev/null +++ b/src/java/interceptor/LoginInterceptor.java @@ -0,0 +1,39 @@ +package com.liuyanzhao.ssm.blog.interceptor; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +/** + * @author liuyanzhao + */ +@Component +public class LoginInterceptor extends HandlerInterceptorAdapter { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws IOException { + //这里可以根据session的用户来判断角色的权限,根据权限来转发不同的页面 + if(request.getSession().getAttribute("user") == null) { + response.sendRedirect("/login"); + return false; + } + + return true; + } + + @Override + public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) { + + } + + @Override + public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { + + } +} + diff --git a/src/java/mapper/ArticleCategoryRefMapper.java b/src/java/mapper/ArticleCategoryRefMapper.java new file mode 100644 index 0000000..1fe8379 --- /dev/null +++ b/src/java/mapper/ArticleCategoryRefMapper.java @@ -0,0 +1,70 @@ +package com.liuyanzhao.ssm.blog.mapper; + + +import com.liuyanzhao.ssm.blog.entity.ArticleCategoryRef; +import com.liuyanzhao.ssm.blog.entity.Category; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * 文章分类关联表Mapper + * @author liuyanzhao + */ +@Mapper +public interface ArticleCategoryRefMapper { + + /** + * 添加文章和分类关联记录 + * @param record 关联对象 + * @return 影响行数 + */ + int insert(ArticleCategoryRef record); + + /** + * 根据分类ID删除记录 + * @param categoryId 分类ID + * @return 影响行数 + */ + int deleteByCategoryId(Integer categoryId); + + /** + * 根据文章ID删除记录 + * @param articleId 文章ID + * @return 影响行数 + */ + int deleteByArticleId(Integer articleId); + + /** + * 根据分类ID统计文章数 + * @param categoryId 分类ID + * @return 文章数量 + */ + int countArticleByCategoryId(Integer categoryId); + + + /** + * 根据文章ID查询分类ID + * + * @param articleId 文章ID + * @return 分类ID列表 + */ + List selectCategoryIdByArticleId(Integer articleId); + + /** + * 根据分类ID查询文章ID + * + * @param categoryId 分类ID + * @return 文章ID列表 + */ + List selectArticleIdByCategoryId(Integer categoryId); + + /** + * 根据文章ID获得分类列表 + * + * @param articleId 文章ID + * @return 分类列表 + */ + List listCategoryByArticleId(Integer articleId); + +} \ No newline at end of file diff --git a/src/java/mapper/ArticleMapper.java b/src/java/mapper/ArticleMapper.java new file mode 100644 index 0000000..c6930da --- /dev/null +++ b/src/java/mapper/ArticleMapper.java @@ -0,0 +1,223 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Article; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.HashMap; +import java.util.List; + +/** + * 文章Mapper + * + * @author liuyanzhao + */ +@Mapper +public interface ArticleMapper { + + /** + * 根据ID删除 + * + * @param articleId 文章ID + * @return 影响函数 + */ + Integer deleteById(Integer articleId); + + /** + * 根据用户ID删除 + * + * @param userId 用户ID + * @return 影响函数 + */ + Integer deleteByUserId(Integer userId); + + /** + * 添加文章 + * + * @param article 文章 + * @return 文章 + */ + Integer insert(Article article); + + /** + * 更新文章 + * + * @param article 文章 + * @return 影响行数 + */ + Integer update(Article article); + + /** + * 获得所有的文章 + * + * @param criteria 查询条件 + * @return 文章列表 + */ + List
findAll(HashMap criteria); + + /** + * 文章归档 + * + * @return + */ + List
listAllNotWithContent(); + + /** + * 获取文章总数 + * + * @param status 状态 + * @return 数量 + */ + Integer countArticle(@Param(value = "status") Integer status); + + /** + * 获得留言总数 + * + * @return 数量 + */ + Integer countArticleComment(); + + /** + * 获得浏览量总数 + * + * @return 文章数量 + */ + Integer countArticleView(); + + /** + * 获得所有文章(文章归档) + * + * @return 文章列表 + */ + List
listArticle(); + + /** + * 根据id查询用户信息 + * + * @param status 状态 + * @param id 文章ID + * @return 文章 + */ + Article getArticleByStatusAndId(@Param(value = "status") Integer status, @Param(value = "id") Integer id); + + /** + * 分页操作 + * + * @param status 状态 + * @param pageIndex 从第几页开始 + * @param pageSize 数量 + * @return 文章列表 + */ + @Deprecated + List
pageArticle(@Param(value = "status") Integer status, + @Param(value = "pageIndex") Integer pageIndex, + @Param(value = "pageSize") Integer pageSize); + + + /** + * 获得访问最多的文章(猜你喜欢) + * + * @param limit 查询数量 + * @return 文章列表 + */ + List
listArticleByViewCount(@Param(value = "limit") Integer limit); + + /** + * 获得上一篇文章 + * + * @param id 文章ID + * @return 文章 + */ + Article getAfterArticle(@Param(value = "id") Integer id); + + /** + * 获得下一篇文章 + * + * @param id 文章ID + * @return 文章 + */ + Article getPreArticle(@Param(value = "id") Integer id); + + /** + * 获得随机文章 + * + * @param limit 查询数量 + * @return 文章列表 + */ + List
listRandomArticle(@Param(value = "limit") Integer limit); + + /** + * 热评文章 + * + * @param limit 查询数量 + * @return 文章列表 + */ + List
listArticleByCommentCount(@Param(value = "limit") Integer limit); + + + /** + * 更新文章的评论数 + * + * @param articleId 文章ID + */ + void updateCommentCount(@Param(value = "articleId") Integer articleId); + + /** + * 获得最后更新的记录 + * + * @return 文章 + */ + Article getLastUpdateArticle(); + + /** + * 用户的文章数 + * + * @param id 用户ID + * @return 数量 + */ + Integer countArticleByUser(@Param(value = "id") Integer id); + + /** + * 根据分类ID + * + * @param categoryId 分类ID + * @param limit 查询数量 + * @return 文章列表 + */ + List
findArticleByCategoryId(@Param("categoryId") Integer categoryId, + @Param("limit") Integer limit); + + /** + * 根据分类ID + * + * @param categoryIds 分类ID集合 + * @param limit 查询数量 + * @return 文章列表 + */ + List
findArticleByCategoryIds(@Param("categoryIds") List categoryIds, + @Param("limit") Integer limit); + + /** + * 获得最新文章 + * + * @param limit 查询数量 + * @return 列表 + */ + List
listArticleByLimit(@Param("userId") Integer userId, @Param("limit") Integer limit); + + /** + * 批量删除文章 + * + * @param ids 文章Id列表 + * @return 影响行数 + */ + Integer deleteBatch(@Param("ids") List ids); + + /** + * 获得一个用户的文章id集合 + * + * @param userId + * @return + */ + List listArticleIdsByUserId(Integer userId); +} \ No newline at end of file diff --git a/src/java/mapper/ArticleTagRefMapper.java b/src/java/mapper/ArticleTagRefMapper.java new file mode 100644 index 0000000..6738b20 --- /dev/null +++ b/src/java/mapper/ArticleTagRefMapper.java @@ -0,0 +1,54 @@ +package com.liuyanzhao.ssm.blog.mapper; + + +import com.liuyanzhao.ssm.blog.entity.ArticleTagRef; +import com.liuyanzhao.ssm.blog.entity.Tag; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * 文章标签关联表Mapper + * @author liuyanzhao + */ +@Mapper +public interface ArticleTagRefMapper { + + /** + * 添加文章和标签关联记录 + * @param record 关联对象 + * @return 影响行数 + */ + int insert(ArticleTagRef record); + + /** + * 根据标签ID删除记录 + * @param tagId 标签ID + * @return 影响行数 + */ + int deleteByTagId(Integer tagId); + + /** + * 根据文章ID删除记录 + * @param articleId 文章ID + * @return 影响行数 + */ + int deleteByArticleId(Integer articleId); + + /** + * 根据标签ID统计文章数 + * @param tagId 标签ID + * @return 文章数量 + */ + int countArticleByTagId(Integer tagId); + + /** + * 根据文章获得标签列表 + * + * @param articleId 文章ID + * @return 标签列表 + */ + List listTagByArticleId(Integer articleId); + + +} \ No newline at end of file diff --git a/src/java/mapper/CategoryMapper.java b/src/java/mapper/CategoryMapper.java new file mode 100644 index 0000000..e3f5b15 --- /dev/null +++ b/src/java/mapper/CategoryMapper.java @@ -0,0 +1,79 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Category; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface CategoryMapper { + + + /** + * 添加 + * + * @param category 分类 + * @return 影响行数 + */ + int insert(Category category); + + + /** + * 更新 + * + * @param category 分类 + * @return 影响行数 + */ + int update(Category category); + + /** + * 根据分类id获得分类信息 + * + * @param id ID + * @return 分类 + */ + Category getCategoryById(Integer id); + + + /** + * 删除分类 + * + * @param id 文章ID + */ + int deleteCategory(Integer id); + + /** + * 查询分类总数 + * + * @return 数量 + */ + Integer countCategory(); + + /** + * 获得分类列表 + * + * @return 列表 + */ + List listCategory(); + + /** + * 根据父分类找子分类 + * + * @param id 分类ID + * @return 列表 + */ + List findChildCategory(@Param(value = "id") Integer id); + + /** + * 根据标签名获取标签 + * + * @param name 名称 + * @return 分类 + */ + Category getCategoryByName(String name); +} \ No newline at end of file diff --git a/src/java/mapper/CommentMapper.java b/src/java/mapper/CommentMapper.java new file mode 100644 index 0000000..e8976b3 --- /dev/null +++ b/src/java/mapper/CommentMapper.java @@ -0,0 +1,111 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Comment; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.HashMap; +import java.util.List; + +@Mapper +public interface CommentMapper { + + /** + * 根据ID删除 + * + * @param commentId 评论ID + * @return 影响行数 + */ + int deleteById(Integer commentId); + + /** + * 添加 + * + * @param comment 评论 + * @return 影响行数 + */ + int insert(Comment comment); + + /** + * 根据ID查询 + * + * @param commentId 评论ID + * @return 评论 + */ + Comment getCommentById(Integer commentId); + + /** + * 更新 + * + * @param comment 评论 + * @return 影响行数 + */ + int update(Comment comment); + + /** + * 根据文章id获取评论列表 + * + * @param id ID + * @return 列表 + */ + List listCommentByArticleId(@Param(value = "id") Integer id); + + + /** + * 获得评论列表 + * + * @return 列表 + */ + List listComment(HashMap criteria); + + + /** + * 获得某个用户收到的评论 + * + * @return 列表 + */ + List getReceiveComment(List articleIds); + + + /** + * 统计评论数 + * + * @return 数量 + */ + Integer countComment(); + + /** + * 获得最近评论 + * + * @param limit 查询数量 + * @return 列表 + */ + List listRecentComment(@Param(value = "userId") Integer userId, + @Param(value = "limit") Integer limit); + + /** + * 获得评论的子评论 + * + * @param id 评论ID + * @return 列表 + */ + List listChildComment(@Param(value = "id") Integer id); + + + /** + * 根据用户ID删除 + * + * @param userId 用户ID + * @return 影响函数 + */ + Integer deleteByUserId(Integer userId); + + + /** + * 根据文章ID删除 + * + * @param articleId 文章ID + * @return 影响函数 + */ + Integer deleteByArticleId(Integer articleId); +} \ No newline at end of file diff --git a/src/java/mapper/LinkMapper.java b/src/java/mapper/LinkMapper.java new file mode 100644 index 0000000..dba04b8 --- /dev/null +++ b/src/java/mapper/LinkMapper.java @@ -0,0 +1,61 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Link; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface LinkMapper { + + /** + * 删除 + * @param linkId 链接ID + * @return 影响行数 + */ + int deleteById(Integer linkId); + + /** + * 添加 + * + * @param link 链接 + * @return 影响行数 + */ + int insert(Link link); + + /** + * 根据ID查询 + * + * @param linkId 链接ID + * @return 影响行数 + */ + Link getLinkById(Integer linkId); + + /** + * 更新 + * + * @param link 链接ID + * @return 影响行数 + */ + int update(Link link); + + /** + * 获得链接总数 + * + * @param status 状态 + * @return 数量 + */ + Integer countLink(@Param(value = "status") Integer status); + + /** + * 获得链接列表 + * + * @param status 状态 + * @return 列表 + */ + List listLink(@Param(value = "status") Integer status); +} \ No newline at end of file diff --git a/src/java/mapper/MenuMapper.java b/src/java/mapper/MenuMapper.java new file mode 100644 index 0000000..d39a48a --- /dev/null +++ b/src/java/mapper/MenuMapper.java @@ -0,0 +1,49 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Menu; + +import java.util.List; + +/** + * @author liuyanzhao + */ +public interface MenuMapper { + + /** + * 删除 + * + * @param menuId 菜单ID + * @return 影响行数 + */ + int deleteById(Integer menuId); + + /** + * 添加 + * @param menu 菜单 + * @return 影响行数 + */ + int insert(Menu menu); + + /** + * 根据ID查询 + * + * @param menuId 菜单ID + * @return 菜单 + */ + Menu getMenuById(Integer menuId); + + /** + * 更新 + * + * @param menu 菜单 + * @return 影响行数 + */ + int update(Menu menu); + + /** + * 获得菜单列表 + * + * @return 列表 + */ + List listMenu() ; +} \ No newline at end of file diff --git a/src/java/mapper/NoticeMapper.java b/src/java/mapper/NoticeMapper.java new file mode 100644 index 0000000..e50d80b --- /dev/null +++ b/src/java/mapper/NoticeMapper.java @@ -0,0 +1,70 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Notice; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface NoticeMapper { + + /** + * 根据ID删除 + * + * @param noticeId 公告ID + * @return 影响行数 + */ + int deleteById(Integer noticeId); + + /** + * 添加 + * + * @param notice 公告 + * @return 影响行数 + */ + int insert(Notice notice); + + /** + * 根据ID查询 + * + * @param noticeId 公告ID + * @return 公告 + */ + Notice getNoticeById(Integer noticeId); + + /** + * 获得公告列表 + * + * @param notice 公告 + * @return 影响行数 + */ + int update(Notice notice); + + /** + * 获得公告列表 + * + * @param notice 公告 + * @return 影响行数 + */ + int updateByPrimaryKey(Notice notice); + + /** + * 获得公告总数 + * + * @param status 状态 + * @return 影响行数 + */ + Integer countNotice(@Param(value = "status") Integer status); + + /** + * 获得公告列表 + * + * @param status 状态 + * @return 公告列表 + */ + List listNotice(@Param(value = "status") Integer status); +} \ No newline at end of file diff --git a/src/java/mapper/OptionsMapper.java b/src/java/mapper/OptionsMapper.java new file mode 100644 index 0000000..dbbaf4d --- /dev/null +++ b/src/java/mapper/OptionsMapper.java @@ -0,0 +1,48 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Options; +import org.apache.ibatis.annotations.Mapper; + +/** + * @author liuyanzhao + */ +@Mapper +public interface OptionsMapper { + + /** + * 根据ID删除 + * @param optionId 系统设置ID + * @return 影响行数 + */ + int deleteById(Integer optionId); + + /** + * 添加 + * @param options 系统设置 + * @return 影响行数 + */ + int insert(Options options); + + /** + * 根据ID查询 + * + * @param optionId 系统设置ID + * @return 系统设置 + */ + Options getOptionsById(Integer optionId); + + /** + * 更新 + * + * @param options 系统信息 + * @return 影响行数 + */ + int update(Options options); + + /** + * 获得记录 + * + * @return 系统信息 + */ + Options getOptions(); +} \ No newline at end of file diff --git a/src/java/mapper/PageMapper.java b/src/java/mapper/PageMapper.java new file mode 100644 index 0000000..58c6ba2 --- /dev/null +++ b/src/java/mapper/PageMapper.java @@ -0,0 +1,63 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Page; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface PageMapper { + /** + * 根据ID删除 + * + * @param pageId 页面ID + * @return 影响行数 + */ + int deleteById(Integer pageId); + + /** + * 添加 + * + * @param page 页面 + * @return 影响行数 + */ + int insert(Page page); + + /** + * 根据ID查询 + * + * @param pageId 页面ID + * @return 页面 + */ + Page getPageById(Integer pageId); + + /** + * 更新 + * + * @param page 页面 + * @return 影响行数 + */ + int update(Page page); + + /** + * 获得页面列表 + * + * @param status 状态 + * @return 页面列表 + */ + List listPage(@Param(value = "status") Integer status); + + /** + * 根据key获得页面 + * + * @param status 状态 + * @param key 别名 + * @return 页面 + */ + Page getPageByKey(@Param(value = "status") Integer status, + @Param(value = "key") String key); +} \ No newline at end of file diff --git a/src/java/mapper/TagMapper.java b/src/java/mapper/TagMapper.java new file mode 100644 index 0000000..8de7f08 --- /dev/null +++ b/src/java/mapper/TagMapper.java @@ -0,0 +1,67 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.Tag; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface TagMapper { + + /** + * 根据ID删除 + * + * @param tagId 标签ID + * @return 影响行数 + */ + int deleteById(Integer tagId); + + /** + * 添加 + * + * @param tag 标签 + * @return 影响行数 + */ + int insert(Tag tag); + + /** + * 根据ID查询 + * + * @param tagId 标签ID + * @return 标签 + */ + Tag getTagById(Integer tagId); + + /** + * 更新 + * @param tag 标签 + * @return 影响行数 + */ + int update(Tag tag); + + /** + * 获得标签总数 + * + * @return 数量 + */ + Integer countTag() ; + + /** + * 获得标签列表 + * + * @return 列表 + */ + List listTag() ; + + + /** + * 根据标签名获取标签 + * + * @param name 名称 + * @return 标签 + */ + Tag getTagByName(String name) ; +} \ No newline at end of file diff --git a/src/java/mapper/UserMapper.java b/src/java/mapper/UserMapper.java new file mode 100644 index 0000000..8adbc49 --- /dev/null +++ b/src/java/mapper/UserMapper.java @@ -0,0 +1,79 @@ +package com.liuyanzhao.ssm.blog.mapper; + +import com.liuyanzhao.ssm.blog.entity.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Mapper +public interface UserMapper { + + /** + * 根据ID删除 + * + * @param userId 用户ID + * @return 影响行数 + */ + int deleteById(Integer userId); + + /** + * 添加 + * + * @param user 用户 + * @return 影响行数 + */ + int insert(User user); + + /** + * 根据ID查询 + * + * @param userId 用户ID + * @return 用户 + */ + User getUserById(Integer userId); + + /** + * 更新 + * + * @param user 用户 + * @return 影响行数 + */ + int update(User user); + + + /** + * 获得用户列表 + * + * @return 用户列表 + */ + List listUser() ; + + + /** + * 根据用户名或Email获得用户 + * + * @param str 用户名或Email + * @return 用户 + */ + User getUserByNameOrEmail(String str) ; + + /** + * 根据用户名查用户 + * + * @param name 用户名 + * @return 用户 + */ + User getUserByName(String name) ; + + /** + * 根据Email查询用户 + * + * @param email 邮箱 + * @return 用户 + */ + User getUserByEmail(String email) ; + +} \ No newline at end of file diff --git a/src/java/service/ArticleServiceImpl.java b/src/java/service/ArticleServiceImpl.java index b1c5e94..8567a5b 100644 --- a/src/java/service/ArticleServiceImpl.java +++ b/src/java/service/ArticleServiceImpl.java @@ -97,7 +97,7 @@ public class ArticleServiceImpl implements ArticleService { return articleTagRefMapper.countArticleByTagId(tagId); } - +//sjhdfg @Override public List
listArticle(HashMap criteria) { return articleMapper.findAll(criteria); diff --git a/src/java/service/CategoryService.java b/src/java/service/CategoryService.java new file mode 100644 index 0000000..faaa1dd --- /dev/null +++ b/src/java/service/CategoryService.java @@ -0,0 +1,76 @@ +package com.liuyanzhao.ssm.blog.service; + + +import com.liuyanzhao.ssm.blog.entity.Category; + + +import java.util.List; + +/** + * @author 言曌 + * @date 2017/8/24 + */ +public interface CategoryService { + /** + * 获得分类总数 + * + * @return + */ + Integer countCategory(); + + + /** + * 获得分类列表 + * + * @return 分类列表 + */ + List listCategory(); + + /** + * 获得分类列表 + * + * @return 分类列表 + */ + List listCategoryWithCount(); + + /** + * 删除分类 + * + * @param id ID + */ + + void deleteCategory(Integer id); + + /** + * 根据id查询分类信息 + * + * @param id ID + * @return 分类 + */ + Category getCategoryById(Integer id); + + /** + * 添加分类 + * + * @param category 分类 + * @return 分类 + */ + Category insertCategory(Category category); + + /** + * 更新分类 + * + * @param category 分类 + */ + void updateCategory(Category category); + + /** + * 根据分类名获取分类 + * + * @param name 名称 + * @return 分类 + */ + Category getCategoryByName(String name); + + +} diff --git a/src/java/service/CommentService.java b/src/java/service/CommentService.java new file mode 100644 index 0000000..17613a8 --- /dev/null +++ b/src/java/service/CommentService.java @@ -0,0 +1,105 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.entity.Comment; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; + + +/** + * @author 言曌 + * @date 2017/9/10 + */ +@Service +public interface CommentService { + + /** + * 添加评论 + * + * @param comment 评论 + */ + void insertComment(Comment comment); + + /** + * 根据文章id获取评论列表 + * + * @param articleId 文章ID + * @return 列表 + */ + List listCommentByArticleId(Integer articleId); + + /** + * 根据id获取评论 + * + * @param id + * @return + */ + Comment getCommentById(Integer id); + + + /** + * 获取所有评论列表 + * + * @param pageIndex 第几页开始 + * @param pageSize 一页显示数量 + * @return 列表 + */ + PageInfo listCommentByPage( + Integer pageIndex, + Integer pageSize, + HashMap criteria); + + /** + * 获得某个用户收到的评论 + * + * @param pageIndex 第几页开始 + * @param pageSize 一页显示数量 + * @return 列表 + */ + PageInfo listReceiveCommentByPage( + Integer pageIndex, + Integer pageSize, + Integer userId); + + + /** + * 删除评论 + * + * @param id ID + */ + void deleteComment(Integer id); + + /** + * 修改评论 + * + * @param comment 评论 + */ + void updateComment(Comment comment); + + /** + * 统计评论数 + * + * @return 数量 + */ + Integer countComment(); + + /** + * 获得最近评论 + * + * @param limit 查询数量 + * @return 列表 + */ + List listRecentComment(Integer userId, Integer limit); + + /** + * 获得评论的子评论 + * + * @param id 评论ID + * @return 列表 + */ + List listChildComment(Integer id); + + +} diff --git a/src/java/service/LinkService.java b/src/java/service/LinkService.java new file mode 100644 index 0000000..113b944 --- /dev/null +++ b/src/java/service/LinkService.java @@ -0,0 +1,60 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Link; + +import java.util.List; + +/** + * + * @author 言曌 + * @date 2017/9/4 + */ +public interface LinkService { + + /** + * 获得链接总数 + * + * @param status 状态 + * @return 数量 + */ + Integer countLink(Integer status); + + /** + * 获得链接列表 + * + * @param status 状态 + * @return 链接列表 + */ + List listLink(Integer status); + + /** + * 添加链接 + * + * @param link 链接 + */ + void insertLink(Link link); + + /** + * 删除链接 + * + * @param id 链接ID + */ + void deleteLink(Integer id); + + /** + * 更新链接 + * + * @param link 链接 + */ + void updateLink(Link link); + + /** + * 根据id查询链接 + * + * @param id 链接ID + * @return 链接 + */ + Link getLinkById(Integer id); + + +} diff --git a/src/java/service/MenuService.java b/src/java/service/MenuService.java new file mode 100644 index 0000000..1355fd7 --- /dev/null +++ b/src/java/service/MenuService.java @@ -0,0 +1,46 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Menu; + +import java.util.List; + + /** + * @author liuyanzhao + */ + public interface MenuService { + /** + * 获得菜单列表 + * + * @return 列表 + */ + List listMenu() ; + + /** + * 添加菜单项目 + * + * @param menu 菜单 + */ + Menu insertMenu(Menu menu) ; + + /** + * 删除菜单项目 + * + * @param id 菜单ID + */ + void deleteMenu(Integer id) ; + + /** + * 更新菜单项目 + * + * @param menu 菜单 + */ + void updateMenu(Menu menu) ; + + /** + * 根据id获得菜单项目信息 + * + * @param id 菜单ID + * @return 菜单 + */ + Menu getMenuById(Integer id) ; +} diff --git a/src/java/service/NoticeService.java b/src/java/service/NoticeService.java new file mode 100644 index 0000000..d7c767d --- /dev/null +++ b/src/java/service/NoticeService.java @@ -0,0 +1,50 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Notice; + +import java.util.List; + +/** + * @author liuyanzhao + */ +public interface NoticeService { + + + /** + * 获得公告列表 + * + * @param status 状态 + * @return 列表 + */ + List listNotice(Integer status); + + /** + * 添加公告 + * + * @param notice 公告 + */ + void insertNotice(Notice notice); + + /** + * 删除公告 + * + * @param id + */ + void deleteNotice(Integer id); + + /** + * 更新公告 + * + * @param notice + */ + void updateNotice(Notice notice); + + /** + * 根据id查询公告 + * + * @param id ID + * @return 公告 + */ + Notice getNoticeById(Integer id); + +} diff --git a/src/java/service/OptionsService.java b/src/java/service/OptionsService.java new file mode 100644 index 0000000..ac9ec5b --- /dev/null +++ b/src/java/service/OptionsService.java @@ -0,0 +1,32 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Options; + + +/** + * + * @author 言曌 + * @date 2017/9/7 + */ +public interface OptionsService { + /** + * 获得基本信息 + * + * @return 系统设置 + */ + Options getOptions(); + + /** + * 新建基本信息 + * + * @param options 系统设置 + */ + void insertOptions(Options options); + + /** + * 更新基本信息 + * + * @param options 系统设置 + */ + void updateOptions(Options options); +} diff --git a/src/java/service/PageService.java b/src/java/service/PageService.java new file mode 100644 index 0000000..da84508 --- /dev/null +++ b/src/java/service/PageService.java @@ -0,0 +1,58 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Page; + +import java.util.List; + +/** + * + * @author 言曌 + * @date 2017/9/7 + */ +public interface PageService { + /** + * 获得页面列表 + * + * @param status 状态 + * @return 列表 + */ + List listPage(Integer status); + + /** + * 根据页面key获得页面 + * + * @param status 状态 + * @param key 别名 + * @return 页面 + */ + Page getPageByKey(Integer status, String key); + + /** + * 根据id获取页面 + * + * @param id 页面ID + * @return 页面 + */ + Page getPageById(Integer id); + + /** + * 添加页面 + * + * @param page 页面 + */ + void insertPage(Page page); + + /** + * 删除页面 + * + * @param id 页面ID + */ + void deletePage(Integer id); + + /** + * 编辑页面 + * + * @param page 分页 + */ + void updatePage(Page page); +} diff --git a/src/java/service/TagService.java b/src/java/service/TagService.java new file mode 100644 index 0000000..4bf6e51 --- /dev/null +++ b/src/java/service/TagService.java @@ -0,0 +1,82 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.Tag; + + +import java.util.List; + +/** + * + * @author 言曌 + * @date 2017/9/2 + */ +public interface TagService { + + /** + * 获得标签总数 + * + * @return 数量 + */ + Integer countTag() ; + + /** + * 获得标签列表 + * + * @return 标签列表 + */ + List listTag() ; + + /** + * 获得标签列表 + * + * @return 标签列表 + */ + List listTagWithCount() ; + + /** + * 根据id获得标签信息 + * + * @param id 标签ID + * @return 标签 + */ + Tag getTagById(Integer id) ; + + /** + * 添加标签 + * + * @param tag 标签 + * @return 标签 + */ + Tag insertTag(Tag tag) ; + + /** + * 修改标签 + * + * @param tag 标签 + */ + void updateTag(Tag tag) ; + + /** + * 删除标签 + * + * @param id 标签iD + */ + void deleteTag(Integer id) ; + + /** + * 根据标签名获取标签 + * + * @param name 标签名称 + * @return 标签 + */ + Tag getTagByName(String name) ; + + /** + * 根据文章ID获得标签 + * + * @param articleId 文章ID + * @return 标签列表 + */ + List listTagByArticleId(Integer articleId); + +} diff --git a/src/java/service/UserService.java b/src/java/service/UserService.java new file mode 100644 index 0000000..e0a60a1 --- /dev/null +++ b/src/java/service/UserService.java @@ -0,0 +1,73 @@ +package com.liuyanzhao.ssm.blog.service; + +import com.liuyanzhao.ssm.blog.entity.User; + +import java.util.List; + +/** + * @author 言曌 + * @date 2017/8/24 + */ + +public interface UserService { + /** + * 获得用户列表 + * + * @return 用户列表 + */ + List listUser(); + + /** + * 根据id查询用户信息 + * + * @param id 用户ID + * @return 用户 + */ + User getUserById(Integer id); + + /** + * 修改用户信息 + * + * @param user 用户 + */ + void updateUser(User user); + + /** + * 删除用户 + * + * @param id 用户ID + */ + void deleteUser(Integer id); + + /** + * 添加用户 + * + * @param user 用户 + * @return 用户 + */ + User insertUser(User user); + + /** + * 根据用户名和邮箱查询用户 + * + * @param str 用户名或Email + * @return 用户 + */ + User getUserByNameOrEmail(String str); + + /** + * 根据用户名查询用户 + * + * @param name 用户名 + * @return 用户 + */ + User getUserByName(String name); + + /** + * 根据邮箱查询用户 + * + * @param email Email + * @return 用户 + */ + User getUserByEmail(String email); +} diff --git a/src/java/service/impl/ArticleServiceImpl.java b/src/java/service/impl/ArticleServiceImpl.java new file mode 100644 index 0000000..b1c5e94 --- /dev/null +++ b/src/java/service/impl/ArticleServiceImpl.java @@ -0,0 +1,286 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import cn.hutool.core.util.RandomUtil; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.enums.ArticleCommentStatus; +import com.liuyanzhao.ssm.blog.mapper.*; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.entity.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +/** + * 文章Servie实现 + * + * @author 言曌 + * @date 2017/8/24 + */ +@Service +@Slf4j +public class ArticleServiceImpl implements ArticleService { + + @Autowired + private ArticleMapper articleMapper; + + @Autowired + private ArticleCategoryRefMapper articleCategoryRefMapper; + + @Autowired + private ArticleTagRefMapper articleTagRefMapper; + + @Autowired + private UserMapper userMapper; + + @Autowired + private CommentMapper commentMapper; + + @Override + public Integer countArticle(Integer status) { + Integer count = 0; + try { + count = articleMapper.countArticle(status); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据状态统计文章数, status:{}, cause:{}", status, e); + } + return count; + } + + @Override + public Integer countArticleComment() { + Integer count = 0; + try { + count = articleMapper.countArticleComment(); + } catch (Exception e) { + e.printStackTrace(); + log.error("统计文章评论数失败, cause:{}", e); + } + return count; + } + + + @Override + public Integer countArticleView() { + Integer count = 0; + try { + count = articleMapper.countArticleView(); + } catch (Exception e) { + e.printStackTrace(); + log.error("统计文章访问量失败, cause:{}", e); + } + return count; + } + + @Override + public Integer countArticleByCategoryId(Integer categoryId) { + Integer count = 0; + try { + count = articleCategoryRefMapper.countArticleByCategoryId(categoryId); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据分类统计文章数量失败, categoryId:{}, cause:{}", categoryId, e); + } + return count; + } + + @Override + public Integer countArticleByTagId(Integer tagId) { + return articleTagRefMapper.countArticleByTagId(tagId); + + } + + @Override + public List
listArticle(HashMap criteria) { + return articleMapper.findAll(criteria); + } + + @Override + public List
listRecentArticle(Integer userId, Integer limit) { + return articleMapper.listArticleByLimit(userId, limit); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateArticleDetail(Article article) { + article.setArticleUpdateTime(new Date()); + articleMapper.update(article); + + if (article.getTagList() != null) { + //删除标签和文章关联 + articleTagRefMapper.deleteByArticleId(article.getArticleId()); + //添加标签和文章关联 + for (int i = 0; i < article.getTagList().size(); i++) { + ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId()); + articleTagRefMapper.insert(articleTagRef); + } + } + + if (article.getCategoryList() != null) { + //添加分类和文章关联 + articleCategoryRefMapper.deleteByArticleId(article.getArticleId()); + //删除分类和文章关联 + for (int i = 0; i < article.getCategoryList().size(); i++) { + ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId()); + articleCategoryRefMapper.insert(articleCategoryRef); + } + } + } + + @Override + public void updateArticle(Article article) { + articleMapper.update(article); + } + + @Override + public void deleteArticleBatch(List ids) { + articleMapper.deleteBatch(ids); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteArticle(Integer id) { + articleMapper.deleteById(id); + // 删除分类关联 + articleCategoryRefMapper.deleteByArticleId(id); + // 删除标签管理 + articleTagRefMapper.deleteByArticleId(id); + // 删除评论 + commentMapper.deleteByArticleId(id); + } + + + @Override + public PageInfo
pageArticle(Integer pageIndex, + Integer pageSize, + HashMap criteria) { + PageHelper.startPage(pageIndex, pageSize); + List
articleList = articleMapper.findAll(criteria); + for (int i = 0; i < articleList.size(); i++) { + //封装CategoryList + List categoryList = articleCategoryRefMapper.listCategoryByArticleId(articleList.get(i).getArticleId()); + if (categoryList == null || categoryList.size() == 0) { + categoryList = new ArrayList<>(); + categoryList.add(Category.Default()); + } + articleList.get(i).setCategoryList(categoryList); + + articleList.get(i).setUser(userMapper.getUserById(articleList.get(i).getArticleUserId())); +// //封装TagList +// List tagList = articleTagRefMapper.listTagByArticleId(articleList.get(i).getArticleId()); +// articleList.get(i).setTagList(tagList); + } + return new PageInfo<>(articleList); + } + + @Override + public Article getArticleByStatusAndId(Integer status, Integer id) { + Article article = articleMapper.getArticleByStatusAndId(status, id); + if (article != null) { + List categoryList = articleCategoryRefMapper.listCategoryByArticleId(article.getArticleId()); + List tagList = articleTagRefMapper.listTagByArticleId(article.getArticleId()); + article.setCategoryList(categoryList); + article.setTagList(tagList); + } + return article; + } + + + @Override + public List
listArticleByViewCount(Integer limit) { + return articleMapper.listArticleByViewCount(limit); + } + + @Override + public Article getAfterArticle(Integer id) { + return articleMapper.getAfterArticle(id); + } + + @Override + public Article getPreArticle(Integer id) { + return articleMapper.getPreArticle(id); + } + + @Override + public List
listRandomArticle(Integer limit) { + return articleMapper.listRandomArticle(limit); + } + + @Override + public List
listArticleByCommentCount(Integer limit) { + return articleMapper.listArticleByCommentCount(limit); + } + + + @Override + @Transactional(rollbackFor = Exception.class) + public void insertArticle(Article article) { + //添加文章 + article.setArticleCreateTime(new Date()); + article.setArticleUpdateTime(new Date()); + article.setArticleIsComment(ArticleCommentStatus.ALLOW.getValue()); + article.setArticleViewCount(0); + article.setArticleLikeCount(0); + article.setArticleCommentCount(0); + article.setArticleOrder(1); + if (StringUtils.isEmpty(article.getArticleThumbnail())) { + article.setArticleThumbnail("/img/thumbnail/random/img_" + RandomUtil.randomNumbers(1) + ".jpg"); + } + + articleMapper.insert(article); + //添加分类和文章关联 + for (int i = 0; i < article.getCategoryList().size(); i++) { + ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId()); + articleCategoryRefMapper.insert(articleCategoryRef); + } + //添加标签和文章关联 + for (int i = 0; i < article.getTagList().size(); i++) { + ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId()); + articleTagRefMapper.insert(articleTagRef); + } + } + + + @Override + public void updateCommentCount(Integer articleId) { + articleMapper.updateCommentCount(articleId); + } + + @Override + public Article getLastUpdateArticle() { + return articleMapper.getLastUpdateArticle(); + } + + @Override + public List
listArticleByCategoryId(Integer cateId, Integer limit) { + return articleMapper.findArticleByCategoryId(cateId, limit); + } + + @Override + public List
listArticleByCategoryIds(List cateIds, Integer limit) { + if (cateIds == null || cateIds.size() == 0) { + return null; + } + return articleMapper.findArticleByCategoryIds(cateIds, limit); + } + + @Override + public List listCategoryIdByArticleId(Integer articleId) { + return articleCategoryRefMapper.selectCategoryIdByArticleId(articleId); + } + + @Override + public List
listAllNotWithContent() { + return articleMapper.listAllNotWithContent(); + } + + +} diff --git a/src/java/service/impl/CategoryServiceImpl.java b/src/java/service/impl/CategoryServiceImpl.java new file mode 100644 index 0000000..6cd6cd7 --- /dev/null +++ b/src/java/service/impl/CategoryServiceImpl.java @@ -0,0 +1,133 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.mapper.ArticleCategoryRefMapper; +import com.liuyanzhao.ssm.blog.mapper.CategoryMapper; +import com.liuyanzhao.ssm.blog.entity.Category; +import com.liuyanzhao.ssm.blog.service.CategoryService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; + +import java.util.List; + + +/** + * 用户管理 + * + * @author 言曌 + * @date 2017/8/24 + */ +@Service +@Slf4j +public class CategoryServiceImpl implements CategoryService { + + @Autowired + private CategoryMapper categoryMapper; + + @Autowired + private ArticleCategoryRefMapper articleCategoryRefMapper; + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteCategory(Integer id) { + try { + categoryMapper.deleteCategory(id); + articleCategoryRefMapper.deleteByCategoryId(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("删除分类失败, id:{}, cause:{}", id, e); + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + } + } + + @Override + public Category getCategoryById(Integer id) { + Category category = null; + try { + category = categoryMapper.getCategoryById(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据分类ID获得分类, id:{}, cause:{}", id, e); + } + return category; + } + + @Override + public void updateCategory(Category category) { + try { + categoryMapper.update(category); + } catch (Exception e) { + e.printStackTrace(); + log.error("更新分类失败, category:{}, cause:{}", category, e); + } + } + + @Override + public Category insertCategory(Category category) { + try { + categoryMapper.insert(category); + } catch (Exception e) { + e.printStackTrace(); + log.error("创建分类失败, category:{}, cause:{}", category, e); + } + return category; + } + + + @Override + public Integer countCategory() { + Integer count = 0; + try { + count = categoryMapper.countCategory(); + } catch (Exception e) { + e.printStackTrace(); + log.error("统计分类失败, cause:{}", e); + } + return count; + } + + + @Override + public List listCategory() { + List categoryList = null; + try { + categoryList = categoryMapper.listCategory(); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据文章获得分类列表失败, cause:{}", e); + } + return categoryList; + } + + @Override + public List listCategoryWithCount() { + List categoryList = null; + try { + categoryList = categoryMapper.listCategory(); + for (int i = 0; i < categoryList.size(); i++) { + Integer count = articleCategoryRefMapper.countArticleByCategoryId(categoryList.get(i).getCategoryId()); + categoryList.get(i).setArticleCount(count); + } + } catch (Exception e) { + e.printStackTrace(); + log.error("根据文章获得分类列表失败, cause:{}", e); + } + return categoryList; + } + + @Override + public Category getCategoryByName(String name) { + Category category = null; + try { + category = categoryMapper.getCategoryByName(name); + } catch (Exception e) { + e.printStackTrace(); + log.error("更新分类失败, category:{}, cause:{}", category, e); + } + return category; + } + + +} diff --git a/src/java/service/impl/CommentServiceImpl.java b/src/java/service/impl/CommentServiceImpl.java new file mode 100644 index 0000000..20f9015 --- /dev/null +++ b/src/java/service/impl/CommentServiceImpl.java @@ -0,0 +1,165 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.liuyanzhao.ssm.blog.entity.Article; +import com.liuyanzhao.ssm.blog.entity.Comment; +import com.liuyanzhao.ssm.blog.enums.ArticleStatus; +import com.liuyanzhao.ssm.blog.mapper.CommentMapper; +import com.liuyanzhao.ssm.blog.mapper.ArticleMapper; +import com.liuyanzhao.ssm.blog.service.CommentService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * @author 言曌 + * @date 2017/9/10 + */ +@Service +@Slf4j +public class CommentServiceImpl implements CommentService { + + @Autowired + private CommentMapper commentMapper; + + @Autowired + private ArticleMapper articleMapper; + + @Override + public void insertComment(Comment comment) { + try { + commentMapper.insert(comment); + } catch (Exception e) { + e.printStackTrace(); + log.error("创建评论失败:comment:{}, cause:{}", comment, e); + } + } + + @Override + public List listCommentByArticleId(Integer articleId) { + List commentList = null; + try { + commentList = commentMapper.listCommentByArticleId(articleId); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据文章ID获得评论列表失败,articleId:{},cause:{}", articleId, e); + } + return commentList; + } + + @Override + public Comment getCommentById(Integer id) { + Comment comment = null; + try { + comment = commentMapper.getCommentById(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据评论ID获得评论,id:{}, cause:{}", id, e); + } + return comment; + } + + @Override + public PageInfo listCommentByPage(Integer pageIndex, Integer pageSize, HashMap criteria) { + PageHelper.startPage(pageIndex, pageSize); + List commentList = null; + try { + commentList = commentMapper.listComment(criteria); + for (int i = 0; i < commentList.size(); i++) { + Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); + commentList.get(i).setArticle(article); + } + } catch (Exception e) { + e.printStackTrace(); + log.error("分页获得评论失败,pageIndex:{}, pageSize:{}, cause:{}", pageIndex, pageSize, e); + } + return new PageInfo<>(commentList); + } + + @Override + public PageInfo listReceiveCommentByPage(Integer pageIndex, Integer pageSize, Integer userId) { + PageHelper.startPage(pageIndex, pageSize); + List commentList = new ArrayList<>(); + try { + List articleIds = articleMapper.listArticleIdsByUserId(userId); + if (articleIds != null && articleIds.size() > 0) { + commentList = commentMapper.getReceiveComment(articleIds); + for (int i = 0; i < commentList.size(); i++) { + Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); + commentList.get(i).setArticle(article); + } + } + } catch (Exception e) { + e.printStackTrace(); + log.error("分页获得评论失败,pageIndex:{}, pageSize:{}, cause:{}", pageIndex, pageSize, e); + } + return new PageInfo<>(commentList); + } + + @Override + public void deleteComment(Integer id) { + try { + commentMapper.deleteById(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("删除评论失败, id:{}, cause:{}", id, e); + } + } + + @Override + public void updateComment(Comment comment) { + try { + commentMapper.update(comment); + } catch (Exception e) { + e.printStackTrace(); + log.error("更新评论,comment:{}, cause:{}", comment, e); + } + } + + @Override + public Integer countComment() { + Integer commentCount = null; + try { + commentCount = commentMapper.countComment(); + } catch (Exception e) { + e.printStackTrace(); + log.error("统计评论数量失败, cause:{}", e); + } + return commentCount; + } + + @Override + public List listRecentComment(Integer userId, Integer limit) { + List commentList = null; + try { + commentList = commentMapper.listRecentComment(userId, limit); + for (int i = 0; i < commentList.size(); i++) { + Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId()); + commentList.get(i).setArticle(article); + } + } catch (Exception e) { + e.printStackTrace(); + log.error("获得最新评论失败, limit:{}, cause:{}", limit, e); + } + return commentList; + } + + @Override + public List listChildComment(Integer id) { + List childCommentList = null; + try { + childCommentList = commentMapper.listChildComment(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("获得子评论失败, id:{}, cause:{}", id, e); + } + return childCommentList; + } + +} diff --git a/src/java/service/impl/LinkServiceImpl.java b/src/java/service/impl/LinkServiceImpl.java new file mode 100644 index 0000000..ef0ad3e --- /dev/null +++ b/src/java/service/impl/LinkServiceImpl.java @@ -0,0 +1,53 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.entity.Link; +import com.liuyanzhao.ssm.blog.mapper.LinkMapper; +import com.liuyanzhao.ssm.blog.service.LinkService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +import java.util.List; + +/** + * + * @author 言曌 + * @date 2017/9/4 + */ +@Service +public class LinkServiceImpl implements LinkService { + + @Autowired + private LinkMapper linkMapper; + + @Override + public Integer countLink(Integer status) { + return linkMapper.countLink(status); + } + + @Override + public List listLink(Integer status) { + return linkMapper.listLink(status); + } + + @Override + public void insertLink(Link link) { + linkMapper.insert(link); + } + + @Override + public void deleteLink(Integer id) { + linkMapper.deleteById(id); + } + + @Override + public void updateLink(Link link) { + linkMapper.update(link); + } + + @Override + public Link getLinkById(Integer id) { + return linkMapper.getLinkById(id); + } + +} diff --git a/src/java/service/impl/MenuServiceImpl.java b/src/java/service/impl/MenuServiceImpl.java new file mode 100644 index 0000000..baf2cbd --- /dev/null +++ b/src/java/service/impl/MenuServiceImpl.java @@ -0,0 +1,48 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.entity.Menu; +import com.liuyanzhao.ssm.blog.mapper.MenuMapper; +import com.liuyanzhao.ssm.blog.service.MenuService; +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Service +public class MenuServiceImpl implements MenuService { + + + @Autowired + private MenuMapper menuMapper; + + @Override + public List listMenu() { + List menuList = menuMapper.listMenu(); + return menuList; + } + + @Override + public Menu insertMenu(Menu menu) { + menuMapper.insert(menu); + return menu; + } + + @Override + public void deleteMenu(Integer id) { + menuMapper.deleteById(id); + } + + @Override + public void updateMenu(Menu menu) { + menuMapper.update(menu); + } + + @Override + public Menu getMenuById(Integer id) { + return menuMapper.getMenuById(id); + } +} diff --git a/src/java/service/impl/NoticeServiceImpl.java b/src/java/service/impl/NoticeServiceImpl.java new file mode 100644 index 0000000..f460fcb --- /dev/null +++ b/src/java/service/impl/NoticeServiceImpl.java @@ -0,0 +1,46 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.entity.Notice; +import com.liuyanzhao.ssm.blog.mapper.NoticeMapper; +import com.liuyanzhao.ssm.blog.service.NoticeService; +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @author liuyanzhao + */ +@Service +public class NoticeServiceImpl implements NoticeService { + + @Autowired + private NoticeMapper noticeMapper; + + @Override + public List listNotice(Integer status) { + return noticeMapper.listNotice(status); + } + + @Override + public void insertNotice(Notice notice) { + noticeMapper.insert(notice); + } + + @Override + public void deleteNotice(Integer id) { + noticeMapper.deleteById(id); + } + + @Override + public void updateNotice(Notice notice) { + noticeMapper.update(notice); + } + + @Override + public Notice getNoticeById(Integer id) { + return noticeMapper.getNoticeById(id); + } + +} diff --git a/src/java/service/impl/OptionsServiceImpl.java b/src/java/service/impl/OptionsServiceImpl.java new file mode 100644 index 0000000..de8892f --- /dev/null +++ b/src/java/service/impl/OptionsServiceImpl.java @@ -0,0 +1,40 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.entity.Options; +import com.liuyanzhao.ssm.blog.mapper.OptionsMapper; +import com.liuyanzhao.ssm.blog.service.OptionsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +/** + * + * @author 言曌 + * @date 2017/9/7 + */ +@Service +public class OptionsServiceImpl implements OptionsService { + + + @Autowired + private OptionsMapper optionsMapper; + + @Override + @Cacheable(value = "default", key = "'options'") + public Options getOptions() { + return optionsMapper.getOptions(); + } + + @Override + @CacheEvict(value = "default", key = "'options'") + public void insertOptions(Options options) { + optionsMapper.insert(options); + } + + @Override + @CacheEvict(value = "default", key = "'options'") + public void updateOptions(Options options) { + optionsMapper.update(options); + } +} diff --git a/src/java/service/impl/PageServiceImpl.java b/src/java/service/impl/PageServiceImpl.java new file mode 100644 index 0000000..2d2b882 --- /dev/null +++ b/src/java/service/impl/PageServiceImpl.java @@ -0,0 +1,52 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.entity.Page; +import com.liuyanzhao.ssm.blog.mapper.PageMapper; +import com.liuyanzhao.ssm.blog.service.PageService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +import java.util.List; + +/** + * @author 言曌 + * @date 2017/9/7 + */ +@Service +public class PageServiceImpl implements PageService { + + @Autowired + private PageMapper pageMapper; + + @Override + public Page getPageByKey(Integer status, String key) { + return pageMapper.getPageByKey(status, key); + } + + @Override + public Page getPageById(Integer id) { + return pageMapper.getPageById(id); + } + + @Override + public List listPage(Integer status) { + return pageMapper.listPage(status); + } + + + @Override + public void insertPage(Page page) { + pageMapper.insert(page); + } + + @Override + public void deletePage(Integer id) { + pageMapper.deleteById(id); + } + + @Override + public void updatePage(Page page) { + pageMapper.update(page); + } +} diff --git a/src/java/service/impl/TagServiceImpl.java b/src/java/service/impl/TagServiceImpl.java new file mode 100644 index 0000000..6cc1bd6 --- /dev/null +++ b/src/java/service/impl/TagServiceImpl.java @@ -0,0 +1,133 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.mapper.ArticleTagRefMapper; +import com.liuyanzhao.ssm.blog.mapper.TagMapper; +import com.liuyanzhao.ssm.blog.entity.Tag; +import com.liuyanzhao.ssm.blog.service.TagService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; + +import java.util.List; + +/** + * @author 言曌 + * @date 2017/9/2 + */ +@Service +@Slf4j +public class TagServiceImpl implements TagService { + + @Autowired + private TagMapper tagMapper; + + @Autowired + private ArticleTagRefMapper articleTagRefMapper; + + @Override + public Integer countTag() { + return tagMapper.countTag(); + } + + @Override + public List listTag() { + List tagList = null; + try { + tagList = tagMapper.listTag(); + } catch (Exception e) { + e.printStackTrace(); + log.error("获得所有标签失败, cause:{}", e); + } + return tagList; + } + + @Override + public List listTagWithCount() { + List tagList = null; + try { + tagList = tagMapper.listTag(); + for (int i = 0; i < tagList.size(); i++) { + Integer count = articleTagRefMapper.countArticleByTagId(tagList.get(i).getTagId()); + tagList.get(i).setArticleCount(count); + } + } catch (Exception e) { + e.printStackTrace(); + log.error("获得所有标签失败, cause:{}", e); + } + return tagList; + } + + + @Override + public Tag getTagById(Integer id) { + Tag tag = null; + try { + tag = tagMapper.getTagById(id); + } catch (Exception e) { e.printStackTrace(); + log.error("根据ID获得标签失败, id:{}, cause:{}", id, e); + } + return tag; + } + + @Override + public Tag insertTag(Tag tag) { + try { + tagMapper.insert(tag); + } catch (Exception e) { + e.printStackTrace(); + log.error("添加标签失败, tag:{}, cause:{}", tag, e); + } + return tag; + } + + @Override + public void updateTag(Tag tag) { + try { + tagMapper.update(tag); + } catch (Exception e) { + e.printStackTrace(); + log.error("更新标签失败, tag:{}, cause:{}", tag, e); + } + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteTag(Integer id) { + try { + tagMapper.deleteById(id); + articleTagRefMapper.deleteByTagId(id); + } catch (Exception e) { + e.printStackTrace(); + log.error("删除标签失败, id:{}, cause:{}", id, e); + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + } + + } + + @Override + public Tag getTagByName(String name) { + Tag tag = null; + try { + tag = tagMapper.getTagByName(name); + } catch (Exception e) { e.printStackTrace(); + log.error("根据名称获得标签, name:{}, cause:{}", name, e); + } + return tag; + } + + @Override + public List listTagByArticleId(Integer articleId) { + List tagList = null; + try { + tagList = articleTagRefMapper.listTagByArticleId(articleId); + } catch (Exception e) { + e.printStackTrace(); + log.error("根据文章ID获得标签失败,articleId:{}, cause:{}", articleId, e); + } + return tagList; + } + + +} diff --git a/src/java/service/impl/UserServiceImpl.java b/src/java/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..5fe8e2f --- /dev/null +++ b/src/java/service/impl/UserServiceImpl.java @@ -0,0 +1,95 @@ +package com.liuyanzhao.ssm.blog.service.impl; + +import com.liuyanzhao.ssm.blog.mapper.ArticleMapper; +import com.liuyanzhao.ssm.blog.mapper.CommentMapper; +import com.liuyanzhao.ssm.blog.mapper.UserMapper; +import com.liuyanzhao.ssm.blog.entity.User; +import com.liuyanzhao.ssm.blog.service.ArticleService; +import com.liuyanzhao.ssm.blog.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +/** + * 用户管理 + * + * @author 言曌 + * @date 2017/8/24 + */ +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserMapper userMapper; + + @Autowired + private ArticleMapper articleMapper; + + @Autowired + private ArticleService articleService; + @Autowired + private CommentMapper commentMapper; + + @Override + public List listUser() { + List userList = userMapper.listUser(); + for (int i = 0; i < userList.size(); i++) { + Integer articleCount = articleMapper.countArticleByUser(userList.get(i).getUserId()); + userList.get(i).setArticleCount(articleCount); + } + return userList; + } + + @Override + public User getUserById(Integer id) { + return userMapper.getUserById(id); + } + + @Override + public void updateUser(User user) { + userMapper.update(user); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteUser(Integer id) { + // 删除用户 + userMapper.deleteById(id); + // 删除评论 + commentMapper.deleteByUserId(id); + // 删除文章 + List articleIds = articleMapper.listArticleIdsByUserId(id); + if (articleIds != null && articleIds.size() > 0) { + for (Integer articleId : articleIds) { + articleService.deleteArticle(articleId); + } + } + } + + @Override + public User insertUser(User user) { + user.setUserRegisterTime(new Date()); + userMapper.insert(user); + return user; + } + + @Override + public User getUserByNameOrEmail(String str) { + return userMapper.getUserByNameOrEmail(str); + } + + @Override + public User getUserByName(String name) { + return userMapper.getUserByName(name); + } + + @Override + public User getUserByEmail(String email) { + return userMapper.getUserByEmail(email); + } + + +} diff --git a/src/java/util/MyUtils.java b/src/java/util/MyUtils.java new file mode 100644 index 0000000..49d7704 --- /dev/null +++ b/src/java/util/MyUtils.java @@ -0,0 +1,92 @@ +package com.liuyanzhao.ssm.blog.util; + +import javax.servlet.http.HttpServletRequest; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * 常用的方法 + * Created by 言曌 on 2017/8/24. + */ + +public class MyUtils { + + /** + * 获得IP地址 + * + * @param request + * @return + */ + public static String getIpAddr(HttpServletRequest request) { + String ipAddress = request.getHeader("x-forwarded-for"); + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getHeader("Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getHeader("WL-Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + ipAddress = request.getRemoteAddr(); + } + //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 + if (ipAddress != null && ipAddress.length() > 15) { + if (ipAddress.indexOf(",") > 0) { + ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); + } + } + return ipAddress; + } + + + /** + * 获得Md5加密 + * + * @param str 原字符串 + * @return 加密后的字符串 + */ + public static String strToMd5(String str) { + String md5Str = null; + if (str != null && str.length() != 0) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(str.getBytes()); + byte b[] = md.digest(); + + int i; + StringBuffer buf = new StringBuffer(""); + for (int offset = 0; offset < b.length; offset++) { + i = b[offset]; + if (i < 0) { + i += 256; + } + if (i < 16) { + buf.append("0"); + } + buf.append(Integer.toHexString(i)); + } + //32位 + md5Str = buf.toString(); + //16位 + //md5Str = buf.toString().substring(8, 24); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + return md5Str; + } + + /** + * 根据email获取gravatar头像 + * + * @param email Email + * @return 头像URL + */ + public static String getGravatar(String email) { + String emailMd5 = strToMd5(email); + //设置图片大小32px + String avatar = "http://cn.gravatar.com/avatar/" + emailMd5 + "?s=128&d=identicon&r=PG"; + return avatar; + } + + +} \ No newline at end of file