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

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

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

仪表盘实现效果

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

后台首页 AdminController

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

java 复制代码
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private 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。实体对象不满足所需渲染属性的情况下,创建自定义属性视图对象,

java 复制代码
@Data
public class LatestArticleVO {

    private Long id;
    private String title;
    private Integer hits;

}

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

ArticleMapper.xml

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属性的名一致

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

    List<HotArticleVO> selectHotArticle();

    List<LatestArticleVO> selectLatestArticle();

}

IArticleService,创建service接口类

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

    public List<HotArticleVO> selectHotArticle();

    List<LatestArticleVO> selectLatestArticle();

}

ArticleServiceImpl,调用mapper层方法

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

    @Autowired
    private ArticleMapper articleMapper;

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

    @Override
    public List<LatestArticleVO> selectLatestArticle() {
        return articleMapper.selectLatestArticle();
    }

}

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

使用thymeleaf模板引擎渲染

html 复制代码
  <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>
相关推荐
IT学长编程1 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
艾伦~耶格尔2 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
man20172 小时前
基于spring boot的篮球论坛系统
java·spring boot·后端
Java探秘者3 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
苹果醋33 小时前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
潘多编程3 小时前
Spring Boot与GraphQL:现代化API设计
spring boot·后端·graphql
2401_857622665 小时前
Spring Boot新闻推荐系统:性能优化策略
java·spring boot·后端
ZhongruiRao5 小时前
Springboot+PostgreSQL+MybatisPlus存储JSON或List、数组(Array)数据
spring boot·postgresql·json
IT学长编程5 小时前
计算机毕业设计 Java酷听音乐系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·音乐系统·计算机毕业设计选题
AskHarries5 小时前
如何优雅的处理NPE问题?
java·spring boot·后端