SpringBoot+Mybatis Plus开发博客系统

一.下载layui社区的模版

https://fly.layui.com/store/FlyTemplate/

二.springboot热部署

参考(https://blog.youkuaiyun.com/panruola/article/details/87890234
2.1导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

2.2Compiler,勾选 “Make project automatically”
在这里插入图片描述
3.3.快捷键 Shift+Ctrl+Alt+/ ,选择 “Registry” ,选中打勾 “compiler.automake.allow.when.app.running”
在这里插入图片描述

三.导入到springboot项目中(抽取片段)

我们可以抽取功能的片段:比如comment.html中公共的css样式(title不一样,我们可以通过参数传过来)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="commentStyle">
    <meta charset="utf-8">
    <title th:text="${
   
   title}">基于 layui 的极简社区页面模版</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="fly,layui,前端社区">
    <meta name="description" content="Fly社区是模块化前端UI框架Layui的官网社区,致力于为web开发提供强劲动力">
    <link rel="stylesheet" th:href="@{
   
   /static/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{
   
   /static/css/global.css}">
</head>
<body>

引入:

<!--引入公共头部样式-->
<head th:replace="~{
   
   inc/comment::commentStyle(title='首页')}">

四.controller中防止非整形访问该URL会报异常,所以通过id:\\d*来只允许整形通过

    /*
    博客详细页面
    id:\d*只允许传的参数为整形
     */
    @GetMapping("/post/{
   
   id:\\d*}")
    public String detail(@PathVariable("id") Long id){
   
   
        return "post/detail";
    }

五.导航栏的信息通过项目启动就存进容器中,这样项目一运行就查询数据库中的数据

1.创建一个类实现ApplicationRunner和ServletContextAware类

  1. ApplicationRunner用于在项目运行就执行
  2. ServletContextAware用于获取容器上下文

2.运行项目查询数据库中数据,并存进上下文

package com.wcy.eblog.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.eblog.entity.MCategory;
import com.wcy.eblog.service.MCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * ApplicationRunner 容器在启动的时候就会执行
 * ServletContextAware获取上下文容器
 * 把分类(导航栏)存进上下文容器中 (便于速度更快)
 */
@Component
public class ContextStartUp implements ApplicationRunner, ServletContextAware {
   
   
    @Autowired
    private MCategoryService mCategoryService;

    ServletContext servletContext;
    @Override
    public void run(ApplicationArguments args) throws Exception {
   
   
        //查询status=0的所有分类导航栏 若为1则不查询
        List<MCategory> mCategoryList = mCategoryService.list(new QueryWrapper<MCategory>().eq("status",0));
        //把查询到的数据存进容器中
        servletContext.setAttribute("mCategoryList", mCategoryList);
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
   
   
        this.servletContext=servletContext;
    }
}

3.thymeleaf获取容器上下文的方法

            <li th:each="category:${
   
   #servletContext.getAttribute('mCategoryList')}">
                <a th:href="@{
   
   '/catalog/'+${
   
   category.id}}">[[${
   
   category.name}]]</a>
            </li>

六.thymeleaf处理时间显示成几小时前

              <!--对时间处理成几小时前-->
              <span th:datetime="${#dates.format(postVo.created, 'yyyy-MM-dd HH:mm')}" class="time"></span>
              <script src="https://cdn.bootcss.com/timeago.js/3.0.2/timeago.js"></script>
              <script>

                  // 自动更新
                  var timeagoInstance = timeago();// 实例
                  timeagoInstance.render(document.querySelectorAll('.time'),'zh_CN');
              </script>

七.注意layui分页的JS需要等页面加载完毕才执行

问题:防止layui分页消失,不会出现分页按钮
解决:

放到JQ的加载完毕才会执行的方法

$(function(){
   
   
           
           
 });

八.实现博客七天回复量排行榜(redis实现)

1.实现逻辑:

  1. 查询数据库中前七天的博客以及博客的评论数
  2. 通过redis的zset有序集合key存关于天数的(博客创建的那一天 有序map),然后zset里面的key存博客id之类的,分数存评论数
    比如:2020-7-2日有哪些博客创建了并且评论了,那么就会存进这个有序集合的key里面(见下图)

在这里插入图片描述
4. 因为web端需要展示博客的标题、评论数,所有还需要通过一个hash来存储这些数据。
5. 这些都需要设置一个过期时间 超出七天则清除
相关命令:
在这里插入图片描述
在这里插入图片描述

2.对应的service逻辑方法:

    //初始化本周热议
    @Override
    public void initWeekRank() {
   
   
        //1.查询七天内的博客
        //DateUtil.offset(new Date(), DateField.DAY_OF_MONTH,-7)获取当前时间的第前七天的时间
        List<MPost> mPosts = mPostMapper.selectList(new QueryWrapper<MPost>()
                .ge("created", DateUtil.offset(new Date(), DateField.DAY_OF_MONTH,-7))
                .gt("comment_count",0)//评论数大于0
        );
        //2.存进redis中
        for(MPost mPost:mPosts){
   
   
            String key="eblog:rank:time:"+DateUtil.format(mPost.getCreated(),"yyyy-MM-dd");
            //2.1//存进zset中
            redisUtil.zSet(key,mPost.getId(),mPost.getCommentCount());
            //2.2.计算当前时间与博客创建时间差  7-(18-14)
            long time=(7-DateUtil.between(new Date(),mPost.getCreated(), DateUnit.DAY))*24*60*60;
            //2.3设置key消失时间
            boolean expire = redisUtil.expire(key, time);
            //2.4由于前端需要显示标题、评论数等信息
            hashCachePostIdAndTitle(mPost);
        }

        //3.计算某七天的评论数并逆序排序
        zUnionPost7DaysForWeekRand();
    }

    //计算七天内的评论数
    private void zUnionPost7DaysForWeekRand() {
   
   
        String newKey="eblog:rank:7week";
        List<String> list=new ArrayList<>();
        for(int i=-7;i<0;i++){
   
   
            //这七天的key存进list里面 用户求并集
            String timeKey="eblog:rank:time:"+ DateUtil.format(DateUtil.offsetDay(new Date(),i),"yyyy-MM-dd");
            list.add(timeKey);
        }
        String key="eblog:rank:time:"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        redisUtil.zUnionAndStore(key,list,newKey);//timeKey
    }

    //把博客信息存进redis中
    private void hashCachePostIdAndTitle(MPost mPost) {
   
   
        //通过博客的ID来存储键
        String key="eblog:rank:post:"+mPost.getId();
        //计算当前时间与博客创建时间差  7-(18-14)
        long time=(7-DateUtil.between(new Date(),mPost.getCreated(), DateUnit.DAY))*24*60*60;
        //存进redis中并设置过期时间
        redisUtil
基于springboot_ssm的个人博客源代码: 个人博客系统主要用于发表个人博客,记录个人生活日常,学习心得,技术分享等,供他人浏览,查阅,评论等。本系统结构如下: (1)博主端: 登录模块:登入后台管理系统:首先进入登录页面,需要输入账号和密码。它会使用Shiro进行安全管理,对前台输入的密 码进行加密运算,然后与数据库中的进行比较。成功后才能登入后台系统。 博客管理模块: 博客管理功能分为写博客和博客信息管理。写博客是博主用来发表编写博客的,需要博客标题,然后选择博 客类型,最后将博客内容填入百度的富文本编辑器中,点击发布博客按钮即可发布博客。 博客类别管理模块:博主类别管理系统可以添加,修改和删除博客类型名称和排序序号。将会显示到首页的按日志类别区域。 游客可 以从这里查找相关的感兴趣的博客内容 评论信息管理模块:评论管理功能分为评论审核和评论信息管理两部分。评论审核是当有游客或自己发表了评论之后,博主需 要在后台管理系统中审核评论。若想将此评论显示在页面上则点击审核通过,否则点击审核不通过。 个人信息管理模块:修改博主的个人信息,可以修改昵称,个性签名,可以添加个人头像,修改个人简介; 系统管理功能模块:友情链接管理,修改密码,刷新系统缓存和安全退出,友情链接管理可以添加,修改,删除友情链接网址 (2)游客端: 查询博客: 查询具体哪一篇博客 查看博客内容: 查看博客内容 查看博主个人信息:查看博主个人简介 发表评论: 可以评论具体某篇博客 友情链接: 查看友情链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值