基于 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、实现效果

相关推荐
wasteland~22 分钟前
MyBatis:SpringBoot结合MyBatis、MyBatis插件机制的原理分析与实战
spring boot·mybatis·插件机制·jdk代理
〆、风神27 分钟前
Spring Boot 自定义定时任务组件深度解析:Quartz 集成与设计模式实战
spring boot·后端·设计模式
鸽鸽程序猿31 分钟前
【JavaEE】SpringBoot 统一功能处理
java·spring boot·java-ee
Mikey_n1 小时前
Vue + Spring Boot 整合全解析
前端·vue.js·spring boot
愤怒的代码1 小时前
Spring Boot一次接口请求涉及的完整执行链路
java·spring boot·后端
Java水解2 小时前
解救应用启动危机:Spring Boot的FailureAnalyzer机制
spring boot·后端
顺天gie2 小时前
idea中提高编译速度研究
java·spring boot·intellij-idea
醉卧雕龙舫 、3 小时前
二.springBoot项目集成ElasticSearch及使用
spring boot·elasticsearch
罗政3 小时前
AI工具箱源码+成品网站源码+springboot+vue
vue.js·人工智能·spring boot
宋冠巡4 小时前
SpringBoot集成ActiveMQ异常处理机制:若未捕获异常,消息会被重新投递
spring boot·activemq·java-activemq