代码生成-代码下载
This commit is contained in:
parent
5ec1e82d8b
commit
253441942f
|
|
@ -17,7 +17,8 @@ public class AdminConfig {
|
|||
// 免登录验证
|
||||
public static String[] notLoginUri = new String[]{
|
||||
"system:login", // 登录接口
|
||||
"index:config" // 配置接口
|
||||
"index:config", // 配置接口
|
||||
"gen:genCode"
|
||||
};
|
||||
|
||||
// 免权限验证
|
||||
|
|
|
|||
|
|
@ -108,6 +108,11 @@
|
|||
<groupId>nl.bitwalker</groupId>
|
||||
<artifactId>UserAgentUtils</artifactId>
|
||||
</dependency>
|
||||
<!-- io常用工具类 -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<!-- 七牛云 -->
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
|
|
|
|||
|
|
@ -474,4 +474,65 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文本, {} 表示占位符<br>
|
||||
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
||||
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
|
||||
* 例:<br>
|
||||
* 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
|
||||
* 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
|
||||
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
|
||||
*
|
||||
* @param strPattern 文本模板,被替换的部分用 {} 表示
|
||||
* @param argArray 参数值
|
||||
* @return 格式化后的文本
|
||||
*/
|
||||
public static String format(String strPattern, Object... argArray) {
|
||||
String EMPTY_JSON = "{}";
|
||||
char C_BACKSLASH = '\\';
|
||||
char C_DELIM_START = '{';
|
||||
|
||||
if (isEmpty(argArray) || isEmpty(strPattern)) {
|
||||
return strPattern;
|
||||
}
|
||||
|
||||
final int strPatternLength = strPattern.length();
|
||||
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
|
||||
int handledPosition = 0;
|
||||
int delimIndex;
|
||||
for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
|
||||
delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);
|
||||
if (delimIndex == -1) {
|
||||
if (handledPosition == 0) {
|
||||
return strPattern;
|
||||
} else {
|
||||
sbuf.append(strPattern, handledPosition, strPatternLength);
|
||||
return sbuf.toString();
|
||||
}
|
||||
} else {
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) {
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) {
|
||||
sbuf.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbuf.append(argArray[argIndex]);
|
||||
handledPosition = delimIndex + 2;
|
||||
} else {
|
||||
// 占位符被转义
|
||||
argIndex--;
|
||||
sbuf.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbuf.append(C_DELIM_START);
|
||||
handledPosition = delimIndex + 1;
|
||||
}
|
||||
} else {
|
||||
// 正常占位符
|
||||
sbuf.append(strPattern, handledPosition, delimIndex);
|
||||
sbuf.append(argArray[argIndex]);
|
||||
handledPosition = delimIndex + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sbuf.append(strPattern, handledPosition, strPattern.length());
|
||||
return sbuf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,16 @@ import com.hxkj.generator.validate.GenParam;
|
|||
import com.hxkj.generator.validate.PageParam;
|
||||
import com.hxkj.generator.vo.DbTableVo;
|
||||
import com.hxkj.generator.vo.GenTableVo;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/gen")
|
||||
|
|
@ -130,8 +135,19 @@ public class GenController {
|
|||
* @author fzr
|
||||
* @return Object
|
||||
*/
|
||||
@GetMapping("genCode")
|
||||
public Object genCode(@Validated @IDMust() @RequestParam("id") Integer id) {
|
||||
@GetMapping("/genCode")
|
||||
public Object genCode(HttpServletResponse response, String tables) throws IOException {
|
||||
String[] tableNames = tables.split(",");
|
||||
|
||||
byte[] data = iGenerateService.downloadCode(tableNames);
|
||||
|
||||
response.reset();
|
||||
response.addHeader("Access-Control-Allow-Origin", "*");
|
||||
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
|
||||
response.addHeader("Content-Length", "" + data.length);
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IOUtils.write(data, response.getOutputStream());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -73,16 +73,4 @@ public interface GenTableMapper extends IBaseMapper<GenTable> {
|
|||
"</script>"})
|
||||
List<GenTableColumn> selectDbTableColumnsByName(String tableName);
|
||||
|
||||
@Select({"<script>",
|
||||
"SELECT " +
|
||||
"t.id, t.author_name, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk, t.entity_name, " +
|
||||
"t.module_name, t.package_name, t.business_name, t.function_name, t.gen_tpl, t.gen_type, t.gen_path, t.remarks, " +
|
||||
"c.id as 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.html_type, c.dict_type, c.sort",
|
||||
"FROM ls_gen_table t",
|
||||
"LEFT JOIN ls_gen_table_column c ON t.id = c.table_id",
|
||||
"WHERE t.id = #{id} ORDER BY c.sort",
|
||||
"</script>"})
|
||||
Map<String, String> selectGenTableByName(Integer id);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ public interface IGenerateService {
|
|||
* 生成列表
|
||||
*
|
||||
* @author fzr
|
||||
* @param pageParam 分页参数
|
||||
* @param params 搜索参数
|
||||
* @return Object
|
||||
*/
|
||||
PageResult<GenTableVo> list(PageParam pageParam, Map<String, String> params);
|
||||
|
|
@ -35,6 +37,7 @@ public interface IGenerateService {
|
|||
* 生成详情
|
||||
*
|
||||
* @author fzr
|
||||
* @param id 主键
|
||||
* @return Object
|
||||
*/
|
||||
Map<String, Object> detail(Integer id);
|
||||
|
|
@ -50,6 +53,7 @@ public interface IGenerateService {
|
|||
/**
|
||||
* 编辑表结构
|
||||
*
|
||||
* @param genParam 参数
|
||||
* @author fzr
|
||||
*/
|
||||
void editTable(GenParam genParam);
|
||||
|
|
@ -57,6 +61,7 @@ public interface IGenerateService {
|
|||
/**
|
||||
* 删除表结构
|
||||
*
|
||||
* @param id 主键
|
||||
* @author fzr
|
||||
*/
|
||||
void deleteTable(Integer id);
|
||||
|
|
@ -64,6 +69,7 @@ public interface IGenerateService {
|
|||
/**
|
||||
* 同步数据表
|
||||
*
|
||||
* @param id 主键
|
||||
* @author fzr
|
||||
*/
|
||||
void syncTable(Integer id);
|
||||
|
|
@ -77,11 +83,12 @@ public interface IGenerateService {
|
|||
Map<String, String> previewCode(Integer id);
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
* 下载代码
|
||||
*
|
||||
* @author fzr
|
||||
* @param tableNames 表名集合
|
||||
* @return Object
|
||||
*/
|
||||
Object genCode(Integer id);
|
||||
byte[] downloadCode(String[] tableNames);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,18 +22,25 @@ import com.hxkj.generator.validate.PageParam;
|
|||
import com.hxkj.generator.vo.DbTableVo;
|
||||
import com.hxkj.generator.vo.GenColumnVo;
|
||||
import com.hxkj.generator.vo.GenTableVo;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 代码生成器服务实现类
|
||||
|
|
@ -41,6 +48,8 @@ import java.util.stream.Collectors;
|
|||
@Service
|
||||
public class GenerateServiceImpl implements IGenerateService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GenerateServiceImpl.class);
|
||||
|
||||
@Resource
|
||||
GenTableMapper genTableMapper;
|
||||
|
||||
|
|
@ -348,15 +357,54 @@ public class GenerateServiceImpl implements IGenerateService {
|
|||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] downloadCode(String[] tableNames) {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
for (String tableName : tableNames) {
|
||||
genZipCode(tableName, zip);
|
||||
}
|
||||
IOUtils.closeQuietly(zip);
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @author fzr
|
||||
* @return Object
|
||||
* @param tableName 表名
|
||||
* @param zip 压缩包
|
||||
*/
|
||||
@Override
|
||||
public Object genCode(Integer id) {
|
||||
return null;
|
||||
public void genZipCode(String tableName, ZipOutputStream zip) {
|
||||
// 查表信息
|
||||
GenTable table = genTableMapper.selectOne(new QueryWrapper<GenTable>()
|
||||
.eq("table_name", tableName)
|
||||
.last("limit 1"));
|
||||
|
||||
// 查列信息
|
||||
List<GenTableColumn> columns = genTableColumnMapper.selectList(
|
||||
new QueryWrapper<GenTableColumn>()
|
||||
.orderByAsc("sort")
|
||||
.eq("table_id", table.getId()));
|
||||
|
||||
// 初始模板
|
||||
VelocityUtil.initVelocity();
|
||||
VelocityContext context = VelocityUtil.prepareContext(table, columns);
|
||||
|
||||
// 渲染模板
|
||||
List<String> templates = VelocityUtil.getTemplateList(table.getGenTpl());
|
||||
for (String template : templates) {
|
||||
StringWriter sw = new StringWriter();
|
||||
Template tpl = Velocity.getTemplate(template, "UTF-8");
|
||||
tpl.merge(context, sw);
|
||||
try {
|
||||
zip.putNextEntry(new ZipEntry(VelocityUtil.getFileName(template, table)));
|
||||
IOUtils.write(sw.toString(), zip, "UTF-8");
|
||||
zip.flush();
|
||||
zip.closeEntry();
|
||||
} catch (IOException e) {
|
||||
log.error("生成渲染模板失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,13 +59,54 @@ public class VelocityUtil {
|
|||
*/
|
||||
public static List<String> getTemplateList(String genTpl) {
|
||||
List<String> templates = new LinkedList<>();
|
||||
// templates.add("java/controller.java.vm");
|
||||
// templates.add("java/entity.java.vm");
|
||||
// templates.add("java/mapper.java.vm");
|
||||
// templates.add("java/service.java.vm");
|
||||
templates.add("java/controller.java.vm");
|
||||
templates.add("java/entity.java.vm");
|
||||
templates.add("java/mapper.java.vm");
|
||||
templates.add("java/service.java.vm");
|
||||
templates.add("java/serviceImpl.java.vm");
|
||||
// templates.add("java/validate.java.vm");
|
||||
templates.add("java/validate.java.vm");
|
||||
return templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件名
|
||||
*/
|
||||
public static String getFileName(String template, GenTable genTable)
|
||||
{
|
||||
// 文件名称
|
||||
String fileName = "";
|
||||
String entityName = genTable.getEntityName();
|
||||
String moduleName = genTable.getModuleName();
|
||||
String packageName = genTable.getPackageName();
|
||||
String businessName = genTable.getBusinessName();
|
||||
|
||||
String javaPath = StringUtil.replace(packageName, ".", "/");
|
||||
|
||||
if (template.contains("mapper.java.vm")) {
|
||||
fileName = StringUtil.format("{}/mapper/{}Mapper.java", "com/hxkj/common", entityName);
|
||||
}
|
||||
|
||||
else if (template.contains("entity.java.vm")) {
|
||||
fileName = StringUtil.format("{}/entity/{}Entity.java", "com/hxkj/common", entityName);
|
||||
}
|
||||
|
||||
else if (template.contains("service.java.vm")) {
|
||||
fileName = StringUtil.format("{}/service/I{}Service.java", javaPath, entityName);
|
||||
}
|
||||
|
||||
else if (template.contains("serviceImpl.java.vm")) {
|
||||
fileName = StringUtil.format("{}/service/impl/{}ServiceImpl.java", javaPath, entityName);
|
||||
}
|
||||
|
||||
else if (template.contains("controller.java.vm")) {
|
||||
fileName = StringUtil.format("{}/controller/{}Controller.java", javaPath, entityName);
|
||||
}
|
||||
|
||||
else if (template.contains("validate.java.vm")) {
|
||||
fileName = StringUtil.format("{}/validate/{}Param.java", javaPath, entityName);
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
<gson.version>2.9.0</gson.version>
|
||||
<bitwalker.version>1.2.4</bitwalker.version>
|
||||
<commons.io.version>2.11.0</commons.io.version>
|
||||
<qiniu.version>7.9.5</qiniu.version>
|
||||
<qcloud-version>5.6.54</qcloud-version>
|
||||
<tencentcloudapi.version>3.1.411</tencentcloudapi.version>
|
||||
|
|
@ -114,6 +115,12 @@
|
|||
<artifactId>UserAgentUtils</artifactId>
|
||||
<version>${bitwalker.version}</version>
|
||||
</dependency>
|
||||
<!-- io常用工具类 -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons.io.version}</version>
|
||||
</dependency>
|
||||
<!-- 七牛云 -->
|
||||
<dependency>
|
||||
<groupId>com.qiniu</groupId>
|
||||
|
|
|
|||
Loading…
Reference in New Issue