博客分类管理

 这是展示页面,可以看出来,和归档共用一个页面,只是分类名不相同。

所以,1.需要在前端进行判断,并且在实体类中定义一个标志位并设置它的set方法。

<div class="categorys-title" th:if="${archive.temp}=='archives'" th:text="'时间:'+${archive.data}+' '+'文章数:'+${archive.count}"></div>
   <div class="categorys-title" th:if="${archive.temp}=='category'" th:text="${archive.cname}+' '+'文章数:'+${archive.count}"></div>

2.在查询数据的时候,需要先遍历出分类id和分类名称。

List<Categories> allCategories = categoryService.getAllCategories();

3.然后遍历每个list集合,设置每个分类的文章列表和对应的数量,最后整合到实体bean中,这个实体bean基本和归档实体一样。

 for (Categories c:allCategories
                ) {
//           设置每个分类下的文章列表和对应的数量
            Integer kid = c.getKid();
            example.createCriteria().andCategoriesEqualTo(kid);
            List<Contents> contents = contentsMapper.selectByExample(example);
            long l = contentsMapper.countByExample(example);
            c.setCount((int) l);
            c.setList(contents);

        }

4.传到前端进行展示即可

主页显示的文章需要展示分类。

这里使用了左连接单条件查询。

 <select id="index" resultMap="Index">
    SELECT c.cid,c.title,c.tags,k.cname
    FROM t_contents c
    LEFT JOIN t_category k on c.categories = k.kid
  </select>

需要注意的是,查询出来的k.name即分类名,需要存储到contents的categories中,所以设置resultMap为

  <resultMap id="Index" type="com.sk.blog.bean.Contents">
    <id column="cid" jdbcType="INTEGER" property="cid" />
    <id column="cid" jdbcType="INTEGER" property="cid" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="created" jdbcType="INTEGER" property="created" />
    <result column="tags" jdbcType="VARCHAR" property="tags" />
    <result column="cname" jdbcType="VARCHAR" property="categories" />
  </resultMap>

 首页左侧栏展示分类

 主要使用了ajax在访问首页时异步向后台发送请求

代码为:

<script>
    $(function(){
        $.ajax({
            url:"/indexCategories",
            success:function(result) {
                $.each(result, function (i, v) {
                    $("#categories").append("<li> <a class='indexCategories' href='/getContentsByCategoryId/"+v.kid+"'>"+v.cname+"("+v.count+")"+"</a></li>")
                })

            }
            })
    })
</script>

这里遍历了前端的list集合,为ul标签添加n个li标签

cotroller的语句为:

 public List<Categories> indexCategories()
    {
       //获取所有的分类id和分类名
        List<Categories> allCategories = categoryService.getAllCategories();
        for (Categories c:allCategories
                ) {
            //根据分类id查询对应的文章数量
            Integer cid = c.getKid();
            Integer count = categoryService.selectCountByCategories(cid);
            c.setCount(count);
        }
        return allCategories;
    }

查询所有的类别的id和名字的sql语句为:

SELECT kid,cname
FROM t_category

根据分类id查询对应的文章数量的sql语句为:

    SELECT count(*) as count
    FROM t_contents
    WHERE categories=#{categories}

 2019/4/20更新

分类和文章应该为多对多的关系,即一个文章可以有多个分类,一个分类也可以有多个文章,如下图所示

这时候需要在分类表和文章表之间建立一个关系表,这个表有两个字段为两个主键分别为文章表和分类表的外键。

这里需要注意的是,外键和主键的类型,长度等参数必须要一样。

在查询的时候主要使用内连接查询,速度比where多表查询速度快(查询某个分类下的文章列表)

SELECT c.cid,c.title,c.tags,c.created
FROM t_contents c
INNER JOIN t_relationship k ON c.cid = k.cid AND  k.kid = #{kid} AND c.status = 'publish'

 

转载于:https://www.cnblogs.com/shank/p/10708642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值