3.1 接口说明
接口url:/tags/hot
请求方式:GET
请求参数:无
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id":1,
"tagName":"4444"
}
]
}
TagsController:
package com.example.blog.controller;
import com.example.blog.service.TagService;
import com.example.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tags")
public class TagsController
{
@Autowired
private TagService tagService;
@GetMapping("/hot")
public Result hot()
{
Long limit = 6L;//查询最热门的6个标签
return tagService.hots(limit);
}
}
TagServiceImpl:
package com.example.blog.service.impl;
import com.example.blog.dao.mapper.TagMapper;
import com.example.blog.entity.Tag;
import com.example.blog.service.TagService;
import com.example.blog.vo.Result;
import com.example.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class TagServiceImpl implements TagService
{
@Autowired
private TagMapper tagMapper;
@Override
public Result hots(Long limit)
{
/**
* 1.标签所拥有的数量最多的标签为 最热标签
* 2.查询ms_article_tag,根据tag_id进行分组,并根据获得的每个分组的个数,再从大到小排列,取前limit个
* select count(*) as count,tag_id from ms_article_tag group by tag_id ORDER BY count DESC LIMIT 6
* 3.由于需要的是前limit个tag_id,所以count(*)不需要显示,用于排序就可以了
* select tag_id from ms_article_tag group by tag_id ORDER BY count(*) DESC LIMIT 6
*/
List<Long> hotsTagIds = tagMapper.findHotsTagIds(limit);
/**
* 最终需要的是tagId 和tagName ==>tag对象
*/
if(CollectionUtils.isEmpty(hotsTagIds))
{
return Result.success(Collections.emptyList());
}
List<Tag> tags = tagMapper.findTagsNameByTagIds(hotsTagIds);
return Result.success(tags);
}
@Override
public List<TagVo> findTagsById(Long articleId)
{
/*mybatisPlus无法进行多表查询*/
/*通过articleId查询对应的tag*/
List<Tag> tags = tagMapper.findTagsByArticleId(articleId);
return copyList(tags);
}
public List<TagVo> copyList(List<Tag> tags)
{
List<TagVo> tagVoList = new ArrayList<>();
for(Tag tag:tags)
{
tagVoList.add(copy(tag));
}
return tagVoList;
}
public TagVo copy(Tag tag)
{
TagVo tagVo = new TagVo();
BeanUtils.copyProperties(tag,tagVo);
return tagVo;
}
}
tagMapper查询最热门的前limit个标签,以及根据标签ID查询对应的标签名
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis配置文件-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.blog.dao.mapper.TagMapper">
<!--通过文章Id查询对应的标签列表
public List<Tag> findTagsByArticleId(Long articleId);-->
<select id="findTagsByArticleId" parameterType="long" resultType="com.example.blog.entity.Tag">
select id,avatar,tag_name tagName from ms_tag where id in
(select tag_id from ms_article_tag
where ms_article_tag.article_id = #{articleId})
</select>
<!--List<Tag> findTagsNameByTagIds(List<Long> hotsTagIds);
select id,tag_name as tagName from ms_tag where id in (1,2,3)-->
<select id="findTagsNameByTagIds" resultType="com.example.blog.entity.Tag" parameterType="list">
select id,tag_name as tagName from ms_tag
where id in
<foreach collection="hotsTagIds" item="tagId" separator="," open="(" close=")">
#{tagId}
</foreach>
</select>
</mapper>