From ec4379abe416cbfff630f15502c68efa387e9c78 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Tue, 1 Jul 2025 17:14:54 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20=E6=B7=BB=E5=8A=A0ReadMe=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 107 +++++++++++++----- .../src/main/java/cn/bunny/utils/VmsUtil.java | 7 +- .../static/src/views/main/MainForm.js | 23 +++- .../src/main/resources/templates/main.html | 15 ++- 4 files changed, 111 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 55e6a1a..30422df 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,90 @@ -# 代码生成器 +# Bunny Code Generator 🚀 -## 内置字段 +[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)[![Java Version](https://img.shields.io/badge/JDK-17-green.svg)]()[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.4.3-6DB33F.svg)]() -```java -// vm 不能直接写 `{` 需要转换下 -context.put("leftBrace", "{"); +Bunny Code Generator 是一个高效的数据库表结构到代码的生成工具,帮助开发者快速生成高质量的服务端代码,显著提升开发效率。 -// 当前的表名 -context.put("tableName", tableMetaData.getTableName()); +## ✨ 核心特性 -// 当前表的列信息 -context.put("columnInfoList", columnInfoList); +- **多数据源支持** - 支持通过数据库连接或直接解析SQL语句生成代码 +- **模板引擎自由** - 基于Velocity模板引擎,支持完全自定义代码模板 +- **灵活输出方式** - 支持即时代码预览和ZIP打包下载两种输出模式 +- **智能命名转换** - 自动将表名转换为大/小驼峰命名的类名 +- **动态模板选择** - 前端可交互式选择需要生成的模板文件组合 +- **注释保留** - 自动将表字段注释带入生成的代码中 -// 数据库sql列 -context.put("baseColumnList", String.join(",", list)); +## 🚀 快速入门 -// 当前日期 -String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date()); -context.put("date", date); +### 前置要求 -// 作者名字 -context.put("author", dto.getAuthor()); +- JDK 1.8 或更高版本 +- MySQL 5.7+ (或其他支持的数据库) +- Spring Boot 2.7+ -// 每个 Controller 上的请求前缀 -context.put("requestMapping", dto.getRequestMapping()); +### 安装步骤 -// 表字段的注释内容 -context.put("comment", dto.getComment()); +1. 克隆仓库: + ```bash + git clone https://github.com/yourusername/bunny-code-generator.git + ``` -// 设置包名称 -context.put("package", dto.getPackageName()); +2. 配置数据库连接 (`application.yml`): + ```yaml + bunny: + master: + database: your_database + spring: + datasource: + url: jdbc:mysql://localhost:3306/your_database + username: your_username + password: your_password + driver-class-name: com.mysql.cj.jdbc.Driver + ``` -// 将类名称转成小驼峰 -String toCamelCase = TypeConvertCore.convertToCamelCase(replaceTableName); -context.put("classLowercaseName", toCamelCase); +3. 启动应用: + ```bash + mvn spring-boot:run + ``` -// 将类名称转成大驼峰 -String convertToCamelCase = TypeConvertCore.convertToCamelCase(replaceTableName, true); -context.put("classUppercaseName", convertToCamelCase); -``` +4. 访问 `http://localhost:8080` 开始使用 -![wx+alipay](images/wx_alipay.png) \ No newline at end of file +## 🛠️ 模板变量参考 + +模板中可使用以下预定义变量: + +| 变量名 | 描述 | 示例值 | +| -------------------- | ------------------ | ------------------ | +| `tableName` | 原始表名 | `user_info` | +| `classLowercaseName` | 小驼峰类名 | `userInfo` | +| `classUppercaseName` | 大驼峰类名 | `UserInfo` | +| `columnInfoList` | 列信息列表 | `List` | +| `baseColumnList` | SQL列名字符串 | `id,name,age` | +| `package` | 包名 | `com.example.demo` | +| `author` | 作者名 | `YourName` | +| `date` | 当前日期 | `2023-07-20` | +| `comment` | 表注释 | `用户信息表` | +| `requestMapping` | Controller请求前缀 | `/api/user` | + +## 🤝 参与贡献 + +我们欢迎各种形式的贡献!请阅读 [贡献指南](CONTRIBUTING.md) 了解如何参与项目开发。 + +1. Fork 项目仓库 +2. 创建您的特性分支 (`git checkout -b feature/AmazingFeature`) +3. 提交您的更改 (`git commit -m 'Add some AmazingFeature'`) +4. 推送到分支 (`git push origin feature/AmazingFeature`) +5. 发起 Pull Request + +## 📄 许可证 + +本项目采用 MIT 许可证 - 详情请参阅 [LICENSE](LICENSE) 文件。 + +## ☕ 支持项目 + +如果这个项目对您有帮助,可以考虑支持我们: + +![WeChat & Alipay](images/wx_alipay.png) + +--- + +**Happy Coding!** 🎉 \ No newline at end of file diff --git a/generator-code-server/src/main/java/cn/bunny/utils/VmsUtil.java b/generator-code-server/src/main/java/cn/bunny/utils/VmsUtil.java index daacd0d..deff0b2 100644 --- a/generator-code-server/src/main/java/cn/bunny/utils/VmsUtil.java +++ b/generator-code-server/src/main/java/cn/bunny/utils/VmsUtil.java @@ -30,7 +30,7 @@ public class VmsUtil { // 大驼峰名称 String CamelCase = MysqlTypeConvertUtil.convertToCamelCase(className, true); // 小驼峰名称 - String camelCase = MysqlTypeConvertUtil.convertToCamelCase(className); + String smallCamelCase = MysqlTypeConvertUtil.convertToCamelCase(className); // 当前文件名 String filename = splitPaths[splitPathsSize]; @@ -52,8 +52,9 @@ public class VmsUtil { filename = CamelCase + typeMappingsFilename + "." + extension; } - if (filename.contains("vue") && !filename.contains("index")) { - filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension; + if ((filename.contains("vue") || filename.contains("ts") || filename.contains("js")) + && !filename.contains("index")) { + filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, smallCamelCase) + "-" + name + "." + extension; } splitPaths[splitPathsSize] = filename; diff --git a/generator-code-server/src/main/resources/static/src/views/main/MainForm.js b/generator-code-server/src/main/resources/static/src/views/main/MainForm.js index 4c8a727..41c519f 100644 --- a/generator-code-server/src/main/resources/static/src/views/main/MainForm.js +++ b/generator-code-server/src/main/resources/static/src/views/main/MainForm.js @@ -129,7 +129,12 @@ const MainForm = {
- + @@ -137,7 +142,13 @@ const MainForm = { @click="onClearGeneratorData">清空生成记录
- + + +
@@ -151,6 +162,8 @@ const MainForm = { onGeneratorCode: {type: Function, required: true}, // 清空生成记录 onClearGeneratorData: {type: Function, required: true}, + // 生成代码加载 + generatorCodeLoading: {type: Boolean, required: true}, }, data() { return { @@ -169,7 +182,8 @@ const MainForm = { tablePrefixes: '', webTemplates: '', serverTemplates: '' - } + }, + downloadLoading: ref(false), } }, methods: { @@ -273,6 +287,7 @@ const MainForm = { * @returns {Promise} */ async onDownloadZip() { + this.downloadLoading = true; try { const response = await axiosInstance({ url: "/vms/downloadByZip", @@ -314,6 +329,8 @@ const MainForm = { } catch (error) { console.error('下载失败:', error); } + + this.downloadLoading = false; }, /** diff --git a/generator-code-server/src/main/resources/templates/main.html b/generator-code-server/src/main/resources/templates/main.html index 758c248..09d8550 100644 --- a/generator-code-server/src/main/resources/templates/main.html +++ b/generator-code-server/src/main/resources/templates/main.html @@ -65,9 +65,8 @@ v-model:db-select="dbSelect" v-model:table-select="tableSelect"> - + } */ async onGeneratorCode() { + this.generatorCodeLoading = true; // 验证表单是否通过 const isValidate = this.$refs.mainFormRef.validateForm(); if (!isValidate) return; @@ -172,12 +174,15 @@ const {data, code, message} = await axiosInstance.post("/vms/generator", this.form); // 判断是否请求成功 - if (code !== 200) return; - else antd.message.success(message); + if (code !== 200) { + this.generatorCodeLoading = false; + return; + } else antd.message.success(message); // 显示生成的页面 this.generatorData = data; this.generatorPageFlag = true; + this.generatorCodeLoading = false; // 等待 DOM 更新,之后手动更新代码高亮 await this.$nextTick();