use of com.nb6868.onex.coder.entity.ColumnEntity in project onex-boot by zhangchaoxu.
the class GenUtils method generatorCode.
/**
* 生成代码
*
* @param table 生成的表
* @param columns 生成的字段
* @param codeGenerateConfig 生成配置
* @param zip 输出的压缩包
*/
public static void generatorCode(Map<String, Object> table, List<Entity> columns, CodeGenerateConfig codeGenerateConfig, ZipOutputStream zip) {
// 配置信息
Configuration config = getConfig();
boolean hasBigDecimal = false;
// 表信息
TableEntity tableEntity = new TableEntity();
tableEntity.setTableName(MapUtil.getStr(table, "table_name"));
tableEntity.setComments(MapUtil.getStr(table, "table_comment"));
// 表名转换成Java类名
String className = tableToJava(tableEntity.getTableName(), codeGenerateConfig.getTablePrefix());
tableEntity.setClassName(className);
tableEntity.setClassname(StringUtils.uncapitalize(className));
// 列信息
List<ColumnEntity> columnsList = new ArrayList<>();
for (Entity column : columns) {
ColumnEntity columnEntity = new ColumnEntity();
columnEntity.setColumnName(MapUtil.getStr(column, "column_name"));
columnEntity.setDataType(MapUtil.getStr(column, "data_type"));
columnEntity.setComments(MapUtil.getStr(column, "column_comment"));
columnEntity.setExtra(MapUtil.getStr(column, "extra"));
// 列名转换成Java属性名
String attrName = columnToJava(columnEntity.getColumnName());
columnEntity.setAttrName(attrName);
columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
// 列的数据类型,转换成Java类型
String attrType = config.getString(columnEntity.getDataType(), "unknowType");
columnEntity.setAttrType(attrType);
if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
hasBigDecimal = true;
}
// 是否主键
if ("PRI".equalsIgnoreCase(MapUtil.getStr(column, "column_key")) && tableEntity.getPk() == null) {
tableEntity.setPk(columnEntity);
}
columnsList.add(columnEntity);
}
tableEntity.setColumns(columnsList);
// 没主键,则第一个字段为主键
if (tableEntity.getPk() == null) {
tableEntity.setPk(tableEntity.getColumns().get(0));
}
// 设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
// 封装模板数据
Map<String, Object> map = new HashMap<>();
map.put("tableName", tableEntity.getTableName());
map.put("comments", tableEntity.getComments());
map.put("pk", tableEntity.getPk());
map.put("className", tableEntity.getClassName());
map.put("classname", tableEntity.getClassname());
map.put("pathName", tableEntity.getClassname());
if (StringUtils.isNotBlank(codeGenerateConfig.getTablePrefix())) {
map.put("pathNameDash", tableEntity.getTableName().replaceFirst(codeGenerateConfig.getTablePrefix() + "_", "").replace("_", "-"));
} else {
map.put("pathNameDash", tableEntity.getTableName().replace("_", "-"));
}
map.put("columns", tableEntity.getColumns());
map.put("hasBigDecimal", hasBigDecimal);
map.put("version", codeGenerateConfig.getVersion());
map.put("package", codeGenerateConfig.getPackageName());
map.put("moduleName", codeGenerateConfig.getModuleName());
map.put("author", codeGenerateConfig.getAuthorName());
map.put("email", codeGenerateConfig.getAuthorEmail());
map.put("datetime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
map.put("date", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
for (int i = 0; i <= 10; i++) {
map.put("id" + i, IdUtil.getSnowflake(1, 1).nextId());
}
VelocityContext context = new VelocityContext(map);
// 获取模板列表
List<String> templates = getTemplates();
for (String template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);
try {
// 添加到zip
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), codeGenerateConfig.getPackageName(), codeGenerateConfig.getModuleName(), (String) map.get("pathNameDash"))));
IoUtil.write(zip, false, sw.toString().getBytes());
sw.close();
zip.closeEntry();
} catch (IOException e) {
throw new OnexException("渲染模板失败,表名:" + tableEntity.getTableName(), e);
}
}
}
Aggregations