SSM博客实战(5)–获取文章的标签和分类的实现

avatar 2017年09月01日16:59:13 1 3463 views
博主分享免费Java教学视频,B站账号:Java刘哥
本文主要介绍,获取一篇文章的信息和其对应的 分类(可能是多个) 和 标签(可能是多个)。实现起来也很容易。

一、效果如下

  • 显示分类(面包屑导航的一部分)
 
  • 显示标签

一、数据表设计

article 表 category 表 tag 表   主要是关注 红色的部分,article表里的 article_category_ids 和 article_tag_ids 分别是 varchar 类型,用来存储 分类和标签的id。因为可能都存在多个,所以,准备以 逗号分隔,到时候,再从中取即可。   控制层 和 DAO层 这里就不贴代码了,相对随意。

二、业务层代码

这里主要关注业务层 代码,即 Service 部分的实现
  1. @Override
  2.     public void showArticleDetailById(Model model,Integer id) throws Exception {
  3.         Article article  = articleMapper.selectByPrimaryKey(id);
  4.         ArticleCustom articleCustom = new ArticleCustom();
  5.         BeanUtils.copyProperties(article,articleCustom);
  6.         model.addAttribute("articleCustom",articleCustom);
  7.         String category_ids = articleCustom.getArticleCategoryIds();
  8.         if(category_ids!=null && category_ids!="") {
  9.             String[] categorys = category_ids.split(",");
  10.             int categoryLength = categorys.length;
  11.             CategoryCustom[] categoryCustoms = new CategoryCustom[categoryLength];
  12.             for(int i=0;i<categoryLength;i++) {
  13.                 categoryCustoms[i] = categoryMapperCustom.selectCategoryById(Integer.valueOf(categorys[i]));
  14.             }
  15.             model.addAttribute("categoryCustoms",categoryCustoms);
  16.         }
  17.         String tag_ids = articleCustom.getArticleTagIds();
  18.         if(tag_ids!=null && tag_ids!="") {
  19.             String[] tags = tag_ids.split(",");
  20.             int tagLength = tags.length;
  21.             Tag[] tag = new Tag[tagLength];
  22.             for(int i=0;i<tagLength;i++) {
  23.                 tag[i] = tagMapper.selectByPrimaryKey(Integer.valueOf(tags[i]));
  24.             }
  25.             model.addAttribute("tag",tag);
  26.         }
  27.     }
  主要思想是 先通过 id 获取文章信息,然后再从文章信息里获取其分类和标签的值,然后依次从中取出并查询,最后在 发送到视图层。
  • 我这里多加了 if 语句,防止某个文章没有标签或者分类导致 报异常。
  • Article和ArticleCustom,Category和CategoryCustom其实没什么区别,后者是前者的扩展类。因为前者都是用 mybatis-generator 生成的,最好不要在里面改。而是写一个扩展类,继承前者就行。
  • 其中 分类是用的 其扩展类 CategoryCustom,因为我希望那个分类先显示父级分类,再显示子分类。就需要以 category 表的 pid 字段升序排序(父分类pid为0,子分类pid等于其父分类的id)。而标签只需要任意顺序显示就好啦,没关系的,所以直接用 Tag 对象存储。
 

三、视图层

面包屑的显示
  1. <%--面包屑导航 start--%>
  2. <nav class="breadcrumb">
  3.     <a class="crumbs" href="${pageContext.request.contextPath}">
  4.         <i class="fa fa-home"></i>首页
  5.     </a>
  6.     <c:forEach items="${categoryCustoms}" var="c">
  7.         <i class="fa fa-angle-right"></i>
  8.         <a href="${pageContext.request.contextPath/category/${c.categoryId}}">
  9.             ${c.categoryName}
  10.         </a>
  11.     </c:forEach>
  12.     <i class="fa fa-angle-right"></i>
  13.     正文
  14. </nav>
标签的显示
  1. <%--所属标签 start--%>
  2.     <div class="single-tag">
  3.         <ul class="" >
  4.             <c:forEach items="${tag}" var="t">
  5.             <li>
  6.                    <a href="${pageContext.request.contextPath}/tag/${t.tagId}" rel="tag">
  7.                         ${t.tagName}
  8.                    </a>
  9.             </li>
  10.             </c:forEach>
  11.         </ul>
  12.     </div>
  13.     <%--所属标签 end--%>
   

四、补充

其实这个方法感觉很烂,暂时没想到其他方法。之前用的方法是 使用三表关联查询,Mapper.xml 是这样写的。
  1. <resultMap id="ArticleCategoryTagResultMap" type="com.liuyanzhao.blog.po.custom.ArticleCustom">
  2.         <id column="article_id" property="articleId"></id>
  3.         <result column="article_user_id" property="articleUserId"></result>
  4.         <result column="article_title" property="articleTitle"></result>
  5.         <result column="article_content" property="articleContent"></result>
  6.         <result column="article_category_ids" property="articleCategoryIds"></result>
  7.         <result column="article_tag_ids" property="articleTagIds"></result>
  8.         <result column="article_view_count" property="articleViewCount"></result>
  9.         <result column="article_comment_count" property="articleCommentCount"></result>
  10.         <result column="article_like_count" property="articleLikeCount"></result>
  11.         <result column="article_post_time" property="articlePostTime"></result>
  12.         <result column="article_update_time" property="articleUpdateTime"></result>
  13.         <result column="article_is_comment" property="articleIsComment"></result>
  14.         <result column="article_status" property="articleStatus"></result>
  15.         <collection property="categoryList" javaType="ArrayList" ofType="com.liuyanzhao.blog.po.Category">
  16.             <id column="category_id" property="categoryId"></id>
  17.             <result column="category_name" property="categoryName"></result>
  18.         </collection>
  19.         <collection property="tagList" ofType="com.liuyanzhao.blog.po.Tag">
  20.             <id column="tag_id" property="tagId"></id>
  21.             <result column="tag_name" property="tagName"></result>
  22.         </collection>
  23.     </resultMap>
  24.     <select id="showArticleDetailById" parameterType="Integer" resultMap="ArticleCategoryTagResultMap">
  25.         SELECT  *
  26.         FROM
  27.         article,category,tag
  28.         WHERE
  29.         article.article_category_ids=category.category_id   and
  30.         article.article_tag_ids=tag.tag_id and
  31.         article.article_id=#{value}
  32.     </select>
但是这个方法会遇到一个问题,就是如果 文章没有标签或者分类(忘填了),则这篇文章会出现查询不到结果。所以暂时不用这个了。   本文地址:https://liuyanzhao.com/6139.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0