diff --git a/boot-starter/boot-starter-util/pom.xml b/boot-starter/boot-starter-util/pom.xml index e4209eb..bc555d9 100644 --- a/boot-starter/boot-starter-util/pom.xml +++ b/boot-starter/boot-starter-util/pom.xml @@ -47,6 +47,10 @@ commons-io commons-io + + org.freemarker + freemarker + junit junit diff --git a/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtils.java b/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtil.java similarity index 95% rename from boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtils.java rename to boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtil.java index 357578e..b2a511b 100644 --- a/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtils.java +++ b/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FileUtil.java @@ -8,7 +8,7 @@ import java.io.File; * @version 1.0 * @since 1.0 */ -public class FileUtils { +public class FileUtil { /** * 修改文件夹下的所有符合条件的文件的扩展名,递归文件夹下所有文件 * @param dir 文件夹的路径 diff --git a/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FreemarkerUtil.java b/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FreemarkerUtil.java new file mode 100644 index 0000000..594690f --- /dev/null +++ b/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FreemarkerUtil.java @@ -0,0 +1,89 @@ +package com.micro.fast.util; + +import freemarker.core.ParseException; +import freemarker.template.*; + +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +/** + * 根据freemarker模板生成文件 + */ +public class FreemarkerUtil { + + /** + * 加载模板文件所在的目录 + */ + private static final String TEMPLATES_DIR_URL = "templates"; + + private static Configuration cfg = null; + + static { + if (cfg==null){ + cfg = new Configuration(); + } + //设置末班文件的加载目录 + setConfiguration(FreemarkerUtil.TEMPLATES_DIR_URL); + } + + private static void setConfiguration(String templatesDirUrl) { + + //1.设置编码 + cfg.setDefaultEncoding("utf-8"); + //2.设置加载模板文件的位置 + String classPath = FreemarkerUtil.class.getResource("/"+templatesDirUrl).getPath(); + try { + cfg.setDirectoryForTemplateLoading(new File(classPath)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static boolean createFile(String templateName, Map data, String targetFileName) { + Template template = null; + Writer out = null; + //1.获取classpath路径 + String classPath = FreemarkerUtil.class.getResource("/").getPath(); + //2.如果目标文件存在就先删除目标文件 + File targetFile = new File(classPath + targetFileName); + if (targetFile.exists()){ + targetFile.delete(); + } + try { + //3.获取模板文件对象 + template = cfg.getTemplate(templateName); + //4.获取输出流对象 + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(classPath+targetFileName))); + //5.向模板对象中填充数据生成文件 + template.process(data,out); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return false; + } catch (MalformedTemplateNameException e) { + e.printStackTrace(); + return false; + } catch (ParseException e) { + e.printStackTrace(); + return false; + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (TemplateException e) { + e.printStackTrace(); + return false; + } finally { + try { + out.flush(); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + //创建成功 + return true; + } + + +} diff --git a/boot-starter/boot-starter-util/src/main/resources/templates/generatorConfig.fm b/boot-starter/boot-starter-util/src/main/resources/templates/generatorConfig.fm new file mode 100644 index 0000000..ce8f51a --- /dev/null +++ b/boot-starter/boot-starter-util/src/main/resources/templates/generatorConfig.fm @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/boot-starter/boot-starter-util/src/test/java/com/micro/fast/util/FreemarkerUtilTest.java b/boot-starter/boot-starter-util/src/test/java/com/micro/fast/util/FreemarkerUtilTest.java new file mode 100644 index 0000000..1ccd265 --- /dev/null +++ b/boot-starter/boot-starter-util/src/test/java/com/micro/fast/util/FreemarkerUtilTest.java @@ -0,0 +1,15 @@ +package com.micro.fast.util; + +import org.junit.Test; + +import java.util.HashMap; + + +public class FreemarkerUtilTest { + @Test + public void createFile() throws Exception { + HashMap model = new HashMap<>(); + model.put("pojoPackage","com.lishouyu.com.pojo"); + FreemarkerUtil.createFile("generatorConfig.fm",model,"a.xml"); + } +} \ No newline at end of file