Commit 6e7d7aba934158d047c7c31bac356b3d18db4455

Authored by RuoYi
1 parent 388e36ed

代码生成支持自定义路径

ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java
... ... @@ -14,7 +14,7 @@ import javax.servlet.http.HttpServletRequest;
14 14 *
15 15 * @author ruoyi
16 16 */
17   -public class FileUtils
  17 +public class FileUtils extends org.apache.commons.io.FileUtils
18 18 {
19 19 public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
20 20  
... ...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
... ... @@ -17,7 +17,6 @@ import java.util.List;
17 17 import java.util.Map;
18 18 import java.util.UUID;
19 19 import java.util.stream.Collectors;
20   -
21 20 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
22 21 import org.apache.poi.ss.usermodel.BorderStyle;
23 22 import org.apache.poi.ss.usermodel.Cell;
... ... @@ -41,7 +40,6 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
41 40 import org.apache.poi.xssf.usermodel.XSSFDataValidation;
42 41 import org.slf4j.Logger;
43 42 import org.slf4j.LoggerFactory;
44   -
45 43 import com.ruoyi.common.annotation.Excel;
46 44 import com.ruoyi.common.annotation.Excel.ColumnType;
47 45 import com.ruoyi.common.annotation.Excel.Type;
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
... ... @@ -148,18 +148,30 @@ public class GenController extends BaseController
148 148 }
149 149  
150 150 /**
151   - * 生成代码
  151 + * 生成代码(下载方式)
152 152 */
153 153 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
154 154 @Log(title = "代码生成", businessType = BusinessType.GENCODE)
155   - @GetMapping("/genCode/{tableName}")
156   - public void genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
  155 + @GetMapping("/download/{tableName}")
  156 + public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
157 157 {
158   - byte[] data = genTableService.generatorCode(tableName);
  158 + byte[] data = genTableService.downloadCode(tableName);
159 159 genCode(response, data);
160 160 }
161 161  
162 162 /**
  163 + * 生成代码(自定义路径)
  164 + */
  165 + @PreAuthorize("@ss.hasPermi('tool:gen:code')")
  166 + @Log(title = "代码生成", businessType = BusinessType.GENCODE)
  167 + @GetMapping("/genCode/{tableName}")
  168 + public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName)
  169 + {
  170 + genTableService.generatorCode(tableName);
  171 + return AjaxResult.success();
  172 + }
  173 +
  174 + /**
163 175 * 批量生成代码
164 176 */
165 177 @PreAuthorize("@ss.hasPermi('tool:gen:code')")
... ... @@ -168,7 +180,7 @@ public class GenController extends BaseController
168 180 public void batchGenCode(HttpServletResponse response, String tables) throws IOException
169 181 {
170 182 String[] tableNames = Convert.toStrArray(tables);
171   - byte[] data = genTableService.generatorCode(tableNames);
  183 + byte[] data = genTableService.downloadCode(tableNames);
172 184 genCode(response, data);
173 185 }
174 186  
... ... @@ -185,4 +197,4 @@ public class GenController extends BaseController
185 197 response.setContentType("application/octet-stream; charset=UTF-8");
186 198 IOUtils.write(data, response.getOutputStream());
187 199 }
188   -}
  200 +}
189 201 \ No newline at end of file
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java
... ... @@ -55,6 +55,12 @@ public class GenTable extends BaseEntity
55 55 @NotBlank(message = "作者不能为空")
56 56 private String functionAuthor;
57 57  
  58 + /** 生成代码方式(0zip压缩包 1自定义路径) */
  59 + private String genType;
  60 +
  61 + /** 生成路径(不填默认项目路径) */
  62 + private String genPath;
  63 +
58 64 /** 主键信息 */
59 65 private GenTableColumn pkColumn;
60 66  
... ... @@ -180,6 +186,26 @@ public class GenTable extends BaseEntity
180 186 this.functionAuthor = functionAuthor;
181 187 }
182 188  
  189 + public String getGenType()
  190 + {
  191 + return genType;
  192 + }
  193 +
  194 + public void setGenType(String genType)
  195 + {
  196 + this.genType = genType;
  197 + }
  198 +
  199 + public String getGenPath()
  200 + {
  201 + return genPath;
  202 + }
  203 +
  204 + public void setGenPath(String genPath)
  205 + {
  206 + this.genPath = genPath;
  207 + }
  208 +
183 209 public GenTableColumn getPkColumn()
184 210 {
185 211 return pkColumn;
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java
1 1 package com.ruoyi.generator.service;
2 2  
3 3 import java.io.ByteArrayOutputStream;
  4 +import java.io.File;
4 5 import java.io.IOException;
5 6 import java.io.StringWriter;
6 7 import java.util.LinkedHashMap;
... ... @@ -21,9 +22,11 @@ import com.alibaba.fastjson.JSON;
21 22 import com.alibaba.fastjson.JSONObject;
22 23 import com.ruoyi.common.constant.Constants;
23 24 import com.ruoyi.common.constant.GenConstants;
  25 +import com.ruoyi.common.core.text.CharsetKit;
24 26 import com.ruoyi.common.exception.CustomException;
25 27 import com.ruoyi.common.utils.SecurityUtils;
26 28 import com.ruoyi.common.utils.StringUtils;
  29 +import com.ruoyi.common.utils.file.FileUtils;
27 30 import com.ruoyi.generator.domain.GenTable;
28 31 import com.ruoyi.generator.domain.GenTableColumn;
29 32 import com.ruoyi.generator.mapper.GenTableColumnMapper;
... ... @@ -202,13 +205,13 @@ public class GenTableServiceImpl implements IGenTableService
202 205 }
203 206  
204 207 /**
205   - * 生成代码
  208 + * 生成代码(下载方式)
206 209 *
207 210 * @param tableName 表名称
208 211 * @return 数据
209 212 */
210 213 @Override
211   - public byte[] generatorCode(String tableName)
  214 + public byte[] downloadCode(String tableName)
212 215 {
213 216 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
214 217 ZipOutputStream zip = new ZipOutputStream(outputStream);
... ... @@ -218,13 +221,55 @@ public class GenTableServiceImpl implements IGenTableService
218 221 }
219 222  
220 223 /**
221   - * 批量生成代码
  224 + * 生成代码(自定义路径)
  225 + *
  226 + * @param tableName 表名称
  227 + * @return 数据
  228 + */
  229 + @Override
  230 + public void generatorCode(String tableName)
  231 + {
  232 + // 查询表信息
  233 + GenTable table = genTableMapper.selectGenTableByName(tableName);
  234 + // 查询列信息
  235 + List<GenTableColumn> columns = table.getColumns();
  236 + setPkColumn(table, columns);
  237 +
  238 + VelocityInitializer.initVelocity();
  239 +
  240 + VelocityContext context = VelocityUtils.prepareContext(table);
  241 +
  242 + // 获取模板列表
  243 + List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  244 + for (String template : templates)
  245 + {
  246 + if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
  247 + {
  248 + // 渲染模板
  249 + StringWriter sw = new StringWriter();
  250 + Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  251 + tpl.merge(context, sw);
  252 + try
  253 + {
  254 + String path = getGenPath(table, template);
  255 + FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
  256 + }
  257 + catch (IOException e)
  258 + {
  259 + throw new CustomException("渲染模板失败,表名:" + table.getTableName());
  260 + }
  261 + }
  262 + }
  263 + }
  264 +
  265 + /**
  266 + * 批量生成代码(下载方式)
222 267 *
223 268 * @param tableNames 表数组
224 269 * @return 数据
225 270 */
226 271 @Override
227   - public byte[] generatorCode(String[] tableNames)
  272 + public byte[] downloadCode(String[] tableNames)
228 273 {
229 274 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
230 275 ZipOutputStream zip = new ZipOutputStream(outputStream);
... ... @@ -347,4 +392,21 @@ public class GenTableServiceImpl implements IGenTableService
347 392 genTable.setParentMenuName(parentMenuName);
348 393 }
349 394 }
  395 +
  396 + /**
  397 + * 获取代码生成地址
  398 + *
  399 + * @param table 业务表信息
  400 + * @param template 模板文件路径
  401 + * @return 生成地址
  402 + */
  403 + public static String getGenPath(GenTable table, String template)
  404 + {
  405 + String genPath = table.getGenPath();
  406 + if (StringUtils.equals(genPath, "/"))
  407 + {
  408 + return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
  409 + }
  410 + return genPath + File.separator + VelocityUtils.getFileName(template, table);
  411 + }
350 412 }
351 413 \ No newline at end of file
... ...
ruoyi-generator/src/main/java/com/ruoyi/generator/service/IGenTableService.java
... ... @@ -75,20 +75,28 @@ public interface IGenTableService
75 75 public Map<String, String> previewCode(Long tableId);
76 76  
77 77 /**
78   - * 生成代码
  78 + * 生成代码(下载方式)
79 79 *
80 80 * @param tableName 表名称
81 81 * @return 数据
82 82 */
83   - public byte[] generatorCode(String tableName);
  83 + public byte[] downloadCode(String tableName);
84 84  
85 85 /**
86   - * 批量生成代码
  86 + * 生成代码(自定义路径)
  87 + *
  88 + * @param tableName 表名称
  89 + * @return 数据
  90 + */
  91 + public void generatorCode(String tableName);
  92 +
  93 + /**
  94 + * 批量生成代码(下载方式)
87 95 *
88 96 * @param tableNames 表数组
89 97 * @return 数据
90 98 */
91   - public byte[] generatorCode(String[] tableNames);
  99 + public byte[] downloadCode(String[] tableNames);
92 100  
93 101 /**
94 102 * 修改保存参数校验
... ...
ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml
... ... @@ -15,6 +15,8 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
15 15 <result property="businessName" column="business_name" />
16 16 <result property="functionName" column="function_name" />
17 17 <result property="functionAuthor" column="function_author" />
  18 + <result property="genType" column="gen_type" />
  19 + <result property="genPath" column="gen_path" />
18 20 <result property="options" column="options" />
19 21 <result property="createBy" column="create_by" />
20 22 <result property="createTime" column="create_time" />
... ... @@ -50,7 +52,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
50 52 </resultMap>
51 53  
52 54 <sql id="selectGenTableVo">
53   - select table_id, table_name, table_comment, class_name, tpl_category, package_name, module_name, business_name, function_name, function_author, options, create_by, create_time, update_by, update_time, remark from gen_table
  55 + select table_id, table_name, table_comment, class_name, tpl_category, package_name, module_name, business_name, function_name, function_author, gen_type, gen_path, options, create_by, create_time, update_by, update_time, remark from gen_table
54 56 </sql>
55 57  
56 58 <select id="selectGenTableList" parameterType="GenTable" resultMap="GenTableResult">
... ... @@ -106,7 +108,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
106 108 </select>
107 109  
108 110 <select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult">
109   - SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.options, t.remark,
  111 + SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,
110 112 c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort
111 113 FROM gen_table t
112 114 LEFT JOIN gen_table_column c ON t.table_id = c.table_id
... ... @@ -114,7 +116,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
114 116 </select>
115 117  
116 118 <select id="selectGenTableByName" parameterType="String" resultMap="GenTableResult">
117   - SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.options, t.remark,
  119 + SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,
118 120 c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort
119 121 FROM gen_table t
120 122 LEFT JOIN gen_table_column c ON t.table_id = c.table_id
... ... @@ -132,6 +134,8 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
132 134 <if test="businessName != null and businessName != ''">business_name,</if>
133 135 <if test="functionName != null and functionName != ''">function_name,</if>
134 136 <if test="functionAuthor != null and functionAuthor != ''">function_author,</if>
  137 + <if test="genType != null and genType != ''">gen_type,</if>
  138 + <if test="genPath != null and genPath != ''">gen_path,</if>
135 139 <if test="remark != null and remark != ''">remark,</if>
136 140 <if test="createBy != null and createBy != ''">create_by,</if>
137 141 create_time
... ... @@ -145,6 +149,8 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
145 149 <if test="businessName != null and businessName != ''">#{businessName},</if>
146 150 <if test="functionName != null and functionName != ''">#{functionName},</if>
147 151 <if test="functionAuthor != null and functionAuthor != ''">#{functionAuthor},</if>
  152 + <if test="genType != null and genType != ''">#{genType},</if>
  153 + <if test="genPath != null and genPath != ''">#{genPath},</if>
148 154 <if test="remark != null and remark != ''">#{remark},</if>
149 155 <if test="createBy != null and createBy != ''">#{createBy},</if>
150 156 sysdate()
... ... @@ -158,6 +164,8 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
158 164 <if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if>
159 165 <if test="className != null and className != ''">class_name = #{className},</if>
160 166 <if test="functionAuthor != null and functionAuthor != ''">function_author = #{functionAuthor},</if>
  167 + <if test="genType != null and genType != ''">gen_type = #{genType},</if>
  168 + <if test="genPath != null and genPath != ''">gen_path = #{genPath},</if>
161 169 <if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if>
162 170 <if test="packageName != null and packageName != ''">package_name = #{packageName},</if>
163 171 <if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if>
... ...
ruoyi-ui/src/api/tool/gen.js
... ... @@ -42,6 +42,7 @@ export function importTable(data) {
42 42 params: data
43 43 })
44 44 }
  45 +
45 46 // 预览生成代码
46 47 export function previewTable(tableId) {
47 48 return request({
... ... @@ -49,6 +50,7 @@ export function previewTable(tableId) {
49 50 method: 'get'
50 51 })
51 52 }
  53 +
52 54 // 删除表数据
53 55 export function delTable(tableId) {
54 56 return request({
... ... @@ -57,3 +59,11 @@ export function delTable(tableId) {
57 59 })
58 60 }
59 61  
  62 +// 生成代码(自定义路径)
  63 +export function genCode(tableName) {
  64 + return request({
  65 + url: '/tool/gen/genCode/' + tableName,
  66 + method: 'get'
  67 + })
  68 +}
  69 +
... ...
ruoyi-ui/src/views/tool/gen/genInfoForm.vue
... ... @@ -6,7 +6,7 @@
6 6 <span slot="label">生成模板</span>
7 7 <el-select v-model="info.tplCategory">
8 8 <el-option label="单表(增删改查)" value="crud" />
9   - <el-option label="树表(增删改查)" value="tree"/>
  9 + <el-option label="树表(增删改查)" value="tree" />
10 10 </el-select>
11 11 </el-form-item>
12 12 </el-col>
... ... @@ -60,14 +60,56 @@
60 60 </el-col>
61 61  
62 62 <el-col :span="12">
63   - <el-form-item prop="functionName">
  63 + <el-form-item>
64 64 <span slot="label">
65 65 上级菜单
66 66 <el-tooltip content="分配到指定菜单下,例如 系统管理" placement="top">
67 67 <i class="el-icon-question"></i>
68 68 </el-tooltip>
69 69 </span>
70   - <treeselect :append-to-body="true" v-model="info.parentMenuId" :options="menus" :normalizer="normalizer" :show-count="true" placeholder="请选择系统菜单"/>
  70 + <treeselect
  71 + :append-to-body="true"
  72 + v-model="info.parentMenuId"
  73 + :options="menus"
  74 + :normalizer="normalizer"
  75 + :show-count="true"
  76 + placeholder="请选择系统菜单"
  77 + />
  78 + </el-form-item>
  79 + </el-col>
  80 +
  81 + <el-col :span="12">
  82 + <el-form-item prop="genType">
  83 + <span slot="label">
  84 + 生成代码方式
  85 + <el-tooltip content="默认为zip压缩包下载,也可以自定义生成路径" placement="top">
  86 + <i class="el-icon-question"></i>
  87 + </el-tooltip>
  88 + </span>
  89 + <el-radio v-model="info.genType" label="0">zip压缩包</el-radio>
  90 + <el-radio v-model="info.genType" label="1">自定义路径</el-radio>
  91 + </el-form-item>
  92 + </el-col>
  93 +
  94 + <el-col :span="24" v-if="info.genType == '1'">
  95 + <el-form-item prop="genPath">
  96 + <span slot="label">
  97 + 自定义路径
  98 + <el-tooltip content="填写磁盘绝对路径,若不填写,则生成到当前Web项目下" placement="top">
  99 + <i class="el-icon-question"></i>
  100 + </el-tooltip>
  101 + </span>
  102 + <el-input v-model="info.genPath">
  103 + <el-dropdown slot="append">
  104 + <el-button type="primary">
  105 + 最近路径快速选择
  106 + <i class="el-icon-arrow-down el-icon--right"></i>
  107 + </el-button>
  108 + <el-dropdown-menu slot="dropdown">
  109 + <el-dropdown-item @click.native="info.genPath = '/'">恢复默认的生成基础路径</el-dropdown-item>
  110 + </el-dropdown-menu>
  111 + </el-dropdown>
  112 + </el-input>
71 113 </el-form-item>
72 114 </el-col>
73 115 </el-row>
... ... @@ -165,7 +207,7 @@ export default {
165 207 ],
166 208 functionName: [
167 209 { required: true, message: "请输入生成功能名", trigger: "blur" }
168   - ]
  210 + ],
169 211 }
170 212 };
171 213 },
... ...
ruoyi-ui/src/views/tool/gen/index.vue
... ... @@ -166,7 +166,7 @@
166 166 </template>
167 167  
168 168 <script>
169   -import { listTable, previewTable, delTable } from "@/api/tool/gen";
  169 +import { listTable, previewTable, delTable, genCode } from "@/api/tool/gen";
170 170 import importTable from "./importTable";
171 171 import { downLoadZip } from "@/utils/zipdownload";
172 172 export default {
... ... @@ -241,7 +241,13 @@ export default {
241 241 this.msgError("请选择要生成的数据");
242 242 return;
243 243 }
244   - downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
  244 + if(row.genType === "1") {
  245 + genCode(row.tableName).then(response => {
  246 + this.msgSuccess("成功生成到自定义路径:" + row.genPath);
  247 + });
  248 + } else {
  249 + downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
  250 + }
245 251 },
246 252 /** 打开导入表弹窗 */
247 253 openImportTable() {
... ...
sql/ry_20200723.sql renamed to sql/ry_20200724.sql
... ... @@ -635,6 +635,8 @@ create table gen_table (
635 635 business_name varchar(30) comment '生成业务名',
636 636 function_name varchar(50) comment '生成功能名',
637 637 function_author varchar(50) comment '生成功能作者',
  638 + gen_type char(1) default '0' comment '生成代码方式(0zip压缩包 1自定义路径)',
  639 + gen_path varchar(200) default '/' comment '生成路径(不填默认项目路径)',
638 640 options varchar(1000) comment '其它生成选项',
639 641 create_by varchar(64) default '' comment '创建者',
640 642 create_time datetime comment '创建时间',
... ...