1. 環境構築
このプロジェクトで使用される主なテクノロジーは、SpringBoot フレームワーク、MybatisPlus、Thmleaf テンプレート エンジンです。
pom 依存関係のインポート
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. データベース構築
create database blog;
use blog;
create table admin
(
id int auto_increment
primary key,
name varchar(255) not null,
pwd varchar(255) not null,
phone varchar(255) not null,
email varchar(255) not null,
status varchar(255) null,
repwd varchar(255) not null
)
auto_increment = 7;
create table article
(
id int auto_increment comment '主键id'
primary key,
title varchar(255) not null comment '文章标题',
summary varchar(255) not null comment '文章简介',
type varchar(255) not null comment '文章类型 0-博客 1-话题',
read_number tinyint(1) null comment '文章阅读量',
thumb_up_number int null comment '文章点赞数',
create_time datetime not null comment '创建时间',
tag varchar(255) not null
)
auto_increment = 6;
create table article_category
(
id int auto_increment
primary key,
article_id int not null,
type varchar(255) not null,
create_time datetime not null
);
create table article_content
(
id int auto_increment comment '主键id'
primary key,
content mediumtext not null comment '文章内容',
article_id varchar(255) not null comment '文章信息id',
create_time datetime not null comment '创建时间',
tag varchar(255) not null
);
create table category
(
id int auto_increment
primary key,
type varchar(255) not null,
create_time datetime not null,
tag varchar(255) not null
);
create table tag
(
id int auto_increment
primary key,
name varchar(255) not null,
number int not null
);
create table user
(
id int auto_increment
primary key,
username varchar(255) null,
password varchar(255) null
)
charset = utf8mb3
auto_increment = 5;
ここでは、自分でデータを追加できます。
ymlファイル
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
username: root
password: 20020702
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
mvc:
format:
date: yyyy-MM-dd
logging:
level:
com.example.mybatis_plus.sys.mapper: debug
ここで mysql アドレスを変更します。
3. インターセプターの構成
ログイン構成
package com.guo.blog.config;
import com.guo.blog.interceptor.UserLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class LoginConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册TestInterceptor拦截器
InterceptorRegistration registration = registry.addInterceptor(new UserLoginInterceptor());
registration.addPathPatterns("/**"); //所有路径都被拦截
registration.excludePathPatterns( //添加不拦截路径
"/", //首页路径
"/blog/toLogin", //登录路径
"/blog/login",
"/**/*.html", //html静态资源
"/**/*.js", //js静态资源
"/**/*.css" //css静态资源
);
}
}
UserLoginInterceptor
package com.guo.blog.interceptor;
import com.guo.blog.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class UserLoginInterceptor implements HandlerInterceptor {
/*
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("执行了拦截器的preHandle方法");
try {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("login");
if (user != null) {
return true;
}
response.sendRedirect(request.getContextPath() + "/blog/toLogin");
} catch (Exception e) {
e.printStackTrace();
}
return false;
//如果设置为false时,被请求时,拦截器执行到此处将不会继续操作
//如果设置为true时,请求将会继续执行后面的操作
}
/*
* 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("执行了拦截器的postHandle方法");
}
/*
* 整个请求结束之后被调用,也就是在DispatchServlet渲染了对应的视图之后执行(主要用于进行资源清理工作)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("执行了拦截器的afterCompletion方法");
}
}
実際にはここで直接実行できます。ただし、私のプロジェクトはインターセプターなので、使用したくない場合は削除できます。
特定の場所:
4. フロントエンドページの表示
ホームindex.html
ログインページ
ブログのトップページに入る
記事の分類
ここでは自分自身を美しくすることができます。機能は実現していますが、シンプルで荒いですね(笑)
小論を書く
この形も美化する必要があるので、今後少しずつ更新していきたいと思います。引き続きフォローしていただいても構いません!
5. バックグラウンド管理ページの表示
バックグラウンドログイン
ログインに成功したらメインインターフェイスに入ります
ブログリスト
ユーザーリスト
管理者リスト
将来的には、管理者権限、ロール、スーパー管理者、その他の機能など、いくつかの新しい機能が管理ページに追加される予定です。
リスト表示には特定のデータ ページもいくつか追加されています。
プロジェクトのソース コードのアドレス: https://github.com/guo0929/SpringBoot-thmleaf-
興味のある友達はダウンロードして見てください。実際にダウンロードしてすぐに使用できます。
応援プログラム猿