Skip to content

Commit

Permalink
feat(generator): 代码生成新增 Mapper.xml 模板
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Dec 12, 2024
1 parent a7296a3 commit b519364
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public static class TemplateConfig {
*/
private String extension = FileNameUtil.EXT_JAVA;

/**
* 后缀
*/
private String suffix;

/**
* 是否为后端模板
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,12 @@ private List<GeneratePreviewResp> preview(String tableName) {
// 渲染代码
String classNamePrefix = innerGenConfig.getClassNamePrefix();
Map<String, GeneratorProperties.TemplateConfig> templateConfigMap = generatorProperties.getTemplateConfigs();

TemplateEngine engine = TemplateUtil
.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
if (engine instanceof FreemarkerEngine) {
((FreemarkerEngine)engine).getConfiguration()
if (engine instanceof FreemarkerEngine freemarkerEngine) {
freemarkerEngine.getConfiguration()
.setSharedVariable("statics", BeansWrapper.getDefaultInstance().getStaticModels());
}

for (Map.Entry<String, GeneratorProperties.TemplateConfig> templateConfigEntry : templateConfigMap.entrySet()) {
GeneratorProperties.TemplateConfig templateConfig = templateConfigEntry.getValue();
// 移除需要忽略的字段
Expand All @@ -309,7 +307,7 @@ private List<GeneratePreviewResp> preview(String tableName) {
// 处理其他配置
innerGenConfig.setSubPackageName(templateConfig.getPackageName());
String classNameSuffix = templateConfigEntry.getKey();
String className = classNamePrefix + classNameSuffix;
String className = classNamePrefix + StrUtil.blankToDefault(templateConfig.getSuffix(), classNameSuffix);
innerGenConfig.setClassName(className);
boolean isBackend = templateConfig.isBackend();
String extension = templateConfig.getExtension();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.mapper.${classNamePrefix}Mapper">
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ generator:
Mapper:
templatePath: backend/Mapper.ftl
packageName: mapper
MapperXml:
templatePath: backend/MapperXml.ftl
packageName: mapper
extension: .xml
suffix: Mapper
Service:
templatePath: backend/Service.ftl
packageName: service
Expand Down

0 comments on commit b519364

Please sign in to comment.