diff --git a/continew-plugin/continew-plugin-generator/src/main/java/top/continew/admin/generator/service/impl/GeneratorServiceImpl.java b/continew-plugin/continew-plugin-generator/src/main/java/top/continew/admin/generator/service/impl/GeneratorServiceImpl.java index 2ebd061e..b48b64b7 100644 --- a/continew-plugin/continew-plugin-generator/src/main/java/top/continew/admin/generator/service/impl/GeneratorServiceImpl.java +++ b/continew-plugin/continew-plugin-generator/src/main/java/top/continew/admin/generator/service/impl/GeneratorServiceImpl.java @@ -28,8 +28,12 @@ import cn.hutool.extra.template.TemplateConfig; import cn.hutool.extra.template.TemplateEngine; import cn.hutool.extra.template.TemplateUtil; +import cn.hutool.extra.template.engine.freemarker.FreemarkerEngine; import cn.hutool.system.SystemUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.DefaultObjectWrapperBuilder; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -289,6 +293,11 @@ private List preview(String tableName) { Map templateConfigMap = generatorProperties.getTemplateConfigs(); TemplateEngine engine = TemplateUtil .createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH)); + // 在模板中允许使用静态方法 + if (engine instanceof FreemarkerEngine freemarkerEngine) { + DefaultObjectWrapper wrapper = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_33).build(); + freemarkerEngine.getConfiguration().setSharedVariable("statics", wrapper.getStaticModels()); + } for (Map.Entry templateConfigEntry : templateConfigMap.entrySet()) { GeneratorProperties.TemplateConfig templateConfig = templateConfigEntry.getValue(); // 移除需要忽略的字段