添加根据freemarker模板生成文件工具类

This commit is contained in:
lsy
2017-11-26 21:26:39 +08:00
parent 8ce791c0ef
commit 96216234ec
5 changed files with 192 additions and 1 deletions
+4
View File
@@ -47,6 +47,10 @@
<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>
@@ -8,7 +8,7 @@ import java.io.File;
* @version 1.0
* @since 1.0
*/
public class FileUtils {
public class FileUtil {
/**
* 修改文件夹下的所有符合条件的文件的扩展名,递归文件夹下所有文件
* @param dir 文件夹的路径
@@ -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<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;
}
}
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--导入属性配置-->
<properties resource="mybatisGenerator.properties"></properties>
<!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry location=""/>
<context id="default" targetRuntime="MyBatis3" defaultModelType="flat">
<!--java文件的编码设置-->
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 由于beginningDelimiter和endingDelimiter的默认值为双引号("),在Mysql中不能这么写,所以还要将这两个默认值改为` -->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 为生成的Java模型创建一个toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<!-- 对生成的pojo集成serializable接口-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" ></plugin>
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=""
connectionURL=""
userId=""
password="">
</jdbcConnection>
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="${pojoPackage}" targetProject="./src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<!-- targetPackage:mapper接口dao生成的位置 -->
<!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.micro.fast.upms.dao" targetProject="./src/main/java" >
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table tableName="upms_log" domainObjectName="UpmsLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_organization" domainObjectName="UpmsOrganization" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_permission" domainObjectName="UpmsPermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_role" domainObjectName="UpmsRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_role_permission" domainObjectName="UpmsRolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="system" domainObjectName="System" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_user" domainObjectName="UpmsUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_user_organization" domainObjectName="UpmsUserOrganization" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_user_permission" domainObjectName="UpmsUserPermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="upms_user_role" domainObjectName="UpmsUserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
@@ -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<String, Object> model = new HashMap<>();
model.put("pojoPackage","com.lishouyu.com.pojo");
FreemarkerUtil.createFile("generatorConfig.fm",model,"a.xml");
}
}