Skip to content

Commit ed12d88

Browse files
committed
修改zheng-common代码生成工具类,自动检测模板路径,代替路径硬编码
1 parent 10efbea commit ed12d88

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

zheng-common/src/main/java/com/zheng/common/util/MybatisGeneratorUtil.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
public class MybatisGeneratorUtil {
2121

2222
// generatorConfig模板路径
23-
private static String generatorConfig_vm = "zheng-common/src/main/resources/template/generatorConfig.vm";
23+
private static String generatorConfig_vm = "/template/generatorConfig.vm";
2424
// Service模板路径
25-
private static String service_vm = "zheng-common/src/main/resources/template/Service.vm";
25+
private static String service_vm = "/template/Service.vm";
2626
// ServiceMock模板路径
27-
private static String serviceMock_vm = "zheng-common/src/main/resources/template/ServiceMock.vm";
27+
private static String serviceMock_vm = "/template/ServiceMock.vm";
2828
// ServiceImpl模板路径
29-
private static String serviceImpl_vm = "zheng-common/src/main/resources/template/ServiceImpl.vm";
29+
private static String serviceImpl_vm = "/template/ServiceImpl.vm";
3030

3131
/**
3232
* 根据模板生成generatorConfig.xml文件
@@ -50,6 +50,11 @@ public static void generator(
5050
String package_name,
5151
Map<String, String> last_insert_id_tables) throws Exception{
5252

53+
generatorConfig_vm = MybatisGeneratorUtil.class.getResource(generatorConfig_vm).getPath().replaceFirst("/", "");
54+
service_vm = MybatisGeneratorUtil.class.getResource(service_vm).getPath().replaceFirst("/", "");
55+
serviceMock_vm = MybatisGeneratorUtil.class.getResource(serviceMock_vm).getPath().replaceFirst("/", "");
56+
serviceImpl_vm = MybatisGeneratorUtil.class.getResource(serviceImpl_vm).getPath().replaceFirst("/", "");
57+
5358
String targetProject = module + "/" + module + "-dao";
5459
String module_path = module + "/" + module + "-dao/src/main/resources/generatorConfig.xml";
5560
String sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + database + "' AND table_name LIKE '" + table_prefix + "_%';";

zheng-common/src/main/java/com/zheng/common/util/VelocityUtil.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.zheng.common.util;
22

3+
import org.apache.commons.lang.StringUtils;
34
import org.apache.velocity.Template;
45
import org.apache.velocity.VelocityContext;
56
import org.apache.velocity.app.Velocity;
67
import org.apache.velocity.app.VelocityEngine;
78

89
import java.io.File;
910
import java.io.FileWriter;
11+
import java.util.Properties;
1012

1113
/**
1214
* Velocity工具类
@@ -23,9 +25,11 @@ public class VelocityUtil {
2325
*/
2426
public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
2527
try {
26-
Velocity.init();
27-
VelocityEngine engine = new VelocityEngine();
28-
Template template = engine.getTemplate(inputVmFilePath, "utf-8");
28+
Properties properties = new Properties();
29+
properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath));
30+
Velocity.init(properties);
31+
//VelocityEngine engine = new VelocityEngine();
32+
Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8");
2933
File outputFile = new File(outputFilePath);
3034
FileWriter writer = new FileWriter(outputFile);
3135
template.merge(context, writer);
@@ -35,4 +39,30 @@ public static void generate(String inputVmFilePath, String outputFilePath, Veloc
3539
}
3640
}
3741

42+
/**
43+
* 根据文件绝对路径获取目录
44+
* @param filePath
45+
* @return
46+
*/
47+
public static String getPath(String filePath) {
48+
String path = "";
49+
if (StringUtils.isNotBlank(filePath)) {
50+
path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
51+
}
52+
return path;
53+
}
54+
55+
/**
56+
* 根据文件绝对路径获取文件
57+
* @param filePath
58+
* @return
59+
*/
60+
public static String getFile(String filePath) {
61+
String file = "";
62+
if (StringUtils.isNotBlank(filePath)) {
63+
file = filePath.substring(filePath.lastIndexOf("/") + 1);
64+
}
65+
return file;
66+
}
67+
3868
}

0 commit comments

Comments
 (0)