ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-后端功能开发

来源:互联网 发布:sql语句查询所有表 编辑:程序博客网 时间:2024/06/03 07:46

ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-后端功能开发

    下面就介绍介绍个人博客系统后端业务功能的管理。主要包括:发表博客,管理评论,管理博客,友情链接,博客类型管理等等。具体内容过多,我就不多废话,直接上代码。其中的代码我已经做了注释,直接阅读即可。如果有相关问题,可以加后面的群讨论。

    首先是后端首页的展示:


    下面是博客管理controller--BlogAdminController.java,其中功能包括发表博客,列表搜索,获取博客详情,删除博客,修改博客等等:

package com.steadyjack.controller.admin;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.steadyjack.entity.Blog;import com.steadyjack.entity.PageBean;import com.steadyjack.lucene.BlogIndex;import com.steadyjack.service.BlogService;import com.steadyjack.util.ResponseUtil;import com.steadyjack.util.StringUtil;import com.steadyjack.util.WebFileOperationUtil;/** * title:BlogAdminController.java * description:管理员博客Controller层 * time:2017年1月23日 下午10:15:18 * author:debug-steadyjack */@Controller@RequestMapping("/admin/blog")public class BlogAdminController {@Resourceprivate BlogService blogService;// 博客索引private BlogIndex blogIndex=new BlogIndex();/** * title:BlogAdminController.java * description:添加或者修改博客信息 * time:2017年1月23日 下午10:15:36 * author:debug-steadyjack * @param blog * @param response * @return * @throws Exception */@RequestMapping("/save")public String save(Blog blog,HttpServletResponse response,HttpServletRequest request)throws Exception{int resultTotal=0;String newContent=WebFileOperationUtil.copyImageInUeditor(request, blog.getContent());blog.setContent(newContent);blogIndex.setRequest(request);if(blog.getId()==null){//添加博客索引resultTotal=blogService.add(blog);blogIndex.addIndex(blog); }else{//更新博客索引resultTotal=blogService.update(blog);blogIndex.updateIndex(blog); }JSONObject result=new JSONObject();if(resultTotal>0){result.put("success", true);}else{result.put("success", false);}ResponseUtil.write(response, result);return null;}/** * title:BlogAdminController.java * description:分页查询博客信息 * time:2017年1月23日 下午10:17:41 * author:debug-steadyjack * @param page * @param rows * @param s_blog * @param response * @return * @throws Exception */@RequestMapping("/list")public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,Blog s_blog,HttpServletResponse response)throws Exception{PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));Map<String,Object> map=new HashMap<String,Object>();map.put("title", StringUtil.formatLike(s_blog.getTitle()));map.put("start", pageBean.getStart());map.put("size", pageBean.getPageSize());List<Blog> blogList=blogService.list(map);Long total=blogService.getTotal(map);JSONObject result=new JSONObject();//处理专门的日期字段值JsonConfig jsonConfig=new JsonConfig();jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));JSONArray jsonArray=JSONArray.fromObject(blogList,jsonConfig);result.put("rows", jsonArray);result.put("total", total);ResponseUtil.write(response, result);return null;}/** * title:BlogAdminController.java * description:删除博客信息 * time:2017年1月23日 下午10:20:11 * author:debug-steadyjack * @param ids * @param response * @return * @throws Exception */@RequestMapping("/delete")public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response,HttpServletRequest request)throws Exception{String[] idsStr=ids.split(",");blogIndex.setRequest(request);for(int i=0;i<idsStr.length;i++){Integer currId=Integer.parseInt(idsStr[i]);Blog currBlog=blogService.findById(currId);//删除博客内容里面的图片WebFileOperationUtil.deleteImagesInUeditor(request, currBlog.getContent());blogService.delete(currId);//删除对应博客的索引blogIndex.deleteIndex(idsStr[i]); }JSONObject result=new JSONObject();result.put("success", true);ResponseUtil.write(response, result);return null;}/** * title:BlogAdminController.java * description:通过ID查找实体 * time:2017年1月23日 下午10:20:45 * author:debug-steadyjack * @param id * @param response * @return * @throws Exception */@RequestMapping("/findById")public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{Blog blog=blogService.findById(Integer.parseInt(id));JSONObject jsonObject=JSONObject.fromObject(blog);ResponseUtil.write(response, jsonObject);return null;}}

    其中,值得说明的是,因为发表博客涉及到“百度编辑器ueditor”的操作,比如上传的图片,需要在保存博客时对图片进行处理,比如移动copy到指定的文件目录(因为一般而言ueditor配置初始文件上传目录是一个临时目录,不建议 永久保存在哪里)。对于这个操作,我将在下一篇博客重点讲述。

    下面是博客类型Controller--BlogTypeAdminController.java:

package com.steadyjack.controller.admin;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.steadyjack.entity.BlogType;import com.steadyjack.entity.PageBean;import com.steadyjack.service.BlogService;import com.steadyjack.service.BlogTypeService;import com.steadyjack.util.ResponseUtil;/** * title:BlogTypeAdminController.java * description:管理员博客类别Controller层 * time:2017年1月23日 下午10:22:24 * author:debug-steadyjack */@Controller@RequestMapping("/admin/blogType")public class BlogTypeAdminController {@Resourceprivate BlogTypeService blogTypeService;@Resourceprivate BlogService blogService;/** * title:BlogTypeAdminController.java * description:分页查询博客类别信息 * time:2017年1月23日 下午10:22:43 * author:debug-steadyjack * @param page * @param rows * @param response * @return * @throws Exception */@RequestMapping("/list")public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));Map<String,Object> map=new HashMap<String,Object>();map.put("start", pageBean.getStart());map.put("size", pageBean.getPageSize());List<BlogType> blogTypeList=blogTypeService.list(map);Long total=blogTypeService.getTotal(map);JSONObject result=new JSONObject();JSONArray jsonArray=JSONArray.fromObject(blogTypeList);result.put("rows", jsonArray);result.put("total", total);ResponseUtil.write(response, result);return null;}/** * title:BlogTypeAdminController.java * description:添加或者修改博客类别信息 * time:2017年1月23日 下午10:23:38 * author:debug-steadyjack * @param blogType * @param response * @return * @throws Exception */@RequestMapping("/save")public String save(BlogType blogType,HttpServletResponse response)throws Exception{//操作的记录条数int resultTotal=0;if(blogType.getId()==null){resultTotal=blogTypeService.add(blogType);}else{resultTotal=blogTypeService.update(blogType);}JSONObject result=new JSONObject();if(resultTotal>0){result.put("success", true);}else{result.put("success", false);}ResponseUtil.write(response, result);return null;}/** * title:BlogTypeAdminController.java * description:删除博客类别信息 * time:2017年1月23日 下午10:24:08 * author:debug-steadyjack * @param ids * @param response * @return * @throws Exception */@RequestMapping("/delete")public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{String[] idsStr=ids.split(",");JSONObject result=new JSONObject();for(int i=0;i<idsStr.length;i++){if(blogService.getBlogByTypeId(Integer.parseInt(idsStr[i]))>0){result.put("exist", "博客类别下有博客,不能删除!");}else{blogTypeService.delete(Integer.parseInt(idsStr[i]));}}result.put("success", true);ResponseUtil.write(response, result);return null;}}

    然后是评论管理Controller--CommentAdminController.java:

package com.steadyjack.controller.admin;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.steadyjack.entity.Comment;import com.steadyjack.entity.PageBean;import com.steadyjack.service.CommentService;import com.steadyjack.util.ResponseUtil;/** * title:CommentAdminController.java * description:管理员评论Controller层 * time:2017年1月23日 下午10:24:52 * author:debug-steadyjack */@Controller@RequestMapping("/admin/comment")public class CommentAdminController {@Resourceprivate CommentService commentService;/** * title:CommentAdminController.java * description:分页查询评论信息 * time:2017年1月23日 下午10:25:04 * author:debug-steadyjack * @param page * @param rows * @param state * @param response * @return * @throws Exception */@RequestMapping("/list")public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,@RequestParam(value="state",required=false)String state,HttpServletResponse response)throws Exception{PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));Map<String,Object> map=new HashMap<String,Object>();map.put("start", pageBean.getStart());map.put("size", pageBean.getPageSize());//评论状态map.put("state", state); List<Comment> commentList=commentService.list(map);Long total=commentService.getTotal(map);JSONObject result=new JSONObject();JsonConfig jsonConfig=new JsonConfig();jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));JSONArray jsonArray=JSONArray.fromObject(commentList,jsonConfig);result.put("rows", jsonArray);result.put("total", total);ResponseUtil.write(response, result);return null;}/** * title:CommentAdminController.java * description:删除评论信息 * time:2017年1月23日 下午10:31:32 * author:debug-steadyjack * @param ids * @param response * @return * @throws Exception */@RequestMapping("/delete")public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{String[] idsStr=ids.split(",");for(int i=0;i<idsStr.length;i++){commentService.delete(Integer.parseInt(idsStr[i]));}JSONObject result=new JSONObject();result.put("success", true);ResponseUtil.write(response, result);return null;}/** * title:CommentAdminController.java * description:评论审核 * time:2017年1月23日 下午10:31:58 * author:debug-steadyjack * @param ids * @param state * @param response * @return * @throws Exception */@RequestMapping("/review")public String review(@RequestParam(value="ids")String ids,@RequestParam(value="state")Integer state,HttpServletResponse response)throws Exception{String[] idsStr=ids.split(",");for(int i=0;i<idsStr.length;i++){Comment comment=new Comment();comment.setState(state);comment.setId(Integer.parseInt(idsStr[i]));commentService.update(comment);}JSONObject result=new JSONObject();result.put("success", true);ResponseUtil.write(response, result);return null;}}

    修改个人信息控制器Controller-BloggerAdminController.java:

package com.steadyjack.controller.admin;import java.io.File;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.apache.shiro.SecurityUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;import com.steadyjack.entity.Blogger;import com.steadyjack.service.BloggerService;import com.steadyjack.util.CryptographyUtil;import com.steadyjack.util.DateUtil;import com.steadyjack.util.ResponseUtil;import com.steadyjack.util.WebFileUtil;/** * title:BloggerAdminController.java * description:管理员博主Controller层 * time:2017年1月22日 下午10:27:50 * author:debug-steadyjack */@Controller@RequestMapping("/admin/blogger")public class BloggerAdminController {@Resourceprivate BloggerService bloggerService;/** * title:BloggerAdminController.java * description:修改博主信息 - 异步 * time:2017年1月22日 下午10:28:11 * author:debug-steadyjack * @param imageFile * @param blogger * @param request * @param response * @return * @throws Exception */@RequestMapping("/save")public String save(@RequestParam("imageFile") MultipartFile imageFile,Blogger blogger,HttpServletRequest request,HttpServletResponse response)throws Exception{if(!imageFile.isEmpty()){//对上传的文件(个人头像)进行处理:文件命名,转为FileString filePath=WebFileUtil.getSystemRootPath(request);String imageName=DateUtil.getCurrentDateStr()+"."+imageFile.getOriginalFilename().split("\\.")[1];imageFile.transferTo(new File(filePath+"static/userImages/"+imageName));blogger.setImageName(imageName);}int resultTotal=bloggerService.update(blogger);StringBuffer result=new StringBuffer();if(resultTotal>0){result.append("<script language='javascript'>alert('修改成功!');</script>");}else{result.append("<script language='javascript'>alert('修改失败!');</script>");}ResponseUtil.write(response, result);return null;}/** * title:BloggerAdminController.java * description:查询博主信息 - 异步 * time:2017年1月22日 下午10:41:45 * author:debug-steadyjack * @param response * @return * @throws Exception */@RequestMapping("/find")public String find(HttpServletResponse response)throws Exception{Blogger blogger=bloggerService.find();JSONObject jsonObject=JSONObject.fromObject(blogger);ResponseUtil.write(response, jsonObject);return null;}/** * title:BloggerAdminController.java * description:修改博主密码 * time:2017年1月22日 下午11:18:42 * author:debug-steadyjack * @param newPassword * @param response * @return * @throws Exception */@RequestMapping("/modifyPassword")public String modifyPassword(String newPassword,HttpServletResponse response)throws Exception{Blogger blogger=new Blogger();blogger.setPassword(CryptographyUtil.md5(newPassword, "debug"));int resultTotal=bloggerService.update(blogger);JSONObject result=new JSONObject();if(resultTotal>0){result.put("success", true);}else{result.put("success", false);}ResponseUtil.write(response, result);return null;}/** * title:BloggerAdminController.java * description:注销-退出登录 * time:2017年1月22日 下午11:18:17 * author:debug-steadyjack * @return * @throws Exception */@RequestMapping("/logout")public String  logout()throws Exception{SecurityUtils.getSubject().logout();return "redirect:/login.jsp";}}

    友情链接管理Controller--LinkAdminController.java

package com.steadyjack.controller.admin;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.steadyjack.entity.Link;import com.steadyjack.entity.PageBean;import com.steadyjack.service.LinkService;import com.steadyjack.util.ResponseUtil;/** * title:LinkAdminController.java * description:友情链接Controller层 * time:2017年1月23日 下午10:32:33 * author:debug-steadyjack */@Controller@RequestMapping("/admin/link")public class LinkAdminController {@Resourceprivate LinkService linkService;/** * title:LinkAdminController.java * description:分页查询友情链接信息 * time:2017年1月23日 下午10:32:41 * author:debug-steadyjack * @param page * @param rows * @param response * @return * @throws Exception */@RequestMapping("/list")public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));Map<String,Object> map=new HashMap<String,Object>();map.put("start", pageBean.getStart());map.put("size", pageBean.getPageSize());List<Link> linkList=linkService.list(map);Long total=linkService.getTotal(map);JSONObject result=new JSONObject();JSONArray jsonArray=JSONArray.fromObject(linkList);result.put("rows", jsonArray);result.put("total", total);ResponseUtil.write(response, result);return null;}/** * title:LinkAdminController.java * description:添加或者修改友情链接信息 * time:2017年1月23日 下午10:33:17 * author:debug-steadyjack * @param link * @param response * @return * @throws Exception */@RequestMapping("/save")public String save(Link link,HttpServletResponse response)throws Exception{//操作的记录条数int resultTotal=0;if(link.getId()==null){resultTotal=linkService.add(link);}else{resultTotal=linkService.update(link);}JSONObject result=new JSONObject();if(resultTotal>0){result.put("success", true);}else{result.put("success", false);}ResponseUtil.write(response, result);return null;}/** * title:LinkAdminController.java * description:删除友情链接信息 * time:2017年1月23日 下午10:33:47 * author:debug-steadyjack * @param ids * @param response * @return * @throws Exception */@RequestMapping("/delete")public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{String[] idsStr=ids.split(",");for(int i=0;i<idsStr.length;i++){linkService.delete(Integer.parseInt(idsStr[i]));}JSONObject result=new JSONObject();result.put("success", true);ResponseUtil.write(response, result);return null;}}

     最后是介绍一个全局的关于日期处理的处理器DateJsonValueProcessor.java:

package com.steadyjack.controller.admin;import java.text.SimpleDateFormat;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;/** * title:DateJsonValueProcessor.java * description:json-lib 日期处理器(处理含有日期字段的对象;处理含有日期字段的对象list) * time:2017年1月23日 下午10:29:09 * author:debug-steadyjack */public class DateJsonValueProcessor implements JsonValueProcessor{private String format;      public DateJsonValueProcessor(String format){          this.format = format;      }      public Object processArrayValue(Object value, JsonConfig jsonConfig) {return null;}public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {if(value == null)          {              return "";          }          if(value instanceof java.sql.Timestamp)          {              String str = new SimpleDateFormat(format).format((java.sql.Timestamp)value);              return str;          }          if (value instanceof java.util.Date)          {              String str = new SimpleDateFormat(format).format((java.util.Date) value);              return str;          }                    return value.toString(); }}

    相关的后端的页面已经在前一篇博客上传了,在本文的最后,将直接分享整个项目的源码与数据库。并也会分享到github,我会在第一条评论留言!如果下载不方便,可以加入群讨论:Java开源技术交流:583522159     鏖战八方群:391619659  我叫debug


原创粉丝点击