SpringBoot+Thmleaf实现个人博客项目

淩亂°似流年 2023-10-08 23:43 119阅读 0赞

文章目录

  • 1、环境搭建
  • 2、数据库搭建
  • 3、拦截器配置
  • 4、前端页面展示
  • 5、后台管理页面展示

1、环境搭建

本项目使用到的主要技术有:SpringBoot框架,MybatisPlus,Thmleaf模板引擎。

pom依赖导入

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <version>8.0.32</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.baomidou</groupId>
  13. <artifactId>mybatis-plus-boot-starter</artifactId>
  14. <version>3.5.3.1</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.projectlombok</groupId>
  22. <artifactId>lombok</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>

2、数据库搭建

  1. create database blog;
  2. use blog;
  3. create table admin
  4. (
  5. id int auto_increment
  6. primary key,
  7. name varchar(255) not null,
  8. pwd varchar(255) not null,
  9. phone varchar(255) not null,
  10. email varchar(255) not null,
  11. status varchar(255) null,
  12. repwd varchar(255) not null
  13. )
  14. auto_increment = 7;
  15. create table article
  16. (
  17. id int auto_increment comment '主键id'
  18. primary key,
  19. title varchar(255) not null comment '文章标题',
  20. summary varchar(255) not null comment '文章简介',
  21. type varchar(255) not null comment '文章类型 0-博客 1-话题',
  22. read_number tinyint(1) null comment '文章阅读量',
  23. thumb_up_number int null comment '文章点赞数',
  24. create_time datetime not null comment '创建时间',
  25. tag varchar(255) not null
  26. )
  27. auto_increment = 6;
  28. create table article_category
  29. (
  30. id int auto_increment
  31. primary key,
  32. article_id int not null,
  33. type varchar(255) not null,
  34. create_time datetime not null
  35. );
  36. create table article_content
  37. (
  38. id int auto_increment comment '主键id'
  39. primary key,
  40. content mediumtext not null comment '文章内容',
  41. article_id varchar(255) not null comment '文章信息id',
  42. create_time datetime not null comment '创建时间',
  43. tag varchar(255) not null
  44. );
  45. create table category
  46. (
  47. id int auto_increment
  48. primary key,
  49. type varchar(255) not null,
  50. create_time datetime not null,
  51. tag varchar(255) not null
  52. );
  53. create table tag
  54. (
  55. id int auto_increment
  56. primary key,
  57. name varchar(255) not null,
  58. number int not null
  59. );
  60. create table user
  61. (
  62. id int auto_increment
  63. primary key,
  64. username varchar(255) null,
  65. password varchar(255) null
  66. )
  67. charset = utf8mb3
  68. auto_increment = 5;

这里我们可以自行添加一些数据了。

yml文件

  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
  6. username: root
  7. password: 20020702
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. thymeleaf:
  10. cache: false
  11. mvc:
  12. format:
  13. date: yyyy-MM-dd
  14. logging:
  15. level:
  16. com.example.mybatis_plus.sys.mapper: debug

这里把自己的mysql地址改上去。

3、拦截器配置

LoginConfig

  1. package com.guo.blog.config;
  2. import com.guo.blog.interceptor.UserLoginInterceptor;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. @Configuration
  8. public class LoginConfig implements WebMvcConfigurer {
  9. @Override
  10. public void addInterceptors(InterceptorRegistry registry) {
  11. //注册TestInterceptor拦截器
  12. InterceptorRegistration registration = registry.addInterceptor(new UserLoginInterceptor());
  13. registration.addPathPatterns("/**"); //所有路径都被拦截
  14. registration.excludePathPatterns( //添加不拦截路径
  15. "/", //首页路径
  16. "/blog/toLogin", //登录路径
  17. "/blog/login",
  18. "/**/*.html", //html静态资源
  19. "/**/*.js", //js静态资源
  20. "/**/*.css" //css静态资源
  21. );
  22. }
  23. }

UserLoginInterceptor

  1. package com.guo.blog.interceptor;
  2. import com.guo.blog.pojo.User;
  3. import jakarta.servlet.http.HttpServletRequest;
  4. import jakarta.servlet.http.HttpServletResponse;
  5. import jakarta.servlet.http.HttpSession;
  6. import org.springframework.web.servlet.HandlerInterceptor;
  7. import org.springframework.web.servlet.ModelAndView;
  8. public class UserLoginInterceptor implements HandlerInterceptor {
  9. /*
  10. * 在请求处理之前进行调用(Controller方法调用之前)
  11. */
  12. @Override
  13. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  14. System.out.println("执行了拦截器的preHandle方法");
  15. try {
  16. HttpSession session = request.getSession();
  17. User user = (User) session.getAttribute("login");
  18. if (user != null) {
  19. return true;
  20. }
  21. response.sendRedirect(request.getContextPath() + "/blog/toLogin");
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. return false;
  26. //如果设置为false时,被请求时,拦截器执行到此处将不会继续操作
  27. //如果设置为true时,请求将会继续执行后面的操作
  28. }
  29. /*
  30. * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
  31. */
  32. @Override
  33. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  34. System.out.println("执行了拦截器的postHandle方法");
  35. }
  36. /*
  37. * 整个请求结束之后被调用,也就是在DispatchServlet渲染了对应的视图之后执行(主要用于进行资源清理工作)
  38. */
  39. @Override
  40. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  41. System.out.println("执行了拦截器的afterCompletion方法");
  42. }
  43. }

到这里其实就可以直接运行了。不过我的项目是做了拦截器的,如果不想用也可以把它删掉。
具体位置:
在这里插入图片描述

4、前端页面展示

首页 index.html

在这里插入图片描述

登陆页面

在这里插入图片描述

进入博客主页

在这里插入图片描述

文章分类

这里可以自己美化一下。功能是实现的,不过就是简单粗糙了一点 哈哈~
在这里插入图片描述

写文章

在这里插入图片描述
这个表格的话也得美化一下,这都是我以后要慢慢更新的方向。可以持续关注我哟!

5、后台管理页面展示

后台登录

在这里插入图片描述

登录成功后进入主界面

在这里插入图片描述

博客列表

在这里插入图片描述

用户列表

在这里插入图片描述

管理员列表

在这里插入图片描述
管理页面以后还会添加一些新的功能比如说管理员的权限,角色,超级管理员等功能。
还有列表展示方面加些具体数据页面。
项目源代码地址:https://github.com/guo0929/SpringBoot-thmleaf-
感兴趣的小伙伴可以下载看看,真的是下载即用哦!
加油叭 程序猿

发表评论

表情:
评论列表 (有 0 条评论,119人围观)

还没有评论,来说两句吧...

相关阅读