模板引擎支持字符串模板渲染输出字符串内容
This commit is contained in:
parent
530e23a6de
commit
0995140b94
@ -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;
|
||||
}
|
||||
}
|
@ -18,6 +18,8 @@ package com.baomidou.mybatisplus.generator.config.builder;
|
||||
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;
|
||||
|
||||
@ -87,6 +89,14 @@ public class ConfigBuilder {
|
||||
*/
|
||||
private final IDatabaseQuery databaseQuery;
|
||||
|
||||
/**
|
||||
* 资源加载器
|
||||
* @since 3.5.9
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private TemplateLoadWay templateLoadWay = TemplateLoadWay.FILE;
|
||||
|
||||
/**
|
||||
* 在构造器中处理配置
|
||||
*
|
||||
|
@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
||||
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;
|
||||
@ -47,6 +48,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class DbQueryDecorator extends AbstractDbQuery {
|
||||
private final IDbQuery dbQuery;
|
||||
@Getter
|
||||
private final Connection connection;
|
||||
private final DbType dbType;
|
||||
private final StrategyConfig strategyConfig;
|
||||
@ -190,7 +192,6 @@ public class DbQueryDecorator extends AbstractDbQuery {
|
||||
*
|
||||
* @param sql 执行SQL
|
||||
* @param consumer 结果处理
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void execute(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
|
||||
logger.debug("执行SQL:{}", sql);
|
||||
@ -207,10 +208,6 @@ public class DbQueryDecorator extends AbstractDbQuery {
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
Optional.ofNullable(connection).ifPresent((con) -> {
|
||||
try {
|
||||
@ -225,6 +222,7 @@ public class DbQueryDecorator extends AbstractDbQuery {
|
||||
|
||||
private final IDbQuery dbQuery;
|
||||
|
||||
@Getter
|
||||
private final ResultSet resultSet;
|
||||
|
||||
private final DbType dbType;
|
||||
@ -235,10 +233,6 @@ public class DbQueryDecorator extends AbstractDbQuery {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public ResultSet getResultSet() {
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public String getStringResult(String columnLabel) {
|
||||
try {
|
||||
return resultSet.getString(columnLabel);
|
||||
|
@ -22,8 +22,9 @@ import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库查询接口注册中心
|
||||
*
|
||||
* @author nieqiuqiu
|
||||
* @date 2020-01-09
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public class DbQueryRegistry {
|
||||
|
@ -17,7 +17,7 @@ package com.baomidou.mybatisplus.generator.config.querys;
|
||||
|
||||
|
||||
/**
|
||||
* https://github.com/baomidou/generator/pull/83
|
||||
* <a href="https://github.com/baomidou/generator/pull/83">Gbase</a>
|
||||
*
|
||||
* @author lix
|
||||
*/
|
||||
|
@ -18,12 +18,7 @@ package com.baomidou.mybatisplus.generator.engine;
|
||||
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;
|
||||
@ -259,6 +254,16 @@ public abstract class AbstractTemplateEngine {
|
||||
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;
|
||||
|
||||
/**
|
||||
* 将模板转化成为文件
|
||||
*
|
||||
|
@ -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);
|
||||
|
@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.generator.engine;
|
||||
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;
|
||||
@ -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);
|
||||
|
@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -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 {
|
||||
|
@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.generator.engine;
|
||||
|
||||
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;
|
||||
@ -24,10 +25,7 @@ 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;
|
||||
|
||||
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user