You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CodeGenUtil.java 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.xkcoding.codegen.utils;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.io.IoUtil;
  4. import cn.hutool.core.util.CharsetUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.db.Entity;
  7. import cn.hutool.setting.dialect.Props;
  8. import com.google.common.collect.Lists;
  9. import com.xkcoding.codegen.constants.GenConstants;
  10. import com.xkcoding.codegen.entity.ColumnEntity;
  11. import com.xkcoding.codegen.entity.GenConfig;
  12. import com.xkcoding.codegen.entity.TableEntity;
  13. import lombok.experimental.UtilityClass;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.text.WordUtils;
  16. import org.apache.velocity.Template;
  17. import org.apache.velocity.VelocityContext;
  18. import org.apache.velocity.app.Velocity;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.StringWriter;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.*;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipOutputStream;
  26. /**
  27. * <p>
  28. * 代码生成器 工具类
  29. * </p>
  30. *
  31. * @author yangkai.shen
  32. * @date Created in 2019-03-22 09:27
  33. */
  34. @Slf4j
  35. @UtilityClass
  36. public class CodeGenUtil {
  37. private final String ENTITY_JAVA_VM = "Entity.java.vm";
  38. private final String MAPPER_JAVA_VM = "Mapper.java.vm";
  39. private final String SERVICE_JAVA_VM = "Service.java.vm";
  40. private final String SERVICE_IMPL_JAVA_VM = "ServiceImpl.java.vm";
  41. private final String CONTROLLER_JAVA_VM = "Controller.java.vm";
  42. private final String MAPPER_XML_VM = "Mapper.xml.vm";
  43. private final String API_JS_VM = "api.js.vm";
  44. private List<String> getTemplates() {
  45. List<String> templates = new ArrayList<>();
  46. templates.add("template/Entity.java.vm");
  47. templates.add("template/Mapper.java.vm");
  48. templates.add("template/Mapper.xml.vm");
  49. templates.add("template/Service.java.vm");
  50. templates.add("template/ServiceImpl.java.vm");
  51. templates.add("template/Controller.java.vm");
  52. templates.add("template/api.js.vm");
  53. return templates;
  54. }
  55. /**
  56. * 生成代码
  57. */
  58. public void generatorCode(GenConfig genConfig, Entity table, List<Entity> columns, ZipOutputStream zip) {
  59. //配置信息
  60. Props propsDB2Java = getConfig("generator.properties");
  61. Props propsDB2Jdbc = getConfig("jdbc_type.properties");
  62. boolean hasBigDecimal = false;
  63. //表信息
  64. TableEntity tableEntity = new TableEntity();
  65. tableEntity.setTableName(table.getStr("tableName"));
  66. if (StrUtil.isNotBlank(genConfig.getComments())) {
  67. tableEntity.setComments(genConfig.getComments());
  68. } else {
  69. tableEntity.setComments(table.getStr("tableComment"));
  70. }
  71. String tablePrefix;
  72. if (StrUtil.isNotBlank(genConfig.getTablePrefix())) {
  73. tablePrefix = genConfig.getTablePrefix();
  74. } else {
  75. tablePrefix = propsDB2Java.getStr("tablePrefix");
  76. }
  77. //表名转换成Java类名
  78. String className = tableToJava(tableEntity.getTableName(), tablePrefix);
  79. tableEntity.setCaseClassName(className);
  80. tableEntity.setLowerClassName(StrUtil.lowerFirst(className));
  81. //列信息
  82. List<ColumnEntity> columnList = Lists.newArrayList();
  83. for (Entity column : columns) {
  84. ColumnEntity columnEntity = new ColumnEntity();
  85. columnEntity.setColumnName(column.getStr("columnName"));
  86. columnEntity.setDataType(column.getStr("dataType"));
  87. columnEntity.setComments(column.getStr("columnComment"));
  88. columnEntity.setExtra(column.getStr("extra"));
  89. //列名转换成Java属性名
  90. String attrName = columnToJava(columnEntity.getColumnName());
  91. columnEntity.setCaseAttrName(attrName);
  92. columnEntity.setLowerAttrName(StrUtil.lowerFirst(attrName));
  93. //列的数据类型,转换成Java类型
  94. String attrType = propsDB2Java.getStr(columnEntity.getDataType(), "unknownType");
  95. columnEntity.setAttrType(attrType);
  96. String jdbcType = propsDB2Jdbc.getStr(columnEntity.getDataType(), "unknownType");
  97. columnEntity.setJdbcType(jdbcType);
  98. if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
  99. hasBigDecimal = true;
  100. }
  101. //是否主键
  102. if ("PRI".equalsIgnoreCase(column.getStr("columnKey")) && tableEntity.getPk() == null) {
  103. tableEntity.setPk(columnEntity);
  104. }
  105. columnList.add(columnEntity);
  106. }
  107. tableEntity.setColumns(columnList);
  108. //没主键,则第一个字段为主键
  109. if (tableEntity.getPk() == null) {
  110. tableEntity.setPk(tableEntity.getColumns().get(0));
  111. }
  112. //设置velocity资源加载器
  113. Properties prop = new Properties();
  114. prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  115. Velocity.init(prop);
  116. //封装模板数据
  117. Map<String, Object> map = new HashMap<>(16);
  118. map.put("tableName", tableEntity.getTableName());
  119. map.put("pk", tableEntity.getPk());
  120. map.put("className", tableEntity.getCaseClassName());
  121. map.put("classname", tableEntity.getLowerClassName());
  122. map.put("pathName", tableEntity.getLowerClassName().toLowerCase());
  123. map.put("columns", tableEntity.getColumns());
  124. map.put("hasBigDecimal", hasBigDecimal);
  125. map.put("datetime", DateUtil.now());
  126. map.put("year", DateUtil.year(new Date()));
  127. if (StrUtil.isNotBlank(genConfig.getComments())) {
  128. map.put("comments", genConfig.getComments());
  129. } else {
  130. map.put("comments", tableEntity.getComments());
  131. }
  132. if (StrUtil.isNotBlank(genConfig.getAuthor())) {
  133. map.put("author", genConfig.getAuthor());
  134. } else {
  135. map.put("author", propsDB2Java.getStr("author"));
  136. }
  137. if (StrUtil.isNotBlank(genConfig.getModuleName())) {
  138. map.put("moduleName", genConfig.getModuleName());
  139. } else {
  140. map.put("moduleName", propsDB2Java.getStr("moduleName"));
  141. }
  142. if (StrUtil.isNotBlank(genConfig.getPackageName())) {
  143. map.put("package", genConfig.getPackageName());
  144. map.put("mainPath", genConfig.getPackageName());
  145. } else {
  146. map.put("package", propsDB2Java.getStr("package"));
  147. map.put("mainPath", propsDB2Java.getStr("mainPath"));
  148. }
  149. VelocityContext context = new VelocityContext(map);
  150. //获取模板列表
  151. List<String> templates = getTemplates();
  152. for (String template : templates) {
  153. //渲染模板
  154. StringWriter sw = new StringWriter();
  155. Template tpl = Velocity.getTemplate(template, CharsetUtil.UTF_8);
  156. tpl.merge(context, sw);
  157. try {
  158. //添加到zip
  159. zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(), map.get("moduleName").toString()))));
  160. IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
  161. IoUtil.close(sw);
  162. zip.closeEntry();
  163. } catch (IOException e) {
  164. throw new RuntimeException("渲染模板失败,表名:" + tableEntity.getTableName(), e);
  165. }
  166. }
  167. }
  168. /**
  169. * 列名转换成Java属性名
  170. */
  171. private String columnToJava(String columnName) {
  172. return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
  173. }
  174. /**
  175. * 表名转换成Java类名
  176. */
  177. private String tableToJava(String tableName, String tablePrefix) {
  178. if (StrUtil.isNotBlank(tablePrefix)) {
  179. tableName = tableName.replaceFirst(tablePrefix, "");
  180. }
  181. return columnToJava(tableName);
  182. }
  183. /**
  184. * 获取配置信息
  185. */
  186. private Props getConfig(String fileName) {
  187. Props props = new Props(fileName);
  188. props.autoLoad(true);
  189. return props;
  190. }
  191. /**
  192. * 获取文件名
  193. */
  194. private String getFileName(String template, String className, String packageName, String moduleName) {
  195. // 包路径
  196. String packagePath = GenConstants.SIGNATURE + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
  197. // 资源路径
  198. String resourcePath = GenConstants.SIGNATURE + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator;
  199. // api路径
  200. String apiPath = GenConstants.SIGNATURE + File.separator + "api" + File.separator;
  201. if (StrUtil.isNotBlank(packageName)) {
  202. packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
  203. }
  204. if (template.contains(ENTITY_JAVA_VM)) {
  205. return packagePath + "entity" + File.separator + className + ".java";
  206. }
  207. if (template.contains(MAPPER_JAVA_VM)) {
  208. return packagePath + "mapper" + File.separator + className + "Mapper.java";
  209. }
  210. if (template.contains(SERVICE_JAVA_VM)) {
  211. return packagePath + "service" + File.separator + className + "Service.java";
  212. }
  213. if (template.contains(SERVICE_IMPL_JAVA_VM)) {
  214. return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
  215. }
  216. if (template.contains(CONTROLLER_JAVA_VM)) {
  217. return packagePath + "controller" + File.separator + className + "Controller.java";
  218. }
  219. if (template.contains(MAPPER_XML_VM)) {
  220. return resourcePath + "mapper" + File.separator + className + "Mapper.xml";
  221. }
  222. if (template.contains(API_JS_VM)) {
  223. return apiPath + className.toLowerCase() + ".js";
  224. }
  225. return null;
  226. }
  227. }