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

ops/2024/10/18 8:30:19/

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

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

仪表盘实现效果

显示文章总数、评论总数、最新文章和最新留言。实现步骤,首先后端获取文章评论相关数据,然后前端使用thymeleaf获取后端model中的数据进行渲染。
在这里插入图片描述

后台首页 AdminController

获取最新文章列表、最新评论列表和page对象

@Controller
@RequestMapping("/admin")
public class AdminController {@Autowiredprivate IArticleService articleService;@Autowiredprivate ICommentService commentService;/*** 后台首页*/@RequestMapping("/")public String home(Model model){//int articleTotal = articleService.count();//int commentTotal = commentService.count();PageHelper.startPage(1,5);List<LatestArticleVO> latestArticleVOList = articleService.selectLatestArticle();PageInfo<LatestArticleVO> articlePage = new PageInfo<LatestArticleVO>(latestArticleVOList);PageHelper.startPage(1,5,"created desc");List<Comment> commentList = commentService.list();PageInfo<Comment> commentPage = new PageInfo<>(commentList);model.addAttribute("articlePage",articlePage);model.addAttribute("commentPage",commentPage);return "admin/index";}@RequestMapping("/list")public String list(){return "admin/list";}@RequestMapping("/edit")public String edit(){return "admin/edit";}}

创建VO对象,LatestArticleVO。实体对象不满足所需渲染属性的情况下,创建自定义属性视图对象,

@Data
public class LatestArticleVO {private Long id;private String title;private Integer hits;}

编写最新文章列表的SQL、Mapper、service。首先,在Mapper中自定义查询SQL
ArticleMapper.xml

    <resultMap id="LatestArticleVOResult" type="LatestArticleVO"><id property="id" column="id"></id><result property="title" column="title"></result><result property="hits" column="hits"></result></resultMap><select id="selectLatestArticle" resultMap="LatestArticleVOResult">SELECT a.id, a.title,s.hits FROM t_article a LEFT JOIN t_statistic s ON s.article_id = a.id order by a.created desc</select>

ArticleMapper,方法名selectLatestArticle与上面select标签id属性的名一致

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {List<HotArticleVO> selectHotArticle();List<LatestArticleVO> selectLatestArticle();}

IArticleService,创建service接口类

public interface IArticleService extends IService<Article> {public List<HotArticleVO> selectHotArticle();List<LatestArticleVO> selectLatestArticle();}

ArticleServiceImpl,调用mapper层方法

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {@Autowiredprivate ArticleMapper articleMapper;@Overridepublic List<HotArticleVO> selectHotArticle() {return articleMapper.selectHotArticle();}@Overridepublic List<LatestArticleVO> selectLatestArticle() {return articleMapper.selectLatestArticle();}}

后台首页内容前端代码实现

使用thymeleaf模板引擎渲染

  <div class="content-page"><div class="content"><div class="container"><div class="row"><div class="col-sm-12"><h4 class="page-title">仪表盘</h4></div><div class="row"><div class="col-sm-6 col-lg-3"><div class="mini-stat clearfix bx-shadow bg-info"><span class="mini-stat-icon"><i class="fa fa-quote-right" aria-hidden="true"></i></span><div class="mini-stat-info text-right">发表了<span class="counter" th:text="${articlePage.total}">12</span>篇文章</div></div></div><div class="col-sm-6 col-lg-3"><div class="mini-stat clearfix bg-purple bx-shadow"><span class="mini-stat-icon"><i class="fa fa-comments-o" aria-hidden="true"></i></span><div class="mini-stat-info text-right">收到了<span class="counter" th:text="${commentPage.total}">10</span>条留言</div></div></div></div><div class="row"><div class="col-md-4"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title">最新文章</h4></div><div class="panel-body"><ul class="list-group"><li th:each="article:${articlePage.list}" class="list-group-item"><span class="badge badge-primary" th:text="${article.hits}">1</span><a target="_blank" th:href="${'/article/'+article.id}" th:text="${article.title}">Spring Boot 2 权威发布</a></li></ul></div></div></div><div class="col-md-4"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title">最新留言</h4></div><div class="panel-body"><ul class="list-group"><li th:each="comment:${commentPage.list}" class="list-group-item">[[${comment.author}]]于 [[${#dates.format(comment.created,'YYYY-MM-dd')}]]:<a th:href="${'/article/'+comment.articleId+'#comments'}" target="_blank" ><p th:text="${comment.content}">151235</p></a></li></ul></div></div></div></div></div></div></div></div>

http://www.ppmy.cn/ops/42031.html

相关文章

react的多级路由定义

在写实验室项目的时候&#xff0c;有一个需求&#xff0c;在二级路由页面点击按钮&#xff0c;跳转到详情列表页面&#xff0c;同时三级路由不用在导航栏显示&#xff0c;效果图如下&#xff1a; 前期的尝试&#xff1a; 在route,js文件这样定义的&#xff1a; {path: music,…

韵搜坊 -- java爬虫抓取数据

文章目录 三种抓取方式数据抓取的流程获取文章具体操作 获取用户获取图片jsoup操作 三种抓取方式 直接调用请求接口(最方便&#xff0c;这里使用该方法) HttpClient,OKHttp,RestTemplate,Hutool等网页渲染出明文内容后&#xff0c;从前端页面的内容抓取有些网站可能是动态请求…

Spark SQL

一、简介 Spark SQL 是 Spark 用于结构化数据(structured data)处理的 Spark 模块。 1.特点: ➢ 数据兼容方面 SparkSQL 不但兼容 Hive&#xff0c;还可以从 RDD、parquet 文件、JSON 文件中获取数据&#xff0c;未来版本甚至支持获取 RDBMS 数据以及 cassandra 等 NOSQL 数据…

Docker 中快速构建 Redis Cluster 集群

Docker 中快速构建 Redis Cluster 集群 目录 前言环境准备 所需软件配置网络 构建 Redis Cluster 镜像 创建自定义 Dockerfile构建镜像 启动 Redis 节点容器 启动命令 配置 Redis Cluster 集群 创建 Redis 集群验证集群状态 总结 前言 Redis 是一个高性能的键值对数据库&am…

头歌答案哪里找

头歌EduCoder平台实训答案在此&#xff1a;实训笔记 有些作业是在难写&#xff0c;参考上面的连接地址吧&#xff0c;看看有没有自己想要的解答。

MotionDiffuse: Text-Driven Human Motion Generation withDiffusion Model # 论文阅读

URL https://arxiv.org/pdf/2208.15001 主页&#xff1a;https://mingyuan-zhang.github.io/projects/MotionDiffuse.html TD;DR 22 年 8 月商汤的文章&#xff0c;引用量 200。基于 SD&#xff0c;任务是输入文本的动作描述&#xff0c;生成对应的动作序列。 已有的 moti…

React 学习-4

1.React 事件处理-传入函数作为事件处理函数 <button onClick{activateLasers}>激活按钮 </button> 注意事项&#xff1a;&#xff08;1&#xff09;阻止默认行为必须使用preventDefault,不能使用return false &#xff08;2&#xff09;ES6 class 语法来定义一个…

RabbitMQ的基本组件有哪些?

RabbitMQ的基本组件有哪些&#xff1f; RabbitMQ介绍、解耦、提速、削峰、分发 详解、RabbitMQ安装 可视化界面讲解 RabbitMQ 不生产消息&#xff0c;他是消息的搬运工。 1. Producer: 消息的发布者。 2. Connection:producer/comsumer 和 Message Broker 之间的 TCP 连接。 3…