jf-helper 是一个 jfinal 框架的简化搭建后端开发环境助手包:一行依赖引入,通过继承 JfHelper 即可获得 yml 配置、注解式请求映射、依赖注入、统一返回契约、授权令牌、Redis 缓存、安全防护(防 DoS / 防注入 / 上传安全)与代码生成能力。
gitee 项目路径: https://gitee.com/saiya-tianma/jf-helper
1. 环境要求
| 项 | 要求 | 说明 |
|---|---|---|
| JDK | 8+ | 产物字节码为 major 52,可在 JDK 8 运行 |
| jfinal | 5.3.0(官方发行版) | 随依赖自动引入,请勿随意升级大版本 |
| 构建 | Maven 3.6+ | |
| 日志 | 任意 slf4j 实现 | show-sql: true 时必须提供(如 logback、log4j2-slf4j 桥) |
| 数据库驱动 | 按需自行引入 | 连接池用 druid,驱动不在依赖内 |
2. 引入依赖
<dependency> <groupId>io.github.89ck</groupId> <artifactId>jf-helper</artifactId> <version>2.3.0</version> </dependency>
3. 五分钟快速开始
第一步:让配置类继承 JfHelper:
public class AppConfig extends JfHelper {
@Override
public void addMappingKit(ActiveRecordPlugin arp) {
arp.addMapping(User.class); // 映射 Model 与表
}
public static void main(String[] args) {
JFinal.start("src/main/webapp", 5000, "/", 5);
}
}AppConfig 所在包会被当作路由与组件扫描根包:它及其所有子包下的 Controller、@Component 都会被自动发现。请把 AppConfig 放在项目的根包下。
第二步:在 src/main/resources 下新建 application.yml:
jFinal: # 根键必须精确为 jFinal(大小写敏感) dev-mode: true database: type: mysql url: jdbc:mysql://localhost:3306/test driver-class: com.mysql.cj.jdbc.Driver username: root password: your-password
第三步:写一个 Controller:
@Path("/user")
public class UserController extends Controller {
@Resource
private UserService userService; // 接口注入,实现类标 @Component
@Get // 仅接受 GET,方法不匹配返回 405 返回值序列化为 JSON
public User find() {
return userService.byId(getInt("id"));
}
}启动即可访问 /user/find?id=1 得到 JSON。