博客分类管理
这是展示页面,可以看出来,和归档共用一个页面,只是分类名不相同。
所以,1.需要在前端进行判断,并且在实体类中定义一个标志位并设置它的set方法。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。<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.传到前端进行展示即可
以上的sql可能可以用一次查询代替
select c.cid,c.title,k.cname from contents c group by categories left join categories k on c.categories = k.kid
这里使用了左连接单条件查询。
<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>

更多精彩