基于 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>
相关推荐
兰亭序咖啡1 小时前
学透Spring Boot — 010. 单元测试和Spring Test
spring boot·spring·单元测试
苹果酱05671 小时前
SpringCloud第二篇:注册中心Eureka
java·vue.js·spring boot·mysql·课程设计
圈圈编码1 小时前
WebSocket
java·网络·spring boot·websocket·网络协议·spring
小马爱打代码3 小时前
Spring Boot - 实现邮件发送
spring boot·后端
风象南7 小时前
SpringBoot中6种自定义starter开发方法
java·spring boot·后端
cg50171 天前
Spring Boot 的配置文件
java·linux·spring boot
橘猫云计算机设计1 天前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
有一只柴犬1 天前
深入Spring AI:6大核心概念带你入门AI开发
spring boot·后端
向阳2561 天前
SpringBoot+vue前后端分离整合sa-token(无cookie登录态 & 详细的登录流程)
java·vue.js·spring boot·后端·sa-token·springboot·登录流程
XiaoLeisj1 天前
【MyBatis】深入解析 MyBatis XML 开发:增删改查操作和方法命名规范、@Param 重命名参数、XML 返回自增主键方法
xml·java·数据库·spring boot·sql·intellij-idea·mybatis