org.mybatis.scripting.thymeleaf.ThymeleafSqlSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-thymeleaf Show documentation
Show all versions of mybatis-thymeleaf Show documentation
Thymeleaf support for MyBatis
The newest version!
/*
* Copyright 2018-2022 the original author or authors.
*
* 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
*
* https://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 org.mybatis.scripting.thymeleaf;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiFunction;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.scripting.xmltags.DynamicContext;
import org.apache.ibatis.session.Configuration;
import org.thymeleaf.context.IContext;
/**
* The {@code SqlSource} for integrating with Thymeleaf.
*
* @author Kazuki Shimizu
*
* @version 1.0.0
*
* @see ThymeleafLanguageDriver
*/
class ThymeleafSqlSource implements SqlSource {
private static class TemporaryTakeoverKeys {
private static final String CONFIGURATION = "__configuration__";
private static final String DYNAMIC_CONTEXT = "__dynamicContext__";
private static final String PROCESSING_PARAMETER_TYPE = "__processingParameterType__";
}
private final Configuration configuration;
private final SqlGenerator sqlGenerator;
private final SqlSourceBuilder sqlSourceBuilder;
private final String sqlTemplate;
private final Class> parameterType;
/**
* Constructor for for integrating with template engine provide by Thymeleaf.
*
* @param configuration
* A configuration instance of MyBatis
* @param sqlGenerator
* A sql generator using the Thymeleaf feature
* @param sqlTemplate
* A template string of SQL (inline SQL or template file path)
* @param parameterType
* A parameter type that specified at mapper method argument or xml element
*/
ThymeleafSqlSource(Configuration configuration, SqlGenerator sqlGenerator, String sqlTemplate,
Class> parameterType) {
this.configuration = configuration;
this.sqlGenerator = sqlGenerator;
this.sqlTemplate = sqlTemplate;
this.parameterType = parameterType;
this.sqlSourceBuilder = new SqlSourceBuilder(configuration);
}
/**
* {@inheritDoc}
*/
@Override
public BoundSql getBoundSql(Object parameterObject) {
Class> processingParameterType;
if (parameterType == null) {
processingParameterType = parameterObject == null ? Object.class : parameterObject.getClass();
} else {
processingParameterType = parameterType;
}
DynamicContext dynamicContext = new DynamicContext(configuration, parameterObject);
Map customVariables = dynamicContext.getBindings();
customVariables.put(TemporaryTakeoverKeys.CONFIGURATION, configuration);
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, dynamicContext);
customVariables.put(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE, processingParameterType);
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, dynamicContext::bind, customVariables);
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, dynamicContext.getBindings());
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
dynamicContext.getBindings().forEach(boundSql::setAdditionalParameter);
return boundSql;
}
/**
* The factory class for Thymeleaf's context.
*
* @since 1.0.2
*/
static class ContextFactory implements BiFunction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy