修改zheng-common代码生成工具类,自动检测模板路径,代替路径硬编码

This commit is contained in:
shuzheng 2017-05-03 17:08:05 +08:00
parent 10efbea2eb
commit ed12d88568
2 changed files with 42 additions and 7 deletions

View File

@ -20,13 +20,13 @@ import static com.zheng.common.util.StringUtil.lineToHump;
public class MybatisGeneratorUtil { public class MybatisGeneratorUtil {
// generatorConfig模板路径 // generatorConfig模板路径
private static String generatorConfig_vm = "zheng-common/src/main/resources/template/generatorConfig.vm"; private static String generatorConfig_vm = "/template/generatorConfig.vm";
// Service模板路径 // Service模板路径
private static String service_vm = "zheng-common/src/main/resources/template/Service.vm"; private static String service_vm = "/template/Service.vm";
// ServiceMock模板路径 // ServiceMock模板路径
private static String serviceMock_vm = "zheng-common/src/main/resources/template/ServiceMock.vm"; private static String serviceMock_vm = "/template/ServiceMock.vm";
// ServiceImpl模板路径 // ServiceImpl模板路径
private static String serviceImpl_vm = "zheng-common/src/main/resources/template/ServiceImpl.vm"; private static String serviceImpl_vm = "/template/ServiceImpl.vm";
/** /**
* 根据模板生成generatorConfig.xml文件 * 根据模板生成generatorConfig.xml文件
@ -50,6 +50,11 @@ public class MybatisGeneratorUtil {
String package_name, String package_name,
Map<String, String> last_insert_id_tables) throws Exception{ Map<String, String> last_insert_id_tables) throws Exception{
generatorConfig_vm = MybatisGeneratorUtil.class.getResource(generatorConfig_vm).getPath().replaceFirst("/", "");
service_vm = MybatisGeneratorUtil.class.getResource(service_vm).getPath().replaceFirst("/", "");
serviceMock_vm = MybatisGeneratorUtil.class.getResource(serviceMock_vm).getPath().replaceFirst("/", "");
serviceImpl_vm = MybatisGeneratorUtil.class.getResource(serviceImpl_vm).getPath().replaceFirst("/", "");
String targetProject = module + "/" + module + "-dao"; String targetProject = module + "/" + module + "-dao";
String module_path = module + "/" + module + "-dao/src/main/resources/generatorConfig.xml"; String module_path = module + "/" + module + "-dao/src/main/resources/generatorConfig.xml";
String sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database + "' AND table_name LIKE '" + table_prefix + "_%';"; String sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database + "' AND table_name LIKE '" + table_prefix + "_%';";

View File

@ -1,5 +1,6 @@
package com.zheng.common.util; package com.zheng.common.util;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity; import org.apache.velocity.app.Velocity;
@ -7,6 +8,7 @@ import org.apache.velocity.app.VelocityEngine;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.util.Properties;
/** /**
* Velocity工具类 * Velocity工具类
@ -23,9 +25,11 @@ public class VelocityUtil {
*/ */
public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception { public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
try { try {
Velocity.init(); Properties properties = new Properties();
VelocityEngine engine = new VelocityEngine(); properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath));
Template template = engine.getTemplate(inputVmFilePath, "utf-8"); Velocity.init(properties);
//VelocityEngine engine = new VelocityEngine();
Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8");
File outputFile = new File(outputFilePath); File outputFile = new File(outputFilePath);
FileWriter writer = new FileWriter(outputFile); FileWriter writer = new FileWriter(outputFile);
template.merge(context, writer); template.merge(context, writer);
@ -35,4 +39,30 @@ public class VelocityUtil {
} }
} }
/**
* 根据文件绝对路径获取目录
* @param filePath
* @return
*/
public static String getPath(String filePath) {
String path = "";
if (StringUtils.isNotBlank(filePath)) {
path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
}
return path;
}
/**
* 根据文件绝对路径获取文件
* @param filePath
* @return
*/
public static String getFile(String filePath) {
String file = "";
if (StringUtils.isNotBlank(filePath)) {
file = filePath.substring(filePath.lastIndexOf("/") + 1);
}
return file;
}
} }