1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
public static void gen(String sql, String packageName, String moduleName, String savePath, TemplateSource templateSource, Map<String, String> userReplace) throws IOException { ClassInfo classInfo = TableParseUtil.parseSql(sql); classInfo.setPackageName(packageName); classInfo.setModuleName(moduleName); classInfo.setBaseSavePath(savePath); System.out.println("【INFO】classInfo => " + classInfo.toString()); createFile(classInfo, "controller", classInfo.getClassName() + "Controller.java", templateSource.getControllerTemplate(), userReplace); createFile(classInfo, "pojo", classInfo.getClassName() + ".java", templateSource.getPojoTemplate(), userReplace); createFile(classInfo, "service" + File.separator + "impl", classInfo.getClassName() + "ServiceImpl.java", templateSource.getServiceImplTemplate(), userReplace); createFile(classInfo, "service", classInfo.getClassName() + "Service.java", templateSource.getServiceTemplate(), userReplace); createFile(classInfo, "mapper", classInfo.getClassName() + "Mapper.java", templateSource.getMapperTemplate(), userReplace); createFile(classInfo, "mapper", classInfo.getClassName() + "SqlBuilder.java", templateSource.getSqlBuilderTemplate(), userReplace); }
public static void createFile(ClassInfo classInfo, String path, String fileName, String tpl, Map<String, String> userReplace) throws IOException { if (tpl == null | "".equals(tpl)) { return; } String saveFileName = classInfo.getBaseSavePath() + File.separator + classInfo.getModuleName() + File.separator + path + File.separator + fileName; tpl = tpl.replaceAll("CLASS_NAME_UPPER", classInfo.getClassName()); tpl = tpl.replaceAll("CLASS_NAME_LOWER", classInfo.getClassNameLowerFirst()); tpl = tpl.replaceAll("TABLE_NAME", classInfo.getTableName()); tpl = tpl.replaceAll("TABLE_COMMENT", classInfo.getTableName()); tpl = tpl.replaceAll("PACKAGE", classInfo.getPackageName() + "." + classInfo.getModuleName()); if (userReplace != null) { for (Map.Entry<String, String> entry : userReplace.entrySet()) { tpl = tpl.replaceAll(entry.getKey(), entry.getValue()); } } StringBuilder stringBuffer = new StringBuilder(); for (Column column : classInfo.getFieldList()) { StringBuilder row = new StringBuilder(); if (column.getFieldComment() != null && !"".equals(column.getFieldComment())) { row.append(" /**\n" + " * ").append(column.getFieldComment()).append("\n").append(" */\n"); } String javaFieldName = StringUtil.lowerCaseFirst(StringUtil.underlineToCamelCase(column.getFieldName())); if (!column.getFieldName().equals(javaFieldName)) { row.append(" @Column(name = \"").append(column.getFieldName()).append("\")\n"); } row.append(" private ").append(dbTypeToJava(column.getFieldType())).append(" ").append(javaFieldName).append(";\n"); stringBuffer.append(row); } tpl = tpl.replaceAll("POJO_FIELD", stringBuffer.toString()); writeToFile(saveFileName, tpl); }
|