📌 Goal
Based on the data table information provided by the user (table name, field definitions), automatically generate complete CRUD code according to the RuoYi framework specifications, including:
- Java backend code (Domain, Mapper, Service, ServiceImpl, Controller)
- MyBatis XML mapping file
- Vue frontend code (page component, API wrapper)
- Menu initialization SQL
📥 Input Definition
The user needs to provide the following information (which can be clarified through dialogue):
| Parameter | Required | Description | Example |
|---|---|---|---|
tableName |
✅ | Database table name | sys_product |
tableComment |
✅ | Table comment/function name | 产品管理 |
columns |
✅ | Field list (including type and comment) | See the example below |
packageName |
❌ | Package path, default com.ruoyi.system |
com.ruoyi.business |
moduleName |
❌ | Module name, by default the name after the table prefix | product |
businessName |
❌ | Business name, by default the table name without the prefix | product |
author |
❌ | Author name, default ruoyi |
zhangsan |
tplCategory |
❌ | Template type: crud/tree/sub, default crud |
crud |
Field definition example
{
"tableName": "sys_product",
"tableComment": "产品管理",
"columns": [
{"name": "product_id", "type": "bigint", "comment": "产品ID", "isPk": true, "isIncrement": true},
{"name": "product_name", "type": "varchar(100)", "comment": "产品名称", "isRequired": true, "isQuery": true},
{"name": "product_code", "type": "varchar(50)", "comment": "产品编码", "isRequired": true},
{"name": "category_id", "type": "bigint", "comment": "分类ID", "dictType": "product_category"},
{"name": "price", "type": "decimal(10,2)", "comment": "价格"},
{"name": "status", "type": "char(1)", "comment": "状态(0正常 1停用)", "dictType": "sys_normal_disable"},
{"name": "create_time", "type": "datetime", "comment": "创建时间"}
]
}
📤 Output Definition
Generate code with the following file structure:
输出文件清单:
├── java/
│ ├── domain/{ClassName}.java # 实体类
│ ├── mapper/{ClassName}Mapper.java # Mapper接口
│ ├── service/I{ClassName}Service.java # Service接口
│ ├── service/impl/{ClassName}ServiceImpl.java # Service实现
│ └── controller/{ClassName}Controller.java # REST控制器
├── xml/
│ └── {ClassName}Mapper.xml # MyBatis映射文件
├── vue/
│ ├── api/{businessName}.js # API封装
│ └── views/{moduleName}/{businessName}/index.vue # 页面组件
└── sql/
└── {businessName}Menu.sql # 菜单初始化SQL
📋 Workflow
Step 1: Information collection and validation
- Parse user request: identify the table name and field information
- Follow up on missing information: if necessary information is missing, proactively ask the user
- Infer default values:
className= convert the table name to PascalCase (remove the table prefix such assys_)moduleName= the module name after the table prefixbusinessName= the lowercase form of the table name after removing the prefix- Primary key field = the field among the fields
isPk=true, default{tableName}_id
Step 2: Variable preparation
Calculate all template variables based on the input:
核心变量:
- ${tableName} 表名
- ${tableComment} 表注释
- ${ClassName} 类名(大驼峰)
- ${className} 类名(小驼峰)
- ${moduleName} 模块名
- ${businessName} 业务名
- ${BusinessName} 业务名(首字母大写)
- ${packageName} 包路径
- ${author} 作者
- ${datetime} 生成日期
- ${pkColumn} 主键字段信息
- ${columns} 所有字段列表
- ${permissionPrefix} 权限前缀 (格式: moduleName:businessName)
Step 3: Code generation
Read and fill the templates in order:
- Read template file: from
templates/directory load the corresponding template - Variable replacement: replace
${变量名}with actual values - Conditional handling: process according to field configuration
#if/#foreachlogic - Output code: generate the final code file
Step 4: Self-check and delivery
- Code review: check whether the generated code complies with the specifications
- Dependency reminder: inform the user of dependencies or configurations that need to be added
- Usage instructions: provide follow-up operation guidance
⚠️ Constraints
-
Naming conventions:
- Class names must use PascalCase
- Variable names must use camelCase
- Package paths must be all lowercase
-
Coding standards:
- Java files use UTF-8 encoding
- Indentation uses 4 spaces
- Complete Javadoc comments must be included
-
Security Specifications:
- Controller must add
@PreAuthorizepermission annotation - Delete operations must add
@Loglog annotation - Sensitive fields (such as passwords) do not appear in list displays
- Controller must add
-
Prohibitions:
- ❌ Do not generate test classes (request separately if needed)
- ❌ Do not modify existing files (unless explicitly requested by the user)
- ❌ Do not hardcode any sensitive information
🔧 Field Type Mapping
| Database Type | Java Type | Description |
|---|---|---|
bigint |
Long |
Long integer |
int/integer |
Integer |
Integer |
varchar/char/text |
String |
String |
datetime/timestamp |
Date |
Date time |
date |
Date |
Date |
decimal/numeric |
BigDecimal |
High-precision numeric |
float/double |
Double |
Floating point |
tinyint(1)/bit |
Boolean |
Boolean |
📁 Template Reference
When generating code, please refer to the following template files:
- Java templates:
templates/java/files under the.vmdirectory - Vue templates:
templates/vue/files under the.vmdirectory - XML templates:
templates/xml/files under the.vmdirectory - SQL templates:
templates/sql/files under the.vmdirectory
💡 Usage Examples
Example dialogue 1: Basic generation
User: Help me generate code for a product management module, the table name is sys_product
Agent response:
- First confirm the field information
- Infer default configuration
- Generate complete code
- Provide usage instructions
Example dialogue 2: Specify configuration
User:
生成代码:
- 表名: biz_order
- 功能: 订单管理
- 包路径: com.ruoyi.business
- 字段: order_id(主键), order_no(订单号), customer_name(客户名), amount(金额), status(状态), create_time(创建时间)
Agent response:
已为您生成订单管理模块代码,包含以下文件:
Java后端:
- Order.java (实体类)
- OrderMapper.java (Mapper接口)
- IOrderService.java (Service接口)
- OrderServiceImpl.java (Service实现)
- OrderController.java (控制器)
MyBatis:
- OrderMapper.xml (映射文件)
Vue前端:
- order.js (API封装)
- index.vue (列表页面)
SQL脚本:
- orderMenu.sql (菜单初始化)
[具体代码内容...]
🔄 Version Description
- Compatible versions: RuoYi-Vue v3.9.x
- Frontend framework: Vue 2 + Element UI
- Backend framework: Spring Boot 2.x + MyBatis