提交 cf25647a 编写于 作者: D dqjdda

更新代码生成预览的sql文件

上级 0cbc6e55
...@@ -75,6 +75,8 @@ public class GeneratorController { ...@@ -75,6 +75,8 @@ public class GeneratorController {
// 生成代码 // 生成代码
case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName)); case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
break; break;
// 预览
case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
default: break; default: break;
} }
return new ResponseEntity(HttpStatus.OK); return new ResponseEntity(HttpStatus.OK);
......
...@@ -2,6 +2,7 @@ package me.zhengjie.service; ...@@ -2,6 +2,7 @@ package me.zhengjie.service;
import me.zhengjie.domain.GenConfig; import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import java.util.List; import java.util.List;
...@@ -50,7 +51,14 @@ public interface GeneratorService { ...@@ -50,7 +51,14 @@ public interface GeneratorService {
* 代码生成 * 代码生成
* @param genConfig 配置信息 * @param genConfig 配置信息
* @param columns 字段信息 * @param columns 字段信息
*/
void generator(GenConfig genConfig, List<ColumnInfo> columns);
/**
* 预览
* @param genConfig 配置信息
* @param columns 字段信息
* @return / * @return /
*/ */
Object generator(GenConfig genConfig, List<ColumnInfo> columns); ResponseEntity preview(GenConfig genConfig, List<ColumnInfo> columns);
} }
...@@ -11,6 +11,8 @@ import me.zhengjie.service.GeneratorService; ...@@ -11,6 +11,8 @@ import me.zhengjie.service.GeneratorService;
import me.zhengjie.utils.GenUtil; import me.zhengjie.utils.GenUtil;
import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.StringUtils; import me.zhengjie.utils.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
...@@ -18,6 +20,7 @@ import javax.persistence.Query; ...@@ -18,6 +20,7 @@ import javax.persistence.Query;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author Zheng Jie * @author Zheng Jie
...@@ -113,17 +116,24 @@ public class GeneratorServiceImpl implements GeneratorService { ...@@ -113,17 +116,24 @@ public class GeneratorServiceImpl implements GeneratorService {
} }
@Override @Override
public Object generator(GenConfig genConfig, List<ColumnInfo> columns) { public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
if(genConfig.getId() == null){ if(genConfig.getId() == null){
throw new BadRequestException("请先配置生成器"); throw new BadRequestException("请先配置生成器");
} }
try { try {
// 查询是否存在关联实体字段信息
GenUtil.generatorCode(columns, genConfig); GenUtil.generatorCode(columns, genConfig);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new BadRequestException("生成失败,请手动处理已生成的文件"); throw new BadRequestException("生成失败,请手动处理已生成的文件");
} }
return null; }
@Override
public ResponseEntity preview(GenConfig genConfig, List<ColumnInfo> columns) {
if(genConfig.getId() == null){
throw new BadRequestException("请先配置生成器");
}
List<Map<String,Object>> genList = GenUtil.preview(columns, genConfig);
return new ResponseEntity<>(genList, HttpStatus.OK);
} }
} }
...@@ -11,10 +11,7 @@ import java.io.FileWriter; ...@@ -11,10 +11,7 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 代码生成 * 代码生成
...@@ -61,7 +58,72 @@ public class GenUtil { ...@@ -61,7 +58,72 @@ public class GenUtil {
return templateNames; return templateNames;
} }
public static List<Map<String, Object>> preview(List<ColumnInfo> columns, GenConfig genConfig) {
Map<String,Object> genMap = getGenMap(columns, genConfig);
List<Map<String,Object>> genList = new ArrayList<>();
// 获取后端模版
List<String> templates = getAdminTemplateNames();
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
for (String templateName : templates) {
Map<String,Object> map = new HashMap<>(1);
Template template = engine.getTemplate("generator/admin/"+templateName+".ftl");
map.put("content", template.render(genMap));
map.put("name", templateName);
genList.add(map);
}
// 获取前端模版
templates = getFrontTemplateNames();
for (String templateName : templates) {
Map<String,Object> map = new HashMap<>(1);
Template template = engine.getTemplate("generator/front/"+templateName+".ftl");
map.put(templateName, template.render(genMap));
map.put("content", template.render(genMap));
map.put("name", templateName);
genList.add(map);
}
return genList;
}
public static void generatorCode(List<ColumnInfo> columnInfos, GenConfig genConfig) throws IOException { public static void generatorCode(List<ColumnInfo> columnInfos, GenConfig genConfig) throws IOException {
Map<String,Object> genMap = getGenMap(columnInfos, genConfig);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
// 生成后端代码
List<String> templates = getAdminTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/admin/"+templateName+".ftl");
String filePath = getAdminFilePath(templateName,genConfig,genMap.get("className").toString());
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
// 生成前端代码
templates = getFrontTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/front/"+templateName+".ftl");
String filePath = getFrontFilePath(templateName,genConfig,genMap.get("changeClassName").toString());
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
}
// 获取模版数据
private static Map<String,Object> getGenMap(List<ColumnInfo> columnInfos, GenConfig genConfig) {
// 存储模版字段数据 // 存储模版字段数据
Map<String,Object> genMap = new HashMap<>(16); Map<String,Object> genMap = new HashMap<>(16);
// 接口别名 // 接口别名
...@@ -165,7 +227,7 @@ public class GenUtil { ...@@ -165,7 +227,7 @@ public class GenUtil {
// 表单显示 // 表单显示
listMap.put("formShow",column.getFormShow()); listMap.put("formShow",column.getFormShow());
// 表单组件类型 // 表单组件类型
listMap.put("formType",column.getFormType()); listMap.put("formType", StringUtils.isNotBlank(column.getFormType()) ? column.getFormType() : "Input");
// 小写开头的字段名称 // 小写开头的字段名称
listMap.put("changeColumnName",changeColumnName); listMap.put("changeColumnName",changeColumnName);
//大写开头的字段名称 //大写开头的字段名称
...@@ -215,40 +277,7 @@ public class GenUtil { ...@@ -215,40 +277,7 @@ public class GenUtil {
genMap.put("betweens",betweens); genMap.put("betweens",betweens);
// 保存非空字段信息 // 保存非空字段信息
genMap.put("isNotNullColumns",isNotNullColumns); genMap.put("isNotNullColumns",isNotNullColumns);
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH)); return genMap;
// 生成后端代码
List<String> templates = getAdminTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/admin/"+templateName+".ftl");
String filePath = getAdminFilePath(templateName,genConfig,className);
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
// 生成前端代码
templates = getFrontTemplateNames();
for (String templateName : templates) {
Template template = engine.getTemplate("generator/front/"+templateName+".ftl");
String filePath = getFrontFilePath(templateName,genConfig,genMap.get("changeClassName").toString());
assert filePath != null;
File file = new File(filePath);
// 如果非覆盖生成
if(!genConfig.getCover() && FileUtil.exist(file)){
continue;
}
// 生成代码
genFile(file, template, genMap);
}
} }
/** /**
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
-- ---------------------------- -- ----------------------------
INSERT INTO `menu` VALUES (82, b'0', '生成配置', 'generator/config', 36, 33, 'dev', 'generator/config/:tableName', b'1', b'1', 'GeneratorConfig', '2019-11-17 20:08:56', '', 1); INSERT INTO `menu` VALUES (82, b'0', '生成配置', 'generator/config', 36, 33, 'dev', 'generator/config/:tableName', b'1', b'1', 'GeneratorConfig', '2019-11-17 20:08:56', '', 1);
INSERT INTO `menu` VALUES (116, b'0', '生成预览', 'generator/preview', 36, 999, 'java', 'generator/preview/:tableName', b'1', b'1', 'Preview', '2019-11-26 14:54:36', NULL, 1);
-- ---------------------------- -- ----------------------------
-- Table structure for column_config -- Table structure for column_config
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册