基于 Spring Boot 博客系统开发(四)

基于 Spring Boot 博客系统开发(四)

本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿
基于 Spring Boot 博客系统开发(三)👈👈

阅读排行榜实现

阅读排行榜实现涉及到联表查询,主要有两种方式,一种自定义SQL查询,另一种循环查询。

两种方式可以参考这篇文章:springboot 联表查询

这里主要以第一种查询来实现。

阅读排行榜主要涉及两个表分别article表和statistic表,两个表的关系图为一对一:

接下来使用自定义SQL方式来实现该功能。

1、创建VO包和热门文章类,VO表示视图对象

java 复制代码
@Data
public class HotArticleVO {
    private Long id;
    private String title;
    private Integer hits;
}

2、编写mapper.xml文件,在ArticleMapper.xml 添加查询SQL

xml 复制代码
<mapper namespace="cn.qvtu.web.mapper.ArticleMapper">

    <resultMap id="HotArticleVOResult" type="HotArticleVO">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="hits" column="hits"></result>
    </resultMap>
    
    <select id="selectHotArticle" resultMap="HotArticleVOResult">
        SELECT a.id, a.title,s.hits FROM t_article a LEFT JOIN t_statistic s ON s.article_id = a.id order by s.hits desc
    </select>
    
</mapper>

这里type="HotArticleVO" 没有写全包名路径,需要配置XML映射文件中指定的实体类别名路径

bash 复制代码
# 配置XML映射文件中指定的实体类别名路径
mybatis-plus.type-aliases-package=cn.qvtu.web.domain,cn.qvtu.web.vo

3、Mapper类添加执行方法,跟select id一致

java 复制代码
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    public List<HotArticleVO> selectHotArticle();
    
}

4、Service 接口也添加对应调用方法

IArticleService 接口

java 复制代码
public interface IArticleService extends IService<Article> {

    public List<HotArticleVO> selectHotArticle();

}

ArticleServiceImpl 实现类

java 复制代码
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<HotArticleVO> selectHotArticle() {
        return articleMapper.selectHotArticle();
    }
}

5、Controller 调用articleService.selectHotArticle获取热点文章集合

HomeController.java

java 复制代码
 @RequestMapping("/")
    public String home(@RequestParam(defaultValue = "1") Integer pageNum, Model model){
        PageHelper.startPage(pageNum, 5);
        List<Article> articleList = articleService.list();
        PageInfo<Article> articlePage = new PageInfo<>(articleList);
        model.addAttribute("articlePage",articlePage);

        PageHelper.startPage(1, 10);
        List<HotArticleVO> hotArticleList = articleService.selectHotArticle();
        model.addAttribute("hotArticleList",hotArticleList);

        return "client/index";
    }

6、前端使用thymeleaf渲染

html 复制代码
<!-- 阅读排行榜 -->
    <div class="am-u-md-4 am-u-sm-12 blog-sidebar">
        <div class="blog-sidebar-widget blog-bor">
            <h2 class="blog-text-center blog-title"><span>阅读排行榜</span></h2>
            <div style="text-align: left">

                <th:block th:each="article,stat:${hotArticleList}" >
                    <a  style="font-size: 15px;" th:href="${'/article/'+article.id}">[[${stat.count}]]、[[${article.title}]]([[${article.hits}]])</a>
                    <hr style="margin-top: 0.6rem;margin-bottom: 0.4rem" />
                </th:block>

            </div>
        </div>
    </div>

7、实现效果

相关推荐
magic 24539 分钟前
第2章——springboot核心机制
java·spring boot·spring
Q_Q196328847540 分钟前
python小区物业管理系统-小区物业报修系统
开发语言·spring boot·python·django·flask·node.js·php
苹果酱05672 小时前
[数据库之十一] 数据库索引之联合索引
java·vue.js·spring boot·mysql·课程设计
金融数据出海3 小时前
黄金、碳排放期货市场API接口文档
java·开发语言·spring boot·后端·金融·区块链
胡斌附体3 小时前
微服务中 本地启动 springboot 无法找到nacos配置 启动报错
java·spring boot·微服务·yml·naocs yml
向哆哆5 小时前
Spring Boot快速开发:从零开始搭建一个企业级应用
java·spring boot·后端
小马爱打代码6 小时前
Spring Boot + MyBatis-Plus 高并发读写分离实战
spring boot·mybatis
eternal__day12 小时前
Spring Boot 实现验证码生成与校验:从零开始构建安全登录系统
java·spring boot·后端·安全·java-ee·学习方法
宛如昨晚没早睡15 小时前
SpringBoot的自动配置和起步依赖原理
spring boot
源码云商15 小时前
Spring Boot + Vue 实现在线视频教育平台
vue.js·spring boot·后端