代码生成支持自定义路径
This commit is contained in:
@ -188,15 +188,28 @@ public class GenController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
* 生成代码(下载方式)
|
||||
*/
|
||||
@RequiresPermissions("tool:gen:code")
|
||||
@Log(title = "代码生成", 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码(自定义路径)
|
||||
*/
|
||||
@RequiresPermissions("tool:gen:code")
|
||||
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
|
||||
@GetMapping("/genCode/{tableName}")
|
||||
public void genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
|
||||
@ResponseBody
|
||||
public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName)
|
||||
{
|
||||
byte[] data = genTableService.generatorCode(tableName);
|
||||
genCode(response, data);
|
||||
genTableService.generatorCode(tableName);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -209,7 +222,7 @@ public class GenController extends BaseController
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -61,6 +61,12 @@ public class GenTable extends BaseEntity
|
||||
@NotBlank(message = "作者不能为空")
|
||||
private String functionAuthor;
|
||||
|
||||
/** 生成代码方式(0zip压缩包 1自定义路径) */
|
||||
private String genType;
|
||||
|
||||
/** 生成路径(不填默认项目路径) */
|
||||
private String genPath;
|
||||
|
||||
/** 主键信息 */
|
||||
private GenTableColumn pkColumn;
|
||||
|
||||
@ -209,6 +215,26 @@ public class GenTable extends BaseEntity
|
||||
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;
|
||||
|
@ -83,20 +83,28 @@ public interface IGenTableService
|
||||
public Map<String, String> previewCode(Long tableId);
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
* 生成代码(下载方式)
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
public byte[] generatorCode(String tableName);
|
||||
public byte[] downloadCode(String tableName);
|
||||
|
||||
/**
|
||||
* 批量生成代码
|
||||
* 生成代码(自定义路径)
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
public void generatorCode(String tableName);
|
||||
|
||||
/**
|
||||
* 批量生成代码(下载方式)
|
||||
*
|
||||
* @param tableNames 表数组
|
||||
* @return 数据
|
||||
*/
|
||||
public byte[] generatorCode(String[] tableNames);
|
||||
public byte[] downloadCode(String[] tableNames);
|
||||
|
||||
/**
|
||||
* 修改保存参数校验
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.ruoyi.generator.service.impl;
|
||||
|
||||
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.JSON;
|
||||
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.core.text.Convert;
|
||||
import com.ruoyi.common.exception.BusinessException;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.generator.domain.GenTable;
|
||||
import com.ruoyi.generator.domain.GenTableColumn;
|
||||
import com.ruoyi.generator.mapper.GenTableColumnMapper;
|
||||
@ -215,13 +218,13 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
* 生成代码(下载方式)
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] generatorCode(String tableName)
|
||||
public byte[] downloadCode(String tableName)
|
||||
{
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
@ -229,6 +232,49 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
IOUtils.closeQuietly(zip);
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码(自定义路径)
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
@Override
|
||||
public void generatorCode(String tableName)
|
||||
{
|
||||
// 查询表信息
|
||||
GenTable table = genTableMapper.selectGenTableByName(tableName);
|
||||
// 设置主子表信息
|
||||
setSubTable(table);
|
||||
// 设置主键列信息
|
||||
setPkColumn(table);
|
||||
|
||||
VelocityInitializer.initVelocity();
|
||||
|
||||
VelocityContext context = VelocityUtils.prepareContext(table);
|
||||
|
||||
// 获取模板列表
|
||||
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
|
||||
for (String template : templates)
|
||||
{
|
||||
if (!StringUtils.contains(template, "sql.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 BusinessException("渲染模板失败,表名:" + table.getTableName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量生成代码
|
||||
@ -237,7 +283,7 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
* @return 数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] generatorCode(String[] tableNames)
|
||||
public byte[] downloadCode(String[] tableNames)
|
||||
{
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
@ -334,7 +380,6 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
*/
|
||||
public void setPkColumn(GenTable table)
|
||||
{
|
||||
|
||||
for (GenTableColumn column : table.getColumns())
|
||||
{
|
||||
if (column.isPk())
|
||||
@ -401,4 +446,21 @@ public class GenTableServiceImpl implements IGenTableService
|
||||
genTable.setParentMenuName(parentMenuName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代码生成地址
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user