package com.hx.auto; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import com.hx.auto.common.UrlData; import com.hx.auto.util.CommonTool; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; /** * 自动生成Service and ServiceImpl * * @author chenjiahe 2019年09月08日16:57:47 * */ public class GeneratorServiceUtil { private static String templateDir = "/ftl";//获取模板路径 private static String templateServiceName = "Service.tpl";//service名称 private static String templateServiceImplName = "ServiceImpl.tpl";//serviceImpl名称 /** * @param cl 实体类 * @param urlData 配置信息 * @throws Exception */ @SuppressWarnings("deprecation") public static void generatorService(Class cl, UrlData urlData) throws Exception { try { //获取类的包名 String className = cl.getSimpleName(); String[] strs = cl.getName().split("\\."); String packageName = ""; for(int i=0;i root = new HashMap(); root.put("className", classNamex); root.put("classNameUP", className); root.put("packageName", packageName); root.put("servicePack", urlData.getServiceUrl()[1]); root.put("actionPack", urlData.getActionUrl()[1]); System.out.println("serviceUrl:"+targetFile); // 将模板和数据模型合并 输出到Console Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8")); template.process(root, out); out.flush(); out.close(); if(!CommonTool.checkNotNull(urlData.getTotalUrl())) { System.err.println("没有设置总包路径"); return; } root.put("TotalPackageName",urlData.getTotalUrl()); root.put("serviceImpPack", urlData.getServiceImplUrl()[1]); root.put("daoPack", urlData.getDaoUrl()[1]); //serviceImp文件 // 获取或创建模板 Template template2 = configuration.getTemplate(templateServiceImplName); System.out.println("serviceImpUrl:"+targetFile2); // 将模板和数据模型合并 输出到Console Writer out2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile2), "UTF-8")); template2.process(root, out2); out2.flush(); out2.close(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } } }