Skip to content

Akarin-Akari/easyBlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

博客系统后端

基于 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                             # 数据库初始化脚本

如何初始化数据库

1. 创建数据库

确保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)
);

2. 配置数据库连接

修改 src/main/resources/application.yml 中的数据库连接信息:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/blog_system
    username: your_mysql_username
    password: your_mysql_password

如何启动项目

方式一:使用Maven命令

# 进入项目目录
cd newintv

# 启动项目
mvn spring-boot:run

方式二:编译后运行JAR包

# 编译打包
mvn clean package

# 运行JAR包
java -jar target/blog-system-1.0.0.jar

启动成功后,服务将在 http://localhost:8080 运行。

API示例

1. 用户注册

请求:

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"
    }
}

2. 用户登录

请求:

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..."
    }
}

3. 获取文章列表

请求:

GET http://localhost:8080/api/articles

响应:

{
    "success": true,
    "message": "获取成功",
    "data": [
        {
            "id": 1,
            "title": "我的第一篇文章",
            "content": "这是文章内容...",
            "authorId": 1,
            "createdAt": "2023-12-01T10:00:00"
        }
    ]
}

4. 创建文章 (需要token)

请求:

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"
    }
}

5. 获取文章详情

请求:

GET http://localhost:8080/api/articles/1

响应:

{
    "success": true,
    "message": "获取成功",
    "data": {
        "id": 1,
        "title": "我的第一篇文章",
        "content": "这是文章内容...",
        "authorId": 1,
        "createdAt": "2023-12-01T10:00:00"
    }
}

使用Postman测试

测试流程

  1. 注册用户

    • POST http://localhost:8080/api/users/register
    • Body: {"username":"testuser","password":"123456"}
  2. 登录获取token

    • POST http://localhost:8080/api/users/login
    • Body: {"username":"testuser","password":"123456"}
    • 复制响应中的token
  3. 创建文章

    • POST http://localhost:8080/api/articles
    • Headers: Authorization: Bearer <你的token>
    • Body: {"title":"测试文章","content":"文章内容"}
  4. 获取文章列表

    • GET http://localhost:8080/api/articles
  5. 获取文章详情

    • GET http://localhost:8080/api/articles/1

curl命令示例

# 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/1

错误处理

API返回统一的错误格式:

{
    "success": false,
    "message": "错误描述信息",
    "data": null
}

常见错误:

  • 400 - 请求参数错误
  • 401 - 未授权或token无效
  • 404 - 资源不存在
  • 500 - 服务器内部错误

项目特性

  • ✅ 密码BCrypt加密存储
  • ✅ JWT Token认证机制
  • ✅ 文章按时间倒序排列
  • ✅ 获取最新10篇文章
  • ✅ RESTful API设计
  • ✅ 统一响应格式
  • ✅ 数据库外键约束

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages