Skip to content

Commit

Permalink
模板引擎支持字符串模板渲染输出字符串内容
Browse files Browse the repository at this point in the history
  • Loading branch information
qmdx committed Sep 21, 2024
1 parent 530e23a commit 0995140
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2011-2024, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.generator.config;

import lombok.Getter;

/**
* 模板文件加载方式
*
* @author hubin
* @since 3.5.9
*/
@Getter
public enum TemplateLoadWay {
STRING("string"),
FILE("file");

private final String value;

TemplateLoadWay(String value) {
this.value = value;
}

public boolean isFile() {
return FILE == this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.query.IDatabaseQuery;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -87,6 +89,14 @@ public class ConfigBuilder {
*/
private final IDatabaseQuery databaseQuery;

/**
* 资源加载器
* @since 3.5.9
*/
@Getter
@Setter
private TemplateLoadWay templateLoadWay = TemplateLoadWay.FILE;

/**
* 在构造器中处理配置
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.baomidou.mybatisplus.generator.config.IDbQuery;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,6 +48,7 @@
*/
public class DbQueryDecorator extends AbstractDbQuery {
private final IDbQuery dbQuery;
@Getter
private final Connection connection;
private final DbType dbType;
private final StrategyConfig strategyConfig;
Expand Down Expand Up @@ -190,7 +192,6 @@ public Map<String, Object> getCustomFields(ResultSet resultSet) {
*
* @param sql 执行SQL
* @param consumer 结果处理
* @throws SQLException
*/
public void execute(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
logger.debug("执行SQL:{}", sql);
Expand All @@ -207,10 +208,6 @@ public void execute(String sql, Consumer<ResultSetWrapper> consumer) throws SQLE
}
}

public Connection getConnection() {
return connection;
}

public void closeConnection() {
Optional.ofNullable(connection).ifPresent((con) -> {
try {
Expand All @@ -225,6 +222,7 @@ public static class ResultSetWrapper {

private final IDbQuery dbQuery;

@Getter
private final ResultSet resultSet;

private final DbType dbType;
Expand All @@ -235,10 +233,6 @@ public static class ResultSetWrapper {
this.dbType = dbType;
}

public ResultSet getResultSet() {
return resultSet;
}

public String getStringResult(String columnLabel) {
try {
return resultSet.getString(columnLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import java.util.Map;

/**
* 数据库查询接口注册中心
*
* @author nieqiuqiu
* @date 2020-01-09
* @since 3.3.1
*/
public class DbQueryRegistry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


/**
* https://github.com/baomidou/generator/pull/83
* <a href="https://github.com/baomidou/generator/pull/83">Gbase</a>
*
* @author lix
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.builder.Controller;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Mapper;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.builder.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.util.FileUtils;
import com.baomidou.mybatisplus.generator.util.RuntimeUtils;
Expand Down Expand Up @@ -259,6 +254,16 @@ public AbstractTemplateEngine batchOutput() {
return this;
}

/**
* 将模板转化成为字符串
*
* @param objectMap 渲染对象 MAP 信息
* @param templateName 模板名称
* @param templateString 模板字符串
* @since 3.5.0
*/
public abstract String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception;

/**
* 将模板转化成为文件
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public class BeetlTemplateEngine extends AbstractTemplateEngine {
return this;
}

@Override
public String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception {
Template template = groupTemplate.getTemplate(templateString);
template.binding(objectMap);
return template.render();
}

@Override
public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
Template template = (Template) method.invoke(groupTemplate, templatePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedWriter;
Expand All @@ -38,12 +39,17 @@ public class EnjoyTemplateEngine extends AbstractTemplateEngine {

@Override
public @NotNull AbstractTemplateEngine init(@NotNull ConfigBuilder configBuilder) {
engine = Engine.createIfAbsent("mybatis-plus-generator", e -> {
e.setToClassPathSourceFactory();
});
engine = Engine.createIfAbsent("mybatis-plus-generator",
Engine::setToClassPathSourceFactory);
return this;
}

@Override
public String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception {
Template template = engine.getTemplate(templateString);
return template.renderToString(objectMap);
}

@Override
public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
String str = engine.getTemplate(templatePath).renderToString(objectMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Map;

/**
Expand All @@ -44,6 +45,13 @@ public class FreemarkerTemplateEngine extends AbstractTemplateEngine {
return this;
}

@Override
public String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception {
Template template = new Template(templateName, templateString, configuration);
StringWriter writer = new StringWriter();
template.process(objectMap, writer);
return writer.toString();
}

@Override
public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.TemplateLoadWay;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.*;
import java.util.Map;
import java.util.Properties;

Expand All @@ -53,16 +51,28 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
public @NotNull VelocityTemplateEngine init(@NotNull ConfigBuilder configBuilder) {
if (null == velocityEngine) {
Properties p = new Properties();
p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE);
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY);
p.setProperty(Velocity.ENCODING_DEFAULT, ConstVal.UTF8);
p.setProperty(Velocity.INPUT_ENCODING, ConstVal.UTF8);
p.setProperty("file.resource.loader.unicode", StringPool.TRUE);
if (configBuilder.getTemplateLoadWay().isFile()) {
// 文件模板
p.setProperty(ConstVal.VM_LOAD_PATH_KEY, ConstVal.VM_LOAD_PATH_VALUE);
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, StringPool.EMPTY);
p.setProperty("file.resource.loader.unicode", StringPool.TRUE);
} else {
// 文本模板
p.setProperty(Velocity.RESOURCE_LOADER, TemplateLoadWay.STRING.getValue());
}
velocityEngine = new VelocityEngine(p);
}
return this;
}

@Override
public String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception {
StringWriter writer = new StringWriter();
velocityEngine.evaluate(new VelocityContext(objectMap), writer, templateName, templateString);
return writer.toString();
}

@Override
public void writer(@NotNull Map<String, Object> objectMap, @NotNull String templatePath, @NotNull File outputFile) throws Exception {
Expand Down

0 comments on commit 0995140

Please sign in to comment.