框架概述
ThinkBoot 是一个基于 Spring Boot 3 的轻量级快速开发框架,专为 API / 客户端服务场景设计。
与若依(RuoYi)等重量级框架不同,ThinkBoot 不包含复杂的角色权限体系,只提供客户端最常用的 Token 认证功能。框架采用模块化设计,所有第三方依赖均可按需启用,无需复杂配置即可开箱即用。
设计哲学:让开发者更关注业务逻辑,减少繁琐的配置工作。
技术栈
| 组件 | 技术 | 版本 |
|---|---|---|
| 基础框架 | Spring Boot | 3.2.5 |
| Java 版本 | JDK | 17+ |
| 认证鉴权 | Sa-Token | 1.45.0 |
| ORM 框架 | MyBatis-Plus | 3.5.6 |
| 多数据源 | Dynamic Datasource | 4.3.0 |
| 缓存 | Spring Data Redis / Spring Cache | - |
| 连接池 | HikariCP | - |
| 工具库 | Hutool | 5.8.27 |
| API 文档 | SpringDoc (OpenAPI 3) | 2.5.0 |
快速开始
环境要求
- JDK 17 或以上
- Maven 3.6+
- MySQL 8.0+(可选)
- Redis(可选)
引入依赖
在你的 pom.xml 中引入 ThinkBoot 父 POM:
<parent>
<groupId>com.thinkboot</groupId>
<artifactId>think-boot</artifactId>
<version>1.0.0</version>
</parent>
配置文件
server:
port: 8080
spring:
datasource:
dynamic:
primary: master
datasource:
master:
url: jdbc:mysql://localhost:3306/your_db
username: root
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
think-boot:
auth:
enabled: true
database:
enabled: true
cors:
enabled: true
swagger:
enabled: true
运行项目
mvn clean install
cd think-boot-example
mvn spring-boot:run
访问 API 文档:http://localhost:8080/swagger-ui.html
模块说明
think-boot-core
核心模块,提供基础工具类和常量。包含常量定义、枚举、异常、工具类、对象转换(MapStruct)。
think-boot-web
Web 层模块,提供统一响应 R<T>、分页响应 PageResult<T>、全局异常处理、TraceId 追踪、Swagger 配置。
think-boot-auth
认证模块,基于 Sa-Token 1.45.0。支持 @IgnoreAuth 注解、配置白名单、双 Token 机制、设备级注销。
think-boot-database
数据库模块,基于 MyBatis-Plus。内置 BaseEntity、分页查询 PageQuery、多数据源支持(读写分离)。
think-boot-codegen
代码生成器模块,基于 MyBatis-Plus Generator。一键生成 Entity、Mapper、Service、Controller 等 CRUD 代码。
think-boot-redis
Redis 缓存模块,JSON 序列化,提供 RedisUtils 工具类,支持字符串、Hash、List 等操作。
think-boot-cache
缓存抽象层,基于 Spring Cache。支持 @Cacheable、@CachePut、@CacheEvict,多缓存配置(default/short/long)。
think-boot-security
安全防护模块。内置 XSS 防护(自动过滤)、接口幂等性(Redis Token 机制)、分布式锁(SETNX + Lua)。
think-boot-storage
对象存储模块,统一接口支持 MinIO、阿里云 OSS、腾讯云 COS,一行配置即可切换。
配置说明
ThinkBoot 遵循"使用原生配置,仅在原生不提供时才增强"的设计原则。原生配置使用 spring.*、sa-token.*、mybatis-plus.*,框架增强配置使用 think-boot.*。
Sa-Token 配置
sa-token:
token-name: Authorization
timeout: 2592000 # 30天
is-concurrent: true # 允许并发登录
is-share: false
token-style: uuid
完整配置参考
think-boot:
auth:
exclude-paths: # 白名单
- /login
- /register
database:
pagination:
max-limit: 500
overflow: true
cache:
ttl:
default-hours: 1
short-minutes: 10
long-hours: 24
storage:
type: minio # minio/aliyun/tencent
使用示例
创建 Controller
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public R<User> getById(@PathVariable Long id) {
return R.ok(userService.getById(id));
}
@PostMapping
public R<Void> create(@RequestBody User user) {
userService.save(user);
return R.ok();
}
@GetMapping("/list")
public R<PageResult<User>> list(PageQuery query) {
Page<User> page = new Page<>(query.getCurrent(), query.getSize());
Page<User> result = userService.page(page);
return R.ok(new PageResult<>(result));
}
}
跳过认证
// 方式一:@IgnoreAuth 注解(推荐)
@IgnoreAuth
@PostMapping("/login")
public R<String> login() { ... }
// 方式二:配置白名单
think-boot:
auth:
exclude-paths:
- /login
- /api/public/**
接口幂等性
// 自动模式(基于请求参数 MD5)
@Idempotent(time = 5, message = "请勿重复提交")
@PostMapping("/order")
public R<Void> createOrder(@RequestBody OrderDTO dto) { ... }
// Token 模式(前端先获取 Token)
@Idempotent(useToken = true, message = "Token 已失效")
@PostMapping("/order")
public R<Void> submitOrder(
@RequestHeader("X-Idempotent-Token") String token,
@RequestBody OrderDTO dto) { ... }
分布式锁
@DistributedLock(key = "'inventory:' + #productId", leaseTime = 10)
public void deductStock(Long productId, int quantity) {
Product product = productMapper.selectById(productId);
if (product.getStock() < quantity) {
throw new RuntimeException("库存不足");
}
product.setStock(product.getStock() - quantity);
productMapper.updateById(product);
}
项目结构
ThinkBoot/
├── pom.xml # 父 POM,统一管理依赖版本
├── think-boot-core/ # 核心模块(常量、枚举、异常、工具类)
├── think-boot-web/ # Web 模块(响应封装、异常处理、Swagger)
├── think-boot-auth/ # 认证模块(Sa-Token、拦截器)
├── think-boot-database/ # 数据库模块(MyBatis-Plus、分页)
├── think-boot-redis/ # Redis 模块(RedisUtils)
├── think-boot-storage/ # 对象存储模块(MinIO/OSS/COS)
├── think-boot-codegen/ # 代码生成模块
├── think-boot-security/ # 安全模块(XSS、幂等性、分布式锁)
├── think-boot-cache/ # 缓存抽象层(Spring Cache)
└── think-boot-example/ # 示例项目
最佳实践
- 按需引入模块:不要一次性引入所有模块,只引入需要的。如只需 REST API,引入 think-boot-web 和 think-boot-auth 即可。
- 使用 @IgnoreAuth 注解:在 Controller 方法上使用注解跳过认证,比配置文件更灵活。
- 使用 BaseEntity:让实体类继承 BaseEntity,自动包含 createdTime、updatedTime 等审计字段。
- 使用代码生成器:开发新功能时,先使用代码生成器生成基础 CRUD 代码,再添加业务逻辑。
- 编写单元测试:ThinkBoot 核心模块已覆盖完整单元测试,业务代码也应及时编写测试。
常见问题
不配置某个模块会影响框架启动吗?
不会!ThinkBoot 采用 @ConditionalOnProperty 条件加载机制,不配置的功能不会加载,不会报错。
如何跳过认证?
有三种方式:使用 @IgnoreAuth 注解(推荐)、在类上标注 @IgnoreAuth、在配置文件中使用 think-boot.auth.exclude-paths。
单数据源和多数据源如何选择?
90% 的场景使用单数据源即可。需要读写分离、分库分表时使用多数据源,使用 @DS 注解切换。
仓库地址
| 平台 | 地址 |
|---|---|
| GitHub | https://github.com/hongxinge/ThinkBoot |
| Gitee(国内镜像) | https://gitee.com/hongxinge/think-boot |