基于 Spring Boot + MySQL 的轻量级博客系统后端API。
- Java 21
- Spring Boot 3.2.0
- MyBatis 3.0.3
- MySQL 8.0+
- BCrypt 密码加密
- JWT Token 认证
src/
├── main/
│ ├── java/com/blog/
│ │ ├── BlogSystemApplication.java # 启动类
│ │ ├── controller/ # 控制器层
│ │ │ ├── UserController.java # 用户控制器
│ │ │ └── ArticleController.java # 文章控制器
│ │ ├── service/ # 服务层
│ │ │ ├── UserService.java # 用户服务
│ │ │ └── ArticleService.java # 文章服务
│ │ ├── mapper/ # 数据访问层
│ │ │ ├── UserMapper.java # 用户Mapper
│ │ │ └── ArticleMapper.java # 文章Mapper
│ │ ├── entity/ # 实体类
│ │ │ ├── User.java # 用户实体
│ │ │ └── Article.java # 文章实体
│ │ ├── dto/ # 数据传输对象
│ │ │ ├── ApiResponse.java # 统一响应格式
│ │ │ ├── UserRegisterRequest.java # 注册请求
│ │ │ ├── UserLoginRequest.java # 登录请求
│ │ │ ├── UserLoginResponse.java # 登录响应
│ │ │ └── ArticleCreateRequest.java # 创建文章请求
│ │ └── util/ # 工具类
│ │ └── JwtUtil.java # JWT工具类
│ └── resources/
│ ├── application.yml # 配置文件
│ └── mapper/ # MyBatis XML
│ ├── UserMapper.xml # 用户SQL映射
│ └── ArticleMapper.xml # 文章SQL映射
└── test/ # 测试代码
database/
└── init.sql # 数据库初始化脚本
确保MySQL已安装并启动,然后执行以下SQL脚本:
mysql -u root -p < database/init.sql或者手动在MySQL客户端中执行:
-- 创建数据库
CREATE DATABASE IF NOT EXISTS blog_system CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE blog_system;
-- 用户表
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 文章表
CREATE TABLE articles (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
content TEXT NOT NULL,
author_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id)
);修改 src/main/resources/application.yml 中的数据库连接信息:
spring:
datasource:
url: jdbc:mysql://localhost:3306/blog_system
username: your_mysql_username
password: your_mysql_password# 进入项目目录
cd newintv
# 启动项目
mvn spring-boot:run# 编译打包
mvn clean package
# 运行JAR包
java -jar target/blog-system-1.0.0.jar启动成功后,服务将在 http://localhost:8080 运行。
请求:
POST http://localhost:8080/api/users/register
Content-Type: application/json
{
"username": "testuser",
"password": "123456"
}响应:
{
"success": true,
"message": "注册成功",
"data": {
"id": 1,
"username": "testuser",
"createdAt": "2023-12-01T10:00:00"
}
}请求:
POST http://localhost:8080/api/users/login
Content-Type: application/json
{
"username": "testuser",
"password": "123456"
}响应:
{
"success": true,
"message": "登录成功",
"data": {
"user": {
"id": 1,
"username": "testuser",
"createdAt": "2023-12-01T10:00:00"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}请求:
GET http://localhost:8080/api/articles响应:
{
"success": true,
"message": "获取成功",
"data": [
{
"id": 1,
"title": "我的第一篇文章",
"content": "这是文章内容...",
"authorId": 1,
"createdAt": "2023-12-01T10:00:00"
}
]
}请求:
POST http://localhost:8080/api/articles
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
{
"title": "我的新文章",
"content": "这是文章的具体内容..."
}响应:
{
"success": true,
"message": "创建成功",
"data": {
"id": 2,
"title": "我的新文章",
"content": "这是文章的具体内容...",
"authorId": 1,
"createdAt": "2023-12-01T11:00:00"
}
}请求:
GET http://localhost:8080/api/articles/1响应:
{
"success": true,
"message": "获取成功",
"data": {
"id": 1,
"title": "我的第一篇文章",
"content": "这是文章内容...",
"authorId": 1,
"createdAt": "2023-12-01T10:00:00"
}
}-
注册用户
- POST
http://localhost:8080/api/users/register - Body:
{"username":"testuser","password":"123456"}
- POST
-
登录获取token
- POST
http://localhost:8080/api/users/login - Body:
{"username":"testuser","password":"123456"} - 复制响应中的token
- POST
-
创建文章
- POST
http://localhost:8080/api/articles - Headers:
Authorization: Bearer <你的token> - Body:
{"title":"测试文章","content":"文章内容"}
- POST
-
获取文章列表
- GET
http://localhost:8080/api/articles
- GET
-
获取文章详情
- GET
http://localhost:8080/api/articles/1
- GET
# 1. 注册用户
curl -X POST http://localhost:8080/api/users/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"123456"}'
# 2. 用户登录
curl -X POST http://localhost:8080/api/users/login \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"123456"}'
# 3. 创建文章 (替换YOUR_TOKEN为实际token)
curl -X POST http://localhost:8080/api/articles \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"title":"我的文章","content":"文章内容"}'
# 4. 获取文章列表
curl http://localhost:8080/api/articles
# 5. 获取文章详情
curl http://localhost:8080/api/articles/1API返回统一的错误格式:
{
"success": false,
"message": "错误描述信息",
"data": null
}常见错误:
400- 请求参数错误401- 未授权或token无效404- 资源不存在500- 服务器内部错误
- ✅ 密码BCrypt加密存储
- ✅ JWT Token认证机制
- ✅ 文章按时间倒序排列
- ✅ 获取最新10篇文章
- ✅ RESTful API设计
- ✅ 统一响应格式
- ✅ 数据库外键约束