代码自动生成
最近开发几个查询接口,从代码来看,基本的格式都是一样的。就想着写一个代码生成工具。直接生成代码。省时省力。经过测试,生成的代码完全可用,节省了至少80%的时间。这里做一个记录。以后有类似的情况就可以拿来改一改就可以用了。
package com.insigma.business.uploadreport.zCode; import cn.hutool.core.util.StrUtil; import java.io.*; import java.net.URISyntaxException; import java.util.*; /** * 自动生成代码 * 查询接口代码自动生成工具:只需定义好要生成的接口 功能名称,注释信息,列表接口和详情接口的入参出参。 * 就可以生成controller,service,mapper的相应方法。 * 注意:生成的provider中对应的查询方法是留空的。需要自己完善。 * * @author ank * @version 1.0 * @date 2022/7/5 10:37 */ public class AutomaticallyGenerateCode { /** * 指定路径文件 从 flagStr 按行添加 writeContent * @param writeContent * @param path * @param flagStr * @throws IOException * @throws URISyntaxException */ public static void write2File(List<String> writeContent, String path, String flagStr) throws IOException, URISyntaxException { // 创建临时文件 File outFile = File.createTempFile("fileTmp", ".tmp"); // 源文件 File testFile = new File(path); // 源文件输入流 FileInputStream fis = new FileInputStream(testFile); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); // 源文件输出流 FileOutputStream fos = new FileOutputStream(outFile); PrintWriter out = new PrintWriter(fos); // 保存一行数据 String thisLine; // jvm 退出 临时文件删除 outFile.deleteOnExit(); while ((thisLine = in.readLine()) != null) { System.out.println(thisLine); // 当读取到目标行时 写入需要写入的内容 if (thisLine.equals(flagStr)) { writeContent.forEach(s-> { out.println(s); }); } // 输出读取到的数据 out.println(thisLine); } // 各种关 out.flush(); out.close(); in.close(); // 删除原始文件 testFile.delete(); // 把临时文件改名为原文件名 outFile.renameTo(testFile); } /** * 生成java文件 * @param writeContent * @param filepath * @throws IOException */ public static void autoGenJava(List<String> writeContent, String filepath) throws IOException { // 创建文件 File outFile = new File(filepath); // 源文件输出流 FileOutputStream fos = new FileOutputStream(outFile); PrintWriter out = new PrintWriter(fos); writeContent.forEach(s-> { out.println(s); }); // 各种关 out.flush(); out.close(); } /** * 生成vo对象 * @param param1 * @param param2 * @param listMap 列表vo对象的 <属性:注释> 键值对 * @param infoMap 详情vo对象的 <属性:注释> 键值对 * @throws IOException */ public static void autoGenVO(String param1, String param2, Map<String,String> listMap, Map<String,String> infoMap) throws IOException { String param3 = param2.replace("情况",""); String filepath = "D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\vo\\" + StrUtil.upperFirst(param2) + "VO.java"; String filepath2 = "D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\vo\\" + StrUtil.upperFirst(param2) + "InfoVO.java"; List<String> writeContent = new ArrayList<String>(); writeContent.add("package com.insigma.business.uploadreport.vo;"); writeContent.add(""); writeContent.add("import io.swagger.annotations.ApiModelProperty;"); writeContent.add("import lombok.Data;"); writeContent.add(""); writeContent.add("import javax.validation.constraints.NotBlank;"); writeContent.add(""); writeContent.add("/**"); writeContent.add(" * "+param1+"列表传入参数"); writeContent.add(" * @author ank"); writeContent.add(" * @date 2022/07/04"); writeContent.add(" */"); writeContent.add("@Data"); writeContent.add("public class "+ StrUtil.upperFirst(param2) +"VO {"); if(null == listMap){ writeContent.add(""); writeContent.add(" @ApiModelProperty(\"所属省/市行政区划代码\")"); writeContent.add(" private String aab301;"); writeContent.add(""); writeContent.add(" @NotBlank(message = \"父级行政区划代码不能为空\")"); writeContent.add(" @ApiModelProperty(\"父级行政区划代码\")"); writeContent.add(" private String aaa148;"); writeContent.add(""); writeContent.add(" @ApiModelProperty(\""+param3+"上报时间-开始\")"); writeContent.add(" private String ape727_s;"); writeContent.add(""); writeContent.add(" @ApiModelProperty(\""+param3+"上报时间-结束\")"); writeContent.add(" private String ape727_e;"); writeContent.add(""); }else{ Set<Map.Entry<String, String>> entries = listMap.entrySet(); for (Map.Entry<String, String> entry : entries) { writeContent.add(""); writeContent.add(" @ApiModelProperty(\""+entry.getValue()+"\")"); writeContent.add(" private String "+entry.getKey()+";"); } } writeContent.add("}"); writeContent.add(""); List<String> writeContent2 = new ArrayList<String>(); writeContent2.add("package com.insigma.business.uploadreport.vo;"); writeContent2.add(""); writeContent2.add("import io.swagger.annotations.ApiModelProperty;"); writeContent2.add("import lombok.Data;"); writeContent2.add(""); writeContent2.add("/**"); writeContent2.add(" * "+param1+"详情传入参数"); writeContent2.add(" * @author ank"); writeContent2.add(" * @date 2022/07/04"); writeContent2.add(" */"); writeContent2.add("@Data"); writeContent2.add("public class "+ StrUtil.upperFirst(param2) +"InfoVO {"); Set<Map.Entry<String, String>> entries = infoMap.entrySet(); for (Map.Entry<String, String> entry : entries) { writeContent2.add(""); writeContent2.add(" @ApiModelProperty(\""+entry.getValue()+"\")"); writeContent2.add(" private String "+entry.getKey()+";"); } writeContent2.add(""); writeContent2.add(""); writeContent2.add("}"); writeContent2.add(""); try { AutomaticallyGenerateCode.autoGenJava(writeContent, filepath); AutomaticallyGenerateCode.autoGenJava(writeContent2, filepath2); }catch (IOException e) { e.printStackTrace(); } } /** * 生成dto对象 * @param param1 * @param param2 * @param listMap2 列表dto对象的 <属性:注释> 键值对 * @param infoMap2 详情dto对象的 <属性:注释> 键值对 * @throws IOException */ public static void autoGenDTO(String param1, String param2, Map<String,String> listMap2,Map<String,String> infoMap2) throws IOException { String param3 = param2.replace("情况",""); String filepath = "D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\dto\\" + StrUtil.upperFirst(param2) + "DTO.java"; String filepath2 = "D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\dto\\" + StrUtil.upperFirst(param2) + "InfoDTO.java"; List<String> writeContent = new ArrayList<String>(); writeContent.add("package com.insigma.business.uploadreport.dto;"); writeContent.add(""); writeContent.add(""); writeContent.add("import com.insigma.business.util.excel.ExcelColumn;"); writeContent.add("import io.swagger.annotations.ApiModelProperty;"); writeContent.add("import lombok.Data;"); writeContent.add(""); writeContent.add("import java.io.Serializable;"); writeContent.add(""); writeContent.add("/**"); writeContent.add(" * 查询"+param3+"返回列表"); writeContent.add(" * @author ank"); writeContent.add(" * @date 2022/07/04"); writeContent.add(" */"); writeContent.add("@Data"); writeContent.add("public class "+ StrUtil.upperFirst(param2) +"DTO implements Serializable {"); writeContent.add(""); writeContent.add(" private static final long serialVersionUID = 1L;"); if(null == listMap2){ writeContent.add(""); writeContent.add(" @ExcelColumn(title = \"地方名称\")"); writeContent.add(" private String aaa146;"); writeContent.add(""); writeContent.add(" private String aaf018;"); writeContent.add(""); writeContent.add(" @ExcelColumn(title = \""+param3+"总数\")"); writeContent.add(" private Integer total;"); }else{ Set<Map.Entry<String, String>> entries = listMap2.entrySet(); for (Map.Entry<String, String> entry : entries) { writeContent.add(""); writeContent.add(" @ApiModelProperty(\""+entry.getValue()+"\")"); writeContent.add(" private String "+entry.getKey()+";"); } } writeContent.add("}"); writeContent.add(""); List<String> writeContent2 = new ArrayList<String>(); writeContent2.add("package com.insigma.business.uploadreport.dto;"); writeContent2.add(""); writeContent2.add("import io.swagger.annotations.ApiModelProperty;"); writeContent2.add("import lombok.Data;"); writeContent2.add("import java.io.Serializable;"); writeContent2.add(""); writeContent2.add("/**"); writeContent2.add(" * "+param2+"详情返回结果"); writeContent2.add(" * @author ank"); writeContent2.add(" * @date 2022/07/04"); writeContent2.add(" */"); writeContent2.add("@Data"); writeContent2.add("public class "+ StrUtil.upperFirst(param2) +"InfoDTO implements Serializable {"); writeContent2.add(""); writeContent2.add(" private static final long serialVersionUID = 1L;"); Set<Map.Entry<String, String>> entries = infoMap2.entrySet(); for (Map.Entry<String, String> entry : entries) { writeContent2.add(""); writeContent2.add(" @ApiModelProperty(\""+entry.getValue()+"\")"); writeContent2.add(" private String "+entry.getKey()+";"); } writeContent2.add(""); writeContent2.add(""); writeContent2.add("}"); writeContent2.add(""); try { AutomaticallyGenerateCode.autoGenJava(writeContent, filepath); AutomaticallyGenerateCode.autoGenJava(writeContent2, filepath2); }catch (IOException e) { e.printStackTrace(); } } /** * 生成controller方法 */ public static void autoGenControllerMethod(String param1,String param2){ String controllerPath = new String("D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\controller\\LocalReportingController.java"); String flagStr = "}"; //插入方法内容 List<String> writeContent = new ArrayList<String>(); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"列表"); writeContent.add(" * @param "+param2+"VO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" @ApiOperation(\""+param1+"列表\")"); writeContent.add(" @PostMapping(\"/"+param2+"/list\")"); writeContent.add(" public ResponseMessage get"+ StrUtil.upperFirst(param2) +"List(@Valid @RequestBody "+ StrUtil.upperFirst(param2) +"VO "+param2+"VO) {"); writeContent.add(" return localReportingService.get"+ StrUtil.upperFirst(param2) +"List("+param2+"VO);"); writeContent.add(" }"); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"详情"); writeContent.add(" * @param "+param2+"InfoVO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" @ApiOperation(\""+param1+"详情\")"); writeContent.add(" @PostMapping(\"/"+param2+"/info\")"); writeContent.add(" public ResponseMessage get"+ StrUtil.upperFirst(param2) +"Info(@RequestBody "+ StrUtil.upperFirst(param2) +"InfoVO "+param2+"InfoVO) {"); writeContent.add(" return localReportingService.get"+ StrUtil.upperFirst(param2) +"Info("+param2+"InfoVO);"); writeContent.add(" }"); try { AutomaticallyGenerateCode.write2File(writeContent, controllerPath, flagStr); }catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } /** * 生成service方法 */ public static void autoGenServiceMethod(String param1,String param2){ String servicePath = new String("D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\service\\LocalReportingService.java"); String serviceImplPath = new String("D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\service\\impl\\LocalReportingServiceImpl.java"); String flagStr = "}"; //插入方法内容 List<String> writeContent = new ArrayList<String>(); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"列表"); writeContent.add(" * @param "+param2+"VO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" ResponseMessage get"+ StrUtil.upperFirst(param2) +"List("+ StrUtil.upperFirst(param2) +"VO "+param2+"VO);"); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"详情"); writeContent.add(" * @param "+param2+"InfoVO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" ResponseMessage get"+ StrUtil.upperFirst(param2) +"Info("+ StrUtil.upperFirst(param2) +"InfoVO "+param2+"InfoVO);"); List<String> writeContent2 = new ArrayList<String>(); writeContent2.add(""); writeContent2.add(" @Override"); writeContent2.add(" public ResponseMessage get"+ StrUtil.upperFirst(param2) +"List("+ StrUtil.upperFirst(param2) +"VO "+param2+"VO) {"); writeContent2.add(" try {"); writeContent2.add(" //获取数据"); writeContent2.add(" List<"+ StrUtil.upperFirst(param2) +"DTO> list = localReportingMapper.get"+ StrUtil.upperFirst(param2) +"List("+param2+"VO);"); writeContent2.add(" return ResponseMessage.ok(\"获取成功\", list);"); writeContent2.add(" } catch (Exception e) {"); writeContent2.add(" log.error(\"查询"+param1+"-列表异常\", e);"); writeContent2.add(" return ResponseMessage.error(\"查询"+param1+"-列表异常\");"); writeContent2.add(" }"); writeContent2.add(" }"); writeContent2.add(""); writeContent2.add(" @Override"); writeContent2.add(" public ResponseMessage get"+ StrUtil.upperFirst(param2) +"Info("+ StrUtil.upperFirst(param2) +"InfoVO "+param2+"InfoVO) {"); writeContent2.add(" try {"); writeContent2.add(" //获取数据"); writeContent2.add(" List<"+ StrUtil.upperFirst(param2) +"InfoDTO> list = localReportingMapper.get"+ StrUtil.upperFirst(param2) +"Info("+param2+"InfoVO);"); writeContent2.add(" return ResponseMessage.ok(\"获取成功\", list);"); writeContent2.add(" } catch (Exception e) {"); writeContent2.add(" log.error(\"查询"+param1+"详情异常\", e);"); writeContent2.add(" return ResponseMessage.error(\"查询"+param1+"详情异常\");"); writeContent2.add(" }"); writeContent2.add(" }"); try { AutomaticallyGenerateCode.write2File(writeContent, servicePath, flagStr); AutomaticallyGenerateCode.write2File(writeContent2, serviceImplPath, flagStr); }catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } /** * 生成mapper方法 */ public static void autoGenMapperMethod(String param1,String param2){ String mapperPath = new String("D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\mapper\\LocalReportingMapper.java"); String providerPath = new String("D:\\editor\\workspace\\migrantWorkers\\managesystem-backend\\insiis-web\\src\\main\\java\\com\\insigma\\business\\uploadreport\\mapper\\provider\\LocalReportingMapperProvider.java"); String flagStr = "}"; //插入方法内容 List<String> writeContent = new ArrayList<String>(); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"列表"); writeContent.add(" * @param "+param2+"VO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" @SelectProvider(type = LocalReportingMapperProvider.class, method = \"get"+ StrUtil.upperFirst(param2) +"List\")"); writeContent.add(" List<"+ StrUtil.upperFirst(param2) +"DTO> get"+ StrUtil.upperFirst(param2) +"List(@Param(\""+param2+"Req\") "+ StrUtil.upperFirst(param2) +"VO "+param2+"VO);"); writeContent.add(""); writeContent.add(" /**"); writeContent.add(" * "+param1+"详情"); writeContent.add(" * @param "+param2+"InfoVO"); writeContent.add(" * @return"); writeContent.add(" */"); writeContent.add(" @SelectProvider(type = LocalReportingMapperProvider.class, method = \"get"+ StrUtil.upperFirst(param2) +"Info\")"); writeContent.add(" List<"+ StrUtil.upperFirst(param2) +"InfoDTO> get"+ StrUtil.upperFirst(param2) +"Info(@Param(\""+param2+"InfoReq\") "+ StrUtil.upperFirst(param2) +"InfoVO "+param2+"InfoVO);"); List<String> writeContent2 = new ArrayList<String>(); writeContent2.add(" /**"); writeContent2.add(" * "+param1+"列表"); writeContent2.add(" * @param "+param2+"VO"); writeContent2.add(" * @return"); writeContent2.add(" */"); writeContent2.add(" public String get"+ StrUtil.upperFirst(param2) +"List(@Param(\""+param2+"Req\") "+ StrUtil.upperFirst(param2) +"VO "+param2+"VO) {"); writeContent2.add(" SQL sql = new SQL();"); writeContent2.add(" // TODO"); writeContent2.add(" return sql.toString();"); writeContent2.add(" }"); writeContent2.add(""); writeContent2.add(" /**"); writeContent2.add(" * "+param1+"详情"); writeContent2.add(" * @param "+param2+"InfoVO"); writeContent2.add(" * @return"); writeContent2.add(" */"); writeContent2.add(" public String get"+ StrUtil.upperFirst(param2) +"Info(@Param(\""+param2+"InfoReq\") "+ StrUtil.upperFirst(param2) +"InfoVO "+param2+"InfoVO) {"); writeContent2.add(" SQL sql = new SQL();"); writeContent2.add(" // TODO"); writeContent2.add(" return sql.toString();"); writeContent2.add(" }"); try { AutomaticallyGenerateCode.write2File(writeContent, mapperPath, flagStr); AutomaticallyGenerateCode.write2File(writeContent2, providerPath, flagStr); }catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } } /** * 生成代码 * @param param1 功能注释 * @param param2 功能名 * @param listMap 列表查询参数 可为null * @param listMap2 列表返回参数 可为null * @param infoMap 详情查询参数 * @param infoMap2 详情返回属性 * @throws IOException * @throws URISyntaxException */ public static void genCode(String param1,String param2,Map listMap,Map listMap2,Map infoMap,Map infoMap2) throws IOException, URISyntaxException{ autoGenControllerMethod(param1,param2); autoGenServiceMethod(param1,param2); autoGenMapperMethod(param1,param2); autoGenVO(param1,param2,listMap,infoMap); autoGenDTO(param1,param2,listMap2,infoMap2); } public static void main(String[] args) throws IOException, URISyntaxException { // String param1 = "保证金情况"; // String param2 = "securityDeposit"; // // vo详情字段 传入参数 // Map<String,String> map = new HashMap<String,String>(); // map.put("aaf018","所属省行政区划代码"); // map.put("ape727_s","保证金信息上报时间-开始"); // map.put("ape727_e","保证金信息上报时间-结束"); // map.put("aaf017","所属市行政区划代码"); // map.put("aae044","项目名称"); // map.put("abb127","保证金缴纳主体"); // // dto详情字段 传出参数 // Map<String,String> map2 = new HashMap<String,String>(); // map2.put("aaa146","地方名称"); // map2.put("aae044","项目名称"); // map2.put("abb127","保证金缴纳主体名称"); // map2.put("abb156","保证金缴纳主体统一社会信用代码"); // map2.put("abb128","保证金缴纳方式"); // map2.put("abb129","应缴金额"); // map2.put("abe166","是否享受优惠政策"); // map2.put("abb131","减免缓金额"); // map2.put("ape727","保证金缴纳时间"); // genCode(param1,param2,null,null,infoMap,infoMap2); String param1 = "工资支付情况"; String param2 = "salaryPayment"; // vo列表字段 传入参数 Map<String,String> listMap = new HashMap<String,String>(); listMap.put("aab301","所属行政区划代码(本级)"); listMap.put("aaa148","所属行政区划代码(父级)"); listMap.put("ape727_s","工资支付上报时间-开始"); listMap.put("ape727_e","工资支付上报时间-结束"); // dto列表字段 传出参数 Map<String,String> listMap2 = new HashMap<String,String>(); listMap2.put("aaa146","地方名称"); listMap2.put("aaf018","所属省行政区划代码"); listMap2.put("salaryTotal","应发工资总额度(万元)"); listMap2.put("netSalaryTotal","实发工资总额度(万元)"); // vo详情字段 传入参数 Map<String,String> infoMap = new HashMap<String,String>(); infoMap.put("aaf018","所属省行政区划代码"); infoMap.put("ape727_s","工资支付上报时间-开始"); infoMap.put("ape727_e","工资支付上报时间-结束"); infoMap.put("aaf017","所属市行政区划代码"); infoMap.put("aae044","项目名称"); infoMap.put("abe175","银行支付业务流水号"); infoMap.put("abc110","支付方式"); // dto详情字段 传出参数 Map<String,String> infoMap2 = new HashMap<String,String>(); infoMap2.put("abe121","支付账号"); infoMap2.put("aaa146","地方名称"); infoMap2.put("aae044","项目名称"); infoMap2.put("aae149","所属年月"); infoMap2.put("abc112Total","当月应发工资总额"); infoMap2.put("abc113Total","当月扣发工资总额"); infoMap2.put("abc114Total","当月实发工资总额"); infoMap2.put("attendanceTotal","当月考勤总人数"); infoMap2.put("abc117","工资发放人数"); infoMap2.put("abc110","工资支付方式"); // genCode(param1,param2,listMap,listMap2,infoMap,infoMap2); // 编写查询sql //............2233 } }
学如逆水行舟,不进则退