Skip to content

Commit

Permalink
修改内置模板的加载路径和加载方式
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfantasy committed Jul 31, 2020
1 parent 1fac97b commit bd7d36c
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.davidfantasy</groupId>
<artifactId>mybatis-plus-generator-ui</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<modelVersion>4.0.0</modelVersion>

<name>mybatis-plus-generator-ui</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public String getAvailableTemplatePath() {
return templatePath;
}
if (fileType.equals(FILE_TYPE_ENTITY)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/entity.java.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/entity.java.btl";
} else if (fileType.equals(FILE_TYPE_MAPPER)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/mapper.java.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/mapper.java.btl";
} else if (fileType.equals(FILE_TYPE_MAPPER_XML)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/mapper.xml.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/mapper.xml.btl";
} else if (fileType.equals(FILE_TYPE_SERVICE)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/service.java.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/service.java.btl";
} else if (fileType.equals(FILE_TYPE_SERVICEIMPL)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/serviceImpl.java.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/serviceImpl.java.btl";
} else if (fileType.equals(FILE_TYPE_CONTROLLER)) {
return RESOURCE_PREFIX_CLASSPATH + "/templates/controller.java.btl";
return RESOURCE_PREFIX_CLASSPATH + "codetpls/controller.java.btl";
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BeetlTemplateEngine(String templateStoreDir) {
this.templateStoreDir = templateStoreDir;
try {
logger.info("模板根目录为:" + templateStoreDir);
ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader(this.getClass().getClassLoader());
FileResourceLoader fileResourceLoader = new FileResourceLoader(templateStoreDir);
CompositeResourceLoader loader = new CompositeResourceLoader();
loader.addResourceLoader(new StartsWithMatcher(RESOURCE_PREFIX_CLASSPATH).withoutPrefix(), classpathResourceLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void genDtoFileFromSQL(String sql, GenDtoConfig config) throws Exception
file.getParentFile().mkdirs();
file.createNewFile();
}
beetlTemplateEngine.writer(tplParams, "classpath:templates/dto.btl", outputPath);
beetlTemplateEngine.writer(tplParams, "classpath:codetpls/dto.btl", outputPath);
log.info("DTO已成功生成,输出位置为:" + outputPath);

}
Expand All @@ -139,7 +139,7 @@ public void genMapperElementsFromSql(String sql, GenDtoConfig config, boolean ge
public MapperElement createResultMapElement(GenDtoConfig config) {
Map<String, Object> tplParams = Maps.newHashMap();
tplParams.put("config", config);
String resultMapStr = beetlTemplateEngine.write2String(tplParams, "classpath:templates/resultMap.btl");
String resultMapStr = beetlTemplateEngine.write2String(tplParams, "classpath:codetpls/resultMap.btl");
MapperElement resultMapEle = MapperElement.builder().id(config.getDtoName() + "Map")
.comment("Author:" + config.getAuthor() + ",Date:" + DateUtil.format(new Date(), "yyyy-MM-dd") + "," + config.getMapperElementId() + "的结果映射配置,由mybatis-plus-generator-ui自动生成")
.content(resultMapStr)
Expand All @@ -156,7 +156,7 @@ public MapperElement createMapperMethodElement(String sql, GenDtoConfig config)
sql = dynamicParamSqlEnhancer.enhanceDynamicConditions(sql);
}
tplParams.put("sql", sql);
String methodEleStr = beetlTemplateEngine.write2String(tplParams, "classpath:templates/mapperMethods.btl");
String methodEleStr = beetlTemplateEngine.write2String(tplParams, "classpath:codetpls/mapperMethods.btl");
MapperElement methodEle = MapperElement.builder().id(config.getMapperElementId())
.comment("Author:" + config.getAuthor() + ",Date:" + DateUtil.format(new Date(), "yyyy-MM-dd") + ",由mybatis-plus-generator-ui自动生成")
.content(methodEleStr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.davidfantasy.mybatisplus.generatorui.service;

import cn.hutool.core.io.resource.ResourceUtil;
import org.springframework.stereotype.Service;

import java.io.InputStream;
Expand All @@ -14,7 +13,7 @@ public class TemplateService {
public InputStream getBuiltInTemplate(String fileType) {
//原来是直接读取mybatis-plus-generator中的模板,现在改为读取项目资源目录下的模板
//InputStream templateIn = AutoGenerator.class.getResourceAsStream("/templates/" + fileType2TemplateName(fileType));
InputStream templateIn = ResourceUtil.getStream("/templates/" + fileType2TemplateName(fileType));
InputStream templateIn = this.getClass().getResourceAsStream("/codetpls/" + fileType2TemplateName(fileType));
return templateIn;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit bd7d36c

Please sign in to comment.