添加mybatis-generate-extend插件
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>mybatis-generator-extend-maven-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>mybatis-generator-extend</name>
|
||||
<description>mybatis-generator-extend-maven-plugin</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.micro.fast</groupId>
|
||||
<artifactId>boot-starter</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.micro.fast</groupId>
|
||||
<artifactId>boot-starter-util</artifactId>
|
||||
<version>${micro.fast.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>mybatis-generator-extend-maven-plugin</finalName>
|
||||
</build>
|
||||
</project>
|
||||
+135
@@ -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<String, Object> 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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>boot-starter-maven-plugin</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>boot-starter-maven-plugin</name>
|
||||
<description>boot-starter-maven-plugin</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.micro.fast</groupId>
|
||||
<artifactId>boot-starter</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>mybatis-generator-extend</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>boot-starter-maven-plugin</finalName>
|
||||
</build>
|
||||
</project>
|
||||
@@ -47,10 +47,6 @@
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
||||
@@ -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<String, Object> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
-15
@@ -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<String, Object> model = new HashMap<>();
|
||||
model.put("pojoPackage","com.lishouyu.com.pojo");
|
||||
FreemarkerUtil.createFile("generatorConfig.fm",model,"a.xml");
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
<modules>
|
||||
<module>boot-starter-util</module>
|
||||
<module>boot-starter-maven-plugin</module>
|
||||
<module>boot-starter-fastdfs-client</module>
|
||||
<module>boot-starter-elasticsearch</module>
|
||||
<module>boot-starter-security</module>
|
||||
|
||||
Reference in New Issue
Block a user