您的位置:澳门新葡8455最新网站 > 编程教学 > 澳门新葡萄京娱乐场博客的后台实现

澳门新葡萄京娱乐场博客的后台实现

发布时间:2019-10-05 16:06编辑:编程教学浏览(106)

    在眼下我们早就形成了博客类别的增加修改删除分页查询操作了,以往大家就来形成了博客的拉长修改

    第一成立t_blog表

    CREATE TABLE `t_blog` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '博客类型', `title` VARCHAR NOT NULL COMMENT '博客题目', `summary` VARCHAR DEFAULT NULL COMMENT '博客摘要', `releaseDate` DATETIME DEFAULT NULL COMMENT '发布日期', `clickHit` INT DEFAULT NULL COMMENT '评论次数', `replyHit` INT DEFAULT NULL COMMENT '回复次数', `content` TEXT COMMENT '博客内容', `keyWord` VARCHAR DEFAULT NULL COMMENT '关键字', `type_id` INT DEFAULT NULL COMMENT '外键关联博客类别', PRIMARY KEY , KEY `type_id` (`type_id`), CONSTRAINT `t_blog_ibfk_1` FOREIGN KEY (`type_id`) REFERENCES `t_blogtype`  ENGINE=INNODB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;
    

    id 博客主键type_id为涉嫌外键列 代表所属博客类别

    然后创立对应的实体类

    package ssm.blog.entity;import java.util.Date;import java.util.LinkedList;import java.util.List;/** * @Description 博客实体 * @author xp * */public class Blog { private Integer id; private String title; private String summary; private Date releaseDate; private Integer clickHit; private Integer replyHit; private String content; private String keyWord; //关键字,用空格隔开 private BlogType blogType; //博客类型 private Integer blogCount; //博客数量,非博客实际属性,用于根据发布日期归档查询 private String releaseDateStr; //发布日期的字符串,只取年月 private List<String> imageList = new LinkedList<String>();//博客里存的图片,主要用于展示缩略图 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public Date getReleaseDate() { return releaseDate; } public void setReleaseDate(Date releaseDate) { this.releaseDate = releaseDate; } public Integer getClickHit() { return clickHit; } public void setClickHit(Integer clickHit) { this.clickHit = clickHit; } public Integer getReplyHit() { return replyHit; } public void setReplyHit(Integer replyHit) { this.replyHit = replyHit; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getKeyWord() { return keyWord; } public void setKeyWord(String keyWord) { this.keyWord = keyWord; } public BlogType getBlogType() { return blogType; } public void setBlogType(BlogType blogType) { this.blogType = blogType; } public List<String> getImageList() { return imageList; } public void setImageList(List<String> imageList) { this.imageList = imageList; } }
    

    其中

    blogType 是该博客所属的博客类型 属于多对一的涉及blogCount 博客数量,非博客实际质量,用于依照发布日期归档查询releaseDateStr 发表日期的字符串,只取年月

    接下去正是dao

     package ssm.blog.dao;import java.util.List;import java.util.Map;import ssm.blog.entity.Blog;/** * @Description 博客Dao接口 * @author xp * */public interface BlogDao { // 分页查询博客 public List<Blog> listBlog(Map<String, Object> map); // 获取总记录数 public Long getTotal(Map<String ,Object> map); // 根据博客类型的id查询该类型下的博客数量 public Integer getBlogByTypeId(Integer typeId); //添加博客 public Integer saveBlog(Blog blog); //更新博客 public Integer updateBlog(Blog blog); //删除博客 public Integer deleteBlog(Integer id); //通过id获取博客 public Blog getById(Integer id);
    

    接下去就dao对应mapper文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="ssm.blog.dao.BlogDao"> <resultMap type="Blog" > <id property="id" column="id"/> <result property="title" column="title"/> <result property="summary" column="summary"/> <result property="releaseDate" column="releaseDate"/> <result property="clickHit" column="clickHit"/> <result property="replyHit" column="replyHit"/> <result property="content" column="content"/> <result property="keyWord" column="keyWord"/> <association property="blogType" column="type_id" select="ssm.blog.dao.BlogTypeDao.getById"> </association> </resultMap> <select parameterType="Map" resultMap="BlogResult"> SELECT * FROM t_blog <where> <if test="title!=null and title!=''"> and title like "%"#{title}"%" </if> <if test="typeId!=null and typeId!=''"> and type_id=#{typeId} </if> <if test="releaseDateStr!=null and releaseDateStr!=''"> and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr} </if> </where> ORDER BY releaseDate DESC <if test="start!=null and end!=null"> limit #{start},#{end} </if> </select> <select parameterType="Map" resultType="Long"> SELECT COUNT FROM t_blog <where> <if test="title!=null and title!=''"> and title like "%"#{title}"%" </if> <if test="typeId!=null and typeId!=''"> and type_id=#{typeId} </if> <if test="releaseDateStr!=null and releaseDateStr!=''"> and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr} </if> </where> </select> <select parameterType="Integer" resultType="Integer"> select count from t_blog where type_id=#{typeId} </select> <insert parameterType="Blog" keyProperty="id" useGeneratedKeys="true"> insert into t_blog VALUES (NULL ,#{title},#{summary},now(),0,0,#{content},#{keyWord},#{blogType.id}) </insert> <update parameterType="Blog"> update t_blog <set> <if test="title!=null and title!=''"> title=#{title}, </if> <if test="summary!=null and summary!=''"> summary=#{summary}, </if> <if test="content!=null and content!=''"> content=#{content}, </if> <if test="blogType.id!=null"> type_id=#{blogType.id}, </if> <if test="clickHit!=null"> clickHit=#{clickHit}, </if> <if test="replyHit!=null"> replyHit=#{replyHit}, </if> <if test="keyWord!=null and keyWord!=''"> keyWord=#{keyWord}, </if> </set> where id=#{id} </update> <delete parameterType="java.lang.Integer"> DELETE FROM t_blog where id=#{id} </delete> <select parameterType="java.lang.Integer" resultMap="BlogResult"> SELECT * from t_blog where id=#{id} </select></mapper> 
    

    为何getTotal方法带参数呢,因为本人索要贯彻模糊分页查询的法力的,所以你对应询问记录的总量也要带上参数。其中的<if> <set>是mybatis提供给我们兑现动态sql的工具association : 一个错综复杂的连串涉及;好多结果将包成那系列型个中property实体类属性 column关联外键类 select给实体类属性开端化的议程因为大家的实体类是BlogType对象 所以大家得以依赖type_id的值从BlogTypeDao的getById方法把那条记下查询出来还要赋值给blogType属性

    接下去正是service达成

    package ssm.blog.service;import java.util.List;import java.util.Map;import ssm.blog.entity.Blog;import ssm.blog.entity.PageBean;/** * @author xp * @Description 博客Service接口 */public interface BlogService { // 分页查询博客 public List<Blog> listBlog(Map<String,Object> map); // 分页查询博客 public PageBean<Blog> listBlog(String title,PageBean<Blog> pageBean); // 根据博客类型的id查询该类型下的博客数量 public Integer getBlogByTypeId(Integer typeId); //添加博客 public Integer saveBlog(Blog blog); //更新博客 public Integer updateBlog(Blog blog); //通过id删除博客 public Integer deleteBlog(Integer id); //通过id获取博客 public Blog getById(Integer id); long getTotal(Map<String, Object> map);
    
    package ssm.blog.service.impl;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import org.springframework.stereotype.Service;import ssm.blog.dao.BlogDao;import ssm.blog.entity.Blog;import ssm.blog.entity.PageBean;import ssm.blog.service.BlogService;/** * @Description 博客Service实现类 * @author xp * */@Service("blogService")public class BlogServiceImpl implements BlogService { @Resource private BlogDao blogDao; public PageBean<Blog> listBlog(String title, PageBean<Blog> pageBean) { Map<String,Object> map = new HashMap<String,Object>(); //设置查询条件 map.put("title",title); //总记录放入pageBean pageBean.setTotal(blogDao.getTotal; map.put("start",pageBean.getStart; map.put("end",pageBean.getEnd; //把分页结果放入pageBean pageBean.setResult(blogDao.listBlog; return pageBean; } public List<Blog> listBlog(Map<String,Object> map) { return blogDao.listBlog; } public Integer getBlogByTypeId(Integer typeId) { return blogDao.getBlogByTypeId; } public Integer saveBlog(Blog blog) { return blogDao.saveBlog; } public Integer updateBlog(Blog blog) { return blogDao.updateBlog; } public Integer deleteBlog(Integer id) { return blogDao.deleteBlog; } public Blog getById(Integer id) { return blogDao.getById; } public long getTotal(Map<String, Object> map) { return blogDao.getTotal; }}
    

    一贯下去正是controller的落到实处

    package ssm.blog.controller.admin;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.serializer.SerializerFeature;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import ssm.blog.entity.Blog;import ssm.blog.entity.PageBean;import ssm.blog.lucene.BlogIndex;import ssm.blog.service.BlogService;import ssm.blog.service.CommentService;import ssm.blog.util.ResponseUtil;/** * @author xp * @Description 管理员博客Controller层 */@Controller@RequestMapping("/admin/blog")public class BlogAdminController { @Resource private BlogService blogService; //后台分页查询博客信息 @RequestMapping("/listBlog") public String listBlog( @RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, Blog s_blog, HttpServletResponse response) throws Exception { PageBean<Blog> pageBean = new PageBean<Blog>(Integer.parseInt, Integer.parseInt; pageBean = blogService.listBlog(s_blog.getTitle(), pageBean); //创建json对象 JSONObject result = new JSONObject(); //设置json序列化日期格式 JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd"; //禁止对象循环引用 //使用默认日期格式化 String jsonStr = JSONObject.toJSONString(pageBean.getResult(), SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat); //得到json数组 JSONArray array = JSON.parseArray; //把结果放入json result.put("rows", array); result.put("total", pageBean.getTotal; //返回 ResponseUtil.write(response, result); return null; } //更新或者新增博客 @RequestMapping(value = "/save") public String saveBlog(Blog blog,HttpServletResponse response) throws Exception { int resultTotal = 0; if(blog.getId{ //更新操作 resultTotal = blogService.updateBlog; //更新索引 blogIndex.updateIndex; }else{ //新增操作 resultTotal = blogService.saveBlog; //添加索引 blogIndex.addIndex; } JSONObject result = new JSONObject(); if(resultTotal > 0) { result.put("success", true); } else { result.put("success", false); } ResponseUtil.write(response, result); return null; } //删除博客 @RequestMapping(value = "delete") public String deleteBlog(@RequestParamString ids,HttpServletResponse response) throws Exception { String[] idsStr = ids.split; for(int i = 0; i < idsStr.length; i++) { int id = Integer.parseInt(idsStr[i]); //先删除博客所关联的评论 现在没有完成评论的功能 先注释 //commentService.deleteCommentByBlogId; blogService.deleteBlog; } JSONObject result = new JSONObject(); result.put("success", true); ResponseUtil.write(response, result); return null; } //通过id获取博客 @RequestMapping(value = "get") public String getById(@RequestParam String id,HttpServletResponse response) throws Exception { Blog blog = blogService.getById(Integer.parseInt; String jsonStr = JSONObject.toJSONString; JSONObject result = JSONObject.parseObject; ResponseUtil.write(response, result); return null; }}
    

    此间关键注意一下json体系化的时候//设置json系列化日期格式JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";设置系列化时date类别化的格式假设不设置 时间戳的格式,设置完格式化日期格式后 就动用该格式类别化日期

    再有二个标题正是目的循环援引的难题

    双重援用:一个对象中的八个属性同不时候引述同八个指标举例

     Object obj=new Object(); Map<String,Object> map=new HashMap<>(); map.put; map.put;//引用了同一个对象 System.out.println(JSON.toJSONString;
    

    循环引用:对象的性质之间存在相互引用导致循环,会唤起StackOverFlow至极举个例子:

    Map<String,Object> map1=new HashMap<>(); Map<String,Object> map2=new HashMap<>(); map1.put;//map1引用了map2 map2.put;//map2又引用了map1,导致循环引用 System.out.println(JSON.toJSONString;
    

    fastjson帮助循环引用/重复援引,况且是缺省张开的。

    • 率先个例子类别化后输出结果为:{"1":{},"2":{"澳门新葡萄京娱乐场 1.1"}}第八个指标平常系列化,第一个对象则用援引表示
    • 其次个列子体系化后输出结果为:{"1":{"1":{"$ref":".."}}}

    fastjson暗中认可对json类别化的时候进行巡回援引的检验,从而制止了出现StackOverFlow卓殊。当体系化后的JSON传输到浏览器还是另外语言中,那个json深入分析器不帮忙循环援用,进而导致数据遗失。你能够关闭fastjson的循环援引质量评定。全局配置关闭

    澳门新葡萄京娱乐场,非全局关闭

    JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
    

    当我们的博客属于同三个类型的时候 也正是说blogType属性同时就能够合世循环引用的状态 那样我们就需求关闭循环引用了。那样调节层写完了接下去大家能够动用postman测量试验了

    下一节我们将贯彻博客的前台达成

    末尾我们关怀一下自家的私有公众号把。关怀自己公众号就能够得到源码

    澳门新葡萄京娱乐场 2公众号

    本文由澳门新葡8455最新网站发布于编程教学,转载请注明出处:澳门新葡萄京娱乐场博客的后台实现

    关键词:

上一篇:没有了

下一篇:web综合开拓