基于 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>
相关推荐
呜呼~225141 小时前
前后端数据交互
java·vue.js·spring boot·前端框架·intellij-idea·交互·css3
问道飞鱼2 小时前
【Springboot知识】Springboot进阶-实现CAS完整流程
java·spring boot·后端·cas
Q_19284999062 小时前
基于Spring Boot的电影网站系统
java·spring boot·后端
阿moments4 小时前
SpringBoot3-第十篇(整合Web安全)
spring boot·安全·web安全
后端转全栈_小伵4 小时前
SQLite本地数据库的简介和适用场景——集成SpringBoot的图文说明
数据库·spring boot·后端·sqlite·学习方法
李长渊哦5 小时前
使用 Spring Boot 实现文件上传:从配置文件中动态读取上传路径
java·spring boot·spring
猿来入此小猿5 小时前
基于SpringBoot在线音乐系统平台功能实现十七
java·spring boot·后端·毕业设计·音乐系统·音乐平台·毕业源码
繁川5 小时前
Spring Boot缓存预热实战指南
spring boot·spring·缓存
苹果醋36 小时前
Vue3响应式数据: 深入分析Ref与Reactive
java·运维·spring boot·mysql·nginx
it噩梦6 小时前
springboot 工程使用proguard混淆
java·spring boot·后端