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.lang.reflect.Field; import java.util.HashMap; import java.util.zip.ZipOutputStream; import com.gitee.sunchenbin.mybatis.actable.annotation.Column; import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; import com.hx.auto.common.JdbcType; import com.hx.auto.common.ReadEntityData; import com.hx.auto.common.UrlData; import com.hx.auto.util.CommonTool; import com.hx.auto.util.GeneratorClassParentUtil; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; /** * 自动生成mapper.xml * * @author chenjiahe 2019年09月08日23:57:47 * */ public class GeneratorMapperUtil { private static String templateDir = "/ftl";//获取模板路径 private static String templateName = "Mapper.tpl";//action模板名称 /**生成mapper.xml * @param cl 实体类 * @param urlData 生成配置信息 * @throws Exception */ public static void generatorMapper(Class cl,UrlData urlData) throws Exception { try { // 反射start // 类名 String entityName = cl.getSimpleName(); // 类名首字母小写 String initial = entityName.substring(0, 1).toLowerCase(); String entityNameSmall = initial + entityName.substring(1, entityName.length()); //获取实体类包名 String[] strs = cl.getName().split("\\."); String packageName = ""; //去掉类名 for(int i=0;i root = new HashMap(); //action包名 if(!CommonTool.checkNotNull(urlData.getActionUrl())) { System.err.println("没有生成action路径"); return; } if(!CommonTool.checkNotNull(urlData.getTotalUrl())) { System.err.println("没有设置总包路径"); return; } root.put("TotalPackageName",urlData.getTotalUrl()); root.put("packageActionName",urlData.getActionUrl()[1]); root.put("packageEntityName",packageName); //实体类的类名 root.put("entityName", entityName); //实体类的类名(首字母小写) root.put("entityNameSmall", entityNameSmall); //表字段 数据 root.put("fieldData",readEntityData); root.put("sqlSentence","${sqlSentence}"); //root.put("sqlSentence","${sqlSentence}"); //root.put("sqlSentence","${sqlSentence}"); //dao的包名 if(!CommonTool.checkNotNull(urlData.getDaoUrl())) { System.err.println("没有dao路径"); return; } root.put("DAOPackageName",urlData.getDaoUrl()[1]); //实体类的包名 root.put("entityPackageName",packageName); //生成文件路径 String targetFile = urlData.getMapperUrl()[1].replace(".", "/")+"/"; //生成文件名称 targetFile += entityName + "Mapper.xml"; //补全路径 targetFile = "./"+urlData.getMapperUrl()[0].replace(".", "/")+"/"+targetFile; //获取是否已经生成过文件,拿取自定义的内容 String xmlStr = GeneratorReadXmlUtil.readMapperXml(targetFile); if(!"false".equals(xmlStr)){ root.put("customData",GeneratorReadXmlUtil.readMapperXml(targetFile)); }else{ root.put("customData",""); } System.out.println("mapperUrl:"+targetFile); // 将模板和数据模型合并 输出到Console Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8")); template.process(root, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }