diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/pom.xml b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/pom.xml new file mode 100644 index 0000000..ef5be09 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + mybatis-generator-extend-maven-plugin + jar + + mybatis-generator-extend + mybatis-generator-extend-maven-plugin + + + com.micro.fast + boot-starter + 0.0.1-SNAPSHOT + + + + + com.micro.fast + boot-starter-util + ${micro.fast.version} + + + org.freemarker + freemarker + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + mybatis-generator-extend-maven-plugin + + \ No newline at end of file diff --git a/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java new file mode 100644 index 0000000..3a061c1 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/java/com/micro/fast/mybatis/generator/extend/util/FreemarkerUtil.java @@ -0,0 +1,135 @@ +package com.micro.fast.mybatis.generator.extend.util; + +import freemarker.core.ParseException; +import freemarker.template.*; + +import java.io.*; +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); + } + + /** + * 设置freemarker模板引擎配置对象的参数 + * @param templatesDirUrl 相对于mybatis-generate-extend项目的classpath的相对路径 + */ + private static void setConfiguration(String templatesDirUrl) { + + //1.设置编码 + cfg.setDefaultEncoding("utf-8"); + //2.设置加载模板文件的位置,这个位置是随着这个class加载的目录位置而确定的 + String relativeClasspath = FreemarkerUtil.class.getResource("").getPath(); + int lastIndexOf = relativeClasspath.lastIndexOf("com/micro/fast"); + String absoluteClasspath = relativeClasspath.substring(0, lastIndexOf); + try { + cfg.setDirectoryForTemplateLoading(new File(absoluteClasspath+templatesDirUrl)); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 根据freemarker模板在当前项目的classpath路径下生成.xml文件 + * @param templateName + * @param data + * @param targetFileName + * @return + */ + public static boolean createFile(String templateName, Map data, String targetFileName) { + Template template = null; + Writer out = null; + //1.获取当前项目classpath路径 + String classPath = FreemarkerUtil.class.getResource("/").getPath(); + int lastIndexOf = classPath.lastIndexOf("target"); + String projectDir = classPath.substring(0, lastIndexOf); + String resourcesDir = projectDir+"src/main/resources"; + File file = new File(resourcesDir); + if (!file.exists()){ + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + String targetFileUrl = resourcesDir + "/" + targetFileName; + //2.如果目标文件存在就先删除目标文件 + File targetFile = new File(targetFileUrl); + if (targetFile.exists()){ + targetFile.delete(); + } + try { + //3.获取模板文件对象 + template = cfg.getTemplate(templateName); + //4.获取输出流对象 + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileUrl))); + //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; + } + + public static void main(String[] args) { + //1.获取类的加载根目录,这个路径会是这个方法在哪个项目中被调用,就是哪个项目的类加载根路径 + String path = FreemarkerUtil.class.getResource("/").getPath(); + String parent = new File(path).getParent(); + String parent1 = new File(parent).getParent(); + System.out.println(path); + System.out.println(parent1); + //2.获取当前类的加载目录,这个目录会是确定的是这个类真实在哪,这个相对路径就是哪里,这个类在jar包里,路径就是这个类在jar包中的路径 + String path1 = FreemarkerUtil.class.getResource("").getPath(); + System.out.println(path1); + //3.获取当前项目的路径 + File file = new File(""); + try { + String path2 = file.getCanonicalPath(); + System.out.println(path2); + } catch (IOException e) { + e.printStackTrace(); + } + //4.获取当前工程路径 + System.out.println(System.getProperty("user.dir")); + //5.获取所有的类的jar的加载路径 + System.out.println(System.getProperty("java.class.path")); + } +} diff --git a/boot-starter/boot-starter-util/src/main/resources/templates/generatorConfig.fm b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm similarity index 100% rename from boot-starter/boot-starter-util/src/main/resources/templates/generatorConfig.fm rename to boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/generatorConfig.fm diff --git a/boot-starter/boot-starter-util/src/main/resources/templates/mybatisGenerator.fm b/boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/mybatisGenerator.fm similarity index 100% rename from boot-starter/boot-starter-util/src/main/resources/templates/mybatisGenerator.fm rename to boot-starter/boot-starter-maven-plugin/mybatis-generator-extend/src/main/resources/templates/mybatisGenerator.fm diff --git a/boot-starter/boot-starter-maven-plugin/pom.xml b/boot-starter/boot-starter-maven-plugin/pom.xml new file mode 100644 index 0000000..ad239b5 --- /dev/null +++ b/boot-starter/boot-starter-maven-plugin/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + boot-starter-maven-plugin + pom + + boot-starter-maven-plugin + boot-starter-maven-plugin + + + com.micro.fast + boot-starter + 0.0.1-SNAPSHOT + + + + mybatis-generator-extend + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + boot-starter-maven-plugin + + \ No newline at end of file diff --git a/boot-starter/boot-starter-util/pom.xml b/boot-starter/boot-starter-util/pom.xml index bc555d9..e4209eb 100644 --- a/boot-starter/boot-starter-util/pom.xml +++ b/boot-starter/boot-starter-util/pom.xml @@ -47,10 +47,6 @@ commons-io commons-io - - org.freemarker - freemarker - junit junit 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 deleted file mode 100644 index 594690f..0000000 --- a/boot-starter/boot-starter-util/src/main/java/com/micro/fast/util/FreemarkerUtil.java +++ /dev/null @@ -1,89 +0,0 @@ -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/test/java/com/micro/fast/util/FreemarkerUtilTest.java b/boot-starter/boot-starter-util/src/test/java/com/micro/fast/util/FreemarkerUtilTest.java deleted file mode 100644 index 1ccd265..0000000 --- a/boot-starter/boot-starter-util/src/test/java/com/micro/fast/util/FreemarkerUtilTest.java +++ /dev/null @@ -1,15 +0,0 @@ -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 diff --git a/boot-starter/pom.xml b/boot-starter/pom.xml index 34cb397..143d39a 100644 --- a/boot-starter/pom.xml +++ b/boot-starter/pom.xml @@ -17,6 +17,7 @@ boot-starter-util + boot-starter-maven-plugin boot-starter-fastdfs-client boot-starter-elasticsearch boot-starter-security diff --git a/pom.xml b/pom.xml index 7a28ba9..751f350 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ boot-starter ms + pa