From e818078392e2db431107af198b8a03ba23473e23 Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期五, 24 七月 2020 15:22:28 +0800 Subject: [PATCH] 代码生成支持自定义路径 --- sql/ry_20200724.sql | 2 src/main/java/com/ruoyi/common/utils/file/FileUtils.java | 2 src/main/java/com/ruoyi/project/tool/gen/controller/GenController.java | 22 +++++- src/main/java/com/ruoyi/project/tool/gen/service/IGenTableService.java | 16 ++++- src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java | 70 ++++++++++++++++++++++- src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java | 26 ++++++++ src/main/resources/mybatis/tool/GenTableMapper.xml | 14 +++- 7 files changed, 135 insertions(+), 17 deletions(-) diff --git a/sql/ry_20200723.sql b/sql/ry_20200724.sql similarity index 99% rename from sql/ry_20200723.sql rename to sql/ry_20200724.sql index 727ac6c..6b075b2 100644 --- a/sql/ry_20200723.sql +++ b/sql/ry_20200724.sql @@ -635,6 +635,8 @@ business_name varchar(30) comment '鐢熸垚涓氬姟鍚�', function_name varchar(50) comment '鐢熸垚鍔熻兘鍚�', function_author varchar(50) comment '鐢熸垚鍔熻兘浣滆��', + gen_type char(1) default '0' comment '鐢熸垚浠g爜鏂瑰紡锛�0zip鍘嬬缉鍖� 1鑷畾涔夎矾寰勶級', + gen_path varchar(200) default '/' comment '鐢熸垚璺緞锛堜笉濉粯璁ら」鐩矾寰勶級', options varchar(1000) comment '鍏跺畠鐢熸垚閫夐」', create_by varchar(64) default '' comment '鍒涘缓鑰�', create_time datetime comment '鍒涘缓鏃堕棿', diff --git a/src/main/java/com/ruoyi/common/utils/file/FileUtils.java b/src/main/java/com/ruoyi/common/utils/file/FileUtils.java index 03f5aa2..f0dee60 100644 --- a/src/main/java/com/ruoyi/common/utils/file/FileUtils.java +++ b/src/main/java/com/ruoyi/common/utils/file/FileUtils.java @@ -14,7 +14,7 @@ * * @author ruoyi */ -public class FileUtils +public class FileUtils extends org.apache.commons.io.FileUtils { public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; diff --git a/src/main/java/com/ruoyi/project/tool/gen/controller/GenController.java b/src/main/java/com/ruoyi/project/tool/gen/controller/GenController.java index 179d7ff..e57dc1f 100644 --- a/src/main/java/com/ruoyi/project/tool/gen/controller/GenController.java +++ b/src/main/java/com/ruoyi/project/tool/gen/controller/GenController.java @@ -148,15 +148,27 @@ } /** - * 鐢熸垚浠g爜 + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 + */ + @PreAuthorize("@ss.hasPermi('tool:gen:code')") + @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.GENCODE) + @GetMapping("/download/{tableName}") + public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException + { + byte[] data = genTableService.downloadCode(tableName); + genCode(response, data); + } + + /** + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� */ @PreAuthorize("@ss.hasPermi('tool:gen:code')") @Log(title = "浠g爜鐢熸垚", businessType = BusinessType.GENCODE) @GetMapping("/genCode/{tableName}") - public void genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException + public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) { - byte[] data = genTableService.generatorCode(tableName); - genCode(response, data); + genTableService.generatorCode(tableName); + return AjaxResult.success(); } /** @@ -168,7 +180,7 @@ public void batchGenCode(HttpServletResponse response, String tables) throws IOException { String[] tableNames = Convert.toStrArray(tables); - byte[] data = genTableService.generatorCode(tableNames); + byte[] data = genTableService.downloadCode(tableNames); genCode(response, data); } diff --git a/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java b/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java index 98fb5fe..c646735 100644 --- a/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java +++ b/src/main/java/com/ruoyi/project/tool/gen/domain/GenTable.java @@ -55,6 +55,12 @@ @NotBlank(message = "浣滆�呬笉鑳戒负绌�") private String functionAuthor; + /** 鐢熸垚浠g爜鏂瑰紡锛�0zip鍘嬬缉鍖� 1鑷畾涔夎矾寰勶級 */ + private String genType; + + /** 鐢熸垚璺緞锛堜笉濉粯璁ら」鐩矾寰勶級 */ + private String genPath; + /** 涓婚敭淇℃伅 */ private GenTableColumn pkColumn; @@ -180,6 +186,26 @@ this.functionAuthor = functionAuthor; } + public String getGenType() + { + return genType; + } + + public void setGenType(String genType) + { + this.genType = genType; + } + + public String getGenPath() + { + return genPath; + } + + public void setGenPath(String genPath) + { + this.genPath = genPath; + } + public GenTableColumn getPkColumn() { return pkColumn; diff --git a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java index 8dc18fb..4347993 100644 --- a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java +++ b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java @@ -1,6 +1,7 @@ package com.ruoyi.project.tool.gen.service; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.LinkedHashMap; @@ -21,9 +22,11 @@ import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.GenConstants; +import com.ruoyi.common.core.text.CharsetKit; import com.ruoyi.common.exception.CustomException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.project.tool.gen.domain.GenTable; import com.ruoyi.project.tool.gen.domain.GenTableColumn; import com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper; @@ -202,13 +205,13 @@ } /** - * 鐢熸垚浠g爜 + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * * @param tableName 琛ㄥ悕绉� * @return 鏁版嵁 */ @Override - public byte[] generatorCode(String tableName) + public byte[] downloadCode(String tableName) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); @@ -218,13 +221,55 @@ } /** - * 鎵归噺鐢熸垚浠g爜 + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� + * + * @param tableName 琛ㄥ悕绉� + * @return 鏁版嵁 + */ + @Override + public void generatorCode(String tableName) + { + // 鏌ヨ琛ㄤ俊鎭� + GenTable table = genTableMapper.selectGenTableByName(tableName); + // 鏌ヨ鍒椾俊鎭� + List<GenTableColumn> columns = table.getColumns(); + setPkColumn(table, columns); + + VelocityInitializer.initVelocity(); + + VelocityContext context = VelocityUtils.prepareContext(table); + + // 鑾峰彇妯℃澘鍒楄〃 + List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) + { + if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) + { + // 娓叉煋妯℃澘 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, Constants.UTF8); + tpl.merge(context, sw); + try + { + String path = getGenPath(table, template); + FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8); + } + catch (IOException e) + { + throw new CustomException("娓叉煋妯℃澘澶辫触锛岃〃鍚嶏細" + table.getTableName()); + } + } + } + } + + /** + * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * * @param tableNames 琛ㄦ暟缁� * @return 鏁版嵁 */ @Override - public byte[] generatorCode(String[] tableNames) + public byte[] downloadCode(String[] tableNames) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); @@ -347,4 +392,21 @@ genTable.setParentMenuName(parentMenuName); } } + + /** + * 鑾峰彇浠g爜鐢熸垚鍦板潃 + * + * @param table 涓氬姟琛ㄤ俊鎭� + * @param template 妯℃澘鏂囦欢璺緞 + * @return 鐢熸垚鍦板潃 + */ + public static String getGenPath(GenTable table, String template) + { + String genPath = table.getGenPath(); + if (StringUtils.equals(genPath, "/")) + { + return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table); + } + return genPath + File.separator + VelocityUtils.getFileName(template, table); + } } \ No newline at end of file diff --git a/src/main/java/com/ruoyi/project/tool/gen/service/IGenTableService.java b/src/main/java/com/ruoyi/project/tool/gen/service/IGenTableService.java index 73093fe..88dfa37 100644 --- a/src/main/java/com/ruoyi/project/tool/gen/service/IGenTableService.java +++ b/src/main/java/com/ruoyi/project/tool/gen/service/IGenTableService.java @@ -75,20 +75,28 @@ public Map<String, String> previewCode(Long tableId); /** - * 鐢熸垚浠g爜 + * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * * @param tableName 琛ㄥ悕绉� * @return 鏁版嵁 */ - public byte[] generatorCode(String tableName); + public byte[] downloadCode(String tableName); /** - * 鎵归噺鐢熸垚浠g爜 + * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛� + * + * @param tableName 琛ㄥ悕绉� + * @return 鏁版嵁 + */ + public void generatorCode(String tableName); + + /** + * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級 * * @param tableNames 琛ㄦ暟缁� * @return 鏁版嵁 */ - public byte[] generatorCode(String[] tableNames); + public byte[] downloadCode(String[] tableNames); /** * 淇敼淇濆瓨鍙傛暟鏍¢獙 diff --git a/src/main/resources/mybatis/tool/GenTableMapper.xml b/src/main/resources/mybatis/tool/GenTableMapper.xml index 64a1f5e..05ccfb7 100644 --- a/src/main/resources/mybatis/tool/GenTableMapper.xml +++ b/src/main/resources/mybatis/tool/GenTableMapper.xml @@ -15,6 +15,8 @@ <result property="businessName" column="business_name" /> <result property="functionName" column="function_name" /> <result property="functionAuthor" column="function_author" /> + <result property="genType" column="gen_type" /> + <result property="genPath" column="gen_path" /> <result property="options" column="options" /> <result property="createBy" column="create_by" /> <result property="createTime" column="create_time" /> @@ -50,7 +52,7 @@ </resultMap> <sql id="selectGenTableVo"> - 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 + 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 </sql> <select id="selectGenTableList" parameterType="GenTable" resultMap="GenTableResult"> @@ -106,7 +108,7 @@ </select> <select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult"> - 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, + 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, 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 FROM gen_table t LEFT JOIN gen_table_column c ON t.table_id = c.table_id @@ -114,7 +116,7 @@ </select> <select id="selectGenTableByName" parameterType="String" resultMap="GenTableResult"> - 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, + 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, 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 FROM gen_table t LEFT JOIN gen_table_column c ON t.table_id = c.table_id @@ -132,6 +134,8 @@ <if test="businessName != null and businessName != ''">business_name,</if> <if test="functionName != null and functionName != ''">function_name,</if> <if test="functionAuthor != null and functionAuthor != ''">function_author,</if> + <if test="genType != null and genType != ''">gen_type,</if> + <if test="genPath != null and genPath != ''">gen_path,</if> <if test="remark != null and remark != ''">remark,</if> <if test="createBy != null and createBy != ''">create_by,</if> create_time @@ -145,6 +149,8 @@ <if test="businessName != null and businessName != ''">#{businessName},</if> <if test="functionName != null and functionName != ''">#{functionName},</if> <if test="functionAuthor != null and functionAuthor != ''">#{functionAuthor},</if> + <if test="genType != null and genType != ''">#{genType},</if> + <if test="genPath != null and genPath != ''">#{genPath},</if> <if test="remark != null and remark != ''">#{remark},</if> <if test="createBy != null and createBy != ''">#{createBy},</if> sysdate() @@ -158,6 +164,8 @@ <if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if> <if test="className != null and className != ''">class_name = #{className},</if> <if test="functionAuthor != null and functionAuthor != ''">function_author = #{functionAuthor},</if> + <if test="genType != null and genType != ''">gen_type = #{genType},</if> + <if test="genPath != null and genPath != ''">gen_path = #{genPath},</if> <if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if> <if test="packageName != null and packageName != ''">package_name = #{packageName},</if> <if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if> -- Gitblit v1.9.3