Article directory
1. Environment construction
The main technologies used in this project are: SpringBoot framework, MybatisPlus, Thmleaf template engine.
pom dependency import
<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. Database construction
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;
Here we can add some data by ourselves.
yml file
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
Change your mysql address here.
3. Interceptor configuration
LoginConfig
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方法");
}
}
It can actually run directly here. But my project is an interceptor, if you don't want to use it, you can delete it.
specific location:
4. Front-end page display
Home index.html
login page
Enter the blog homepage
Article classification
Here you can beautify yourself. The function is realized, but it is simple and rough haha~
write an essay
This form also needs to be beautified. This is the direction I will slowly update in the future. You can continue to follow me!
5. Background management page display
Background login
Enter the main interface after successful login
blog list
user list
administrator list
Some new functions will be added to the management page in the future, such as administrator permissions, roles, super administrators and other functions.
There are also some specific data pages added to the list display.
Project source code address: https://github.com/guo0929/SpringBoot-thmleaf-Interested
friends can download and have a look, it is really downloadable and ready to use!
cheer up program ape