Kungfu Framework
website·Indexed 32 days ago
https://kungfu.wang/An enhancement framework for JFinal designed to build RESTful APIs. It features a code generator for automating Java Web application development, support for dynamic parameter parsing, tree-structured queries, and a JWT-based authentication interceptor. The ecosystem includes Kungfu-Admin, a backend management system, and Kungfu-Admin-UI, a frontend based on Vue 3.2, TypeScript, Vite 4, and Pinia.
What's inside Kungfu
- The KungFuPanda code generator uses pre-defined code templates with placeholders and variables to automate the creation of Java source code. This ensures consistent code style, increases development efficiency, and allows for custom configurations. The generator utilizes the JFinal Enjoy template engine, which is characterized by high performance, lightweight execution (no requirement for a Web container like Tomcat), and high customizability.
Overview of the Kungfu JFinal Enhancement Framework
Kungfu is an elegant enhancement framework based on JFinal for building standard MVC Java Web projects. It aims to accelerate development through several key capabilities:
- Simplified CRUD: Supports automatic construction of SQL query statements from dynamic parameters.
- Code Scaffolding: Provides a one-click layered code generation tool.
- Controller Layer Acceleration: Offers standardized exception handling, result formatting, and flexible parameter acquisition, validation, and conversion.
- Service Layer Acceleration: Supports dynamic parameter parsing for dynamic queries, tree-structure queries, and existence queries.
- Core Utilities: Handles permissions, stateless services, and type conversion with options for high customization (DIY).
Overview of Kungfu for JFinal
Kungfu is a rapid development library specifically designed for JFinal to build RESTful API services. It provides a flexible scaffolding to simplify CRUD operations and accelerate the development of the Controller and Service layers. Key features include:
- Rapid construction of JFinal RESTful API projects.
- Simple and flexible code generation capabilities.
- Support for dynamic construction of query SQL statements.
- Automated input parameter acquisition and conversion.
- Integrated parameter validation with friendly error messaging for front-end clients.
- Standardized API documentation for efficient front-end/back-end integration.
Overview of RESTful API parameter types
RESTful APIs typically use four types of input parameters:
- Path Parameters: Extracted from the URL path (e.g.,
/users/:id). - Query Parameters: Appended to the URL after a
?(e.g.,/users?id=123&name=John). - Request Body: The main body of the request, usually JSON or XML, sent via POST, PUT, or PATCH.
- Request Headers: Key-value pairs providing metadata (e.g.,
Content-Type: application/json).
- Path Parameters: Extracted from the URL path (e.g.,
Overview of Kungfu-Admin-UI features
Kungfu-Admin-UI is a backend management framework built on the Geeker-Admin framework. It utilizes a modern tech stack including Vue 3.2, TypeScript, Vite 4, Pinia, and Element-Plus. Key features include:
- ProTable Component: A configuration-based table component using
columnsto increase development efficiency. - State Management: Uses Pinia with a persistence plugin.
- Network Layer: TypeScript-based Axios wrapper with request interception and cancellation.
- UI/UX: Support for dark mode, i18n internationalization, and Element component size switching.
- Routing & Security: VueRouter for permission interception, button-level access control, and lazy loading.
- Performance: Multi-level nested page caching via KeepAlive.
- Developer Tooling: Integrated ESLint, Stylelint, Prettier, and a standardized commit workflow using husky, lint-staged, and commitlint.
- ProTable Component: A configuration-based table component using
Overview of Kungfu-Admin Backend Management System
Kungfu-Admin is an open-source, reusable backend management system designed for rapid development. It is built using the Kungfu backend framework (an enhancement of JFinal) and the Geeker-Admin frontend framework. It is suitable for various Web applications, including website administration backends, member centers, CMS, CRM, and OA systems.Overview of the Kungfu Code Generator
The Kungfu Code Generator is a tool designed to automatically generate Java Web application code based on user-defined data models, database table structures, and configuration files. It aims to accelerate project development, reduce coding errors by adhering to best practices, improve maintainability through consistent structure, and facilitate team collaboration by enforcing a unified coding style.Understand the relationship between Kungfu and JFinal
Kungfu is an enhancement framework for JFinal. It is designed to be easy to learn and use, providing a powerful and flexible scaffold that significantly simplifies CRUD operations and accelerates the development of the controller and service layers.Perform tree-table paginated queries
For tree-table structures, passing a code (e.g.,menuCode) in themodelMapis automatically interpreted by the server as aparentCodefilter to retrieve the child records of that specific node.// Request { "pageNumber": 1, "pageSize": 20, "orderColumnName": "updateTime", "orderBy": "desc", "modelMap": { "menuCode": "sys" } } // Resulting SQL select * from sys_menu where 1=1 and parent_code = 'sys' order by update_time desc limit 0,20;Implement user authentication using JWT
The system uses JSON Web Tokens (JWT) for interface access authentication. The process involves the server issuing a token upon successful login, which the client stores (e.g., in cookies or localStorage) and sends in theAuthorizationheader for subsequent requests. The server validates the token by checking expiration, signature correctness, and user information before granting access to the resource. If validation fails, a 401 (Unauthorized) status code is returned.Authorization: Bearer <your_jwt_token>Implement a Kungfu Panda Service for CRUD operations
TheSysApiServicedemonstrates how to implement a service layer extendingKungfuService. It handles the business logic for saving or updating a model, including setting audit fields likecreateUser,createTime,updateUser, andupdateTimebased on the currentUserInfoand system date.package io.kungfu.panda.modules.sys.service; import io.kungfu.panda.modules.sys.model.SysApi; import org.kungfu.core.KungfuService; import org.kungfu.core.R; import org.kungfu.core.UserInfo; import java.util.Date; public class SysApiService extends KungfuService<SysApi> { private final SysApi dao = SysApi.dao; public R saveOrUpdate(SysApi sysApi, UserInfo userInfo) { if (sysApi == null) { return R.fail(630, "信息不能为空"); } Date date = new Date(); if (sysApi.getId() != null) { sysApi.setUpdateUser(userInfo.getUserName()); sysApi.setUpdateUserId(userInfo.getUserId()); sysApi.setUpdateTime(date); if (sysApi.update()) { return R.ok("更新成功"); } return R.fail(641, "更新失败"); } else { sysApi.setCreateUser(userInfo.getUserName()); sysApi.setCreateUserId(userInfo.getUserId()); sysApi.setCreateTime(date); if (sysApi.save()) { return R.ok("保存成功"); } return R.fail(640, "保存失败"); } } }Integrate Swagger API documentation using jfinal-swagger-knife4j
To provide online API documentation, configure the Swagger routes, plugins, and handlers within your JFinal configuration methods.@Override public void configRoute(Routes me) { // swagger route me.add(new SwaggerRoutes()); } @Override public void configPlugin(Plugins me) { // swagger plugin me.add(new SwaggerPlugin()); } @Override public void configHandler(Handlers me) { // swagger handler me.add(new SwaggerHandler()); }