Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/asciidoc/user-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ String sql = sqlGenerator.generate(

==== Receiving custom bind variables

You can receiving custom bind variables that created during template processing via user define `Map` reference as follow:
You can receiving custom bind variables that created during template processing via a `BiConsumer` function as follow:

[NOTE]
====
Expand All @@ -1129,16 +1129,14 @@ Map<String, Object> conditionsMap = new HashMap<>();
conditionsMap.put("name", "Yamada");

// sql = "SELECT * FROM accounts WHERE name = #{patternName}"
// customBindVariablesStore = {"patternName":"Yamada%"}
Map<String, Object> customBindVariablesStore = new HashMap<>(); // <1>
// conditionsMap = {"name":"Yamada", "patternName":"Yamada%"}
String sql = sqlGenerator.generate(
"/*[# mb:bind='patternName=|${#likes.escapeWildcard(name)}%|' /]*/" +
"SELECT * FROM accounts WHERE name = /*[# mb:p='patternName']*/ 'Sato' /*[/]*/",
conditionsMap, null, customBindVariablesStore); // <2>
conditionsMap, null, conditionsMap::put); // <1>
----

<1> Define a `Map` reference for receiving custom bind variables
<2> Specify(Pass) a `Map` reference for receiving custom bind variables at 4th argument of `generate` method
<1> Specify(Pass) a `BiConsumer` reference for receiving custom bind variables at 4th argument of `generate` method

=== Advanced Usage

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/mybatis/scripting/thymeleaf/SqlGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -199,12 +200,12 @@ public String generate(CharSequence sqlTemplate, Object parameter, Map<String, O
* a parameter object
* @param customVariable
* a custom variables for passing to template engine
* @param customBindVariableStore
* a store for saving a custom bind variable that generated with {@code mb:bind} or {@code mb:param}
* @param customBindVariableBinder
* a binder for a custom bind variable that generated with {@code mb:bind} or {@code mb:param}
* @return a processed SQL by template engine
*/
public String generate(CharSequence sqlTemplate, Object parameter, Map<String, Object> customVariable,
Map<String, Object> customBindVariableStore) {
BiConsumer<String, Object> customBindVariableBinder) {

Map<String, Object> processingCustomVariables = new HashMap<>(defaultCustomVariables);
Optional.ofNullable(customVariable).ifPresent(processingCustomVariables::putAll);
Expand All @@ -213,8 +214,8 @@ public String generate(CharSequence sqlTemplate, Object parameter, Map<String, O
String sql = templateEngine.process(sqlTemplate.toString(), context);

MyBatisBindingContext bindingContext = MyBatisBindingContext.load(context);
if (bindingContext != null && customBindVariableStore != null) {
customBindVariableStore.putAll(bindingContext.getCustomBindVariables());
if (bindingContext != null && customBindVariableBinder != null) {
bindingContext.getCustomBindVariables().forEach(customBindVariableBinder);
}

return sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -95,10 +94,8 @@ public BoundSql getBoundSql(Object parameterObject) {
customVariables.put(TemporaryTakeoverKeys.CONFIGURATION, configuration);
customVariables.put(TemporaryTakeoverKeys.DYNAMIC_CONTEXT, dynamicContext);
customVariables.put(TemporaryTakeoverKeys.PROCESSING_PARAMETER_TYPE, processingParameterType);
Map<String, Object> customBindVariableStore = new HashMap<>();
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, customVariables, customBindVariableStore);
String sql = sqlGenerator.generate(sqlTemplate, parameterObject, customVariables, dynamicContext::bind);

customBindVariableStore.forEach(dynamicContext::bind);
SqlSource sqlSource = sqlSourceBuilder.parse(sql, processingParameterType, dynamicContext.getBindings());
BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
dynamicContext.getBindings().forEach(boundSql::setAdditionalParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,17 @@ void processWithCustomBindVariablesStore() {
// @formatter: on
{
Map<String, Object> param = Collections.singletonMap("name", "Be%");
Map<String, Object> store = new HashMap<>();
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(param);

String sql = sqlGenerator.generate(sqlTemplate, param, null, store);
String sql = sqlGenerator.generate(sqlTemplate, param, null, mapSqlParameterSource::addValue);

Map<String, Object> record = jdbcOperations.queryForMap(sql, new MapSqlParameterSource(param).addValues(store));
Map<String, Object> record = jdbcOperations.queryForMap(sql, mapSqlParameterSource);

Assertions.assertEquals(6, record.get("ID"));
Assertions.assertEquals("Be%ty", record.get("FIRSTNAME"));
Assertions.assertEquals("Ab_le", record.get("LASTNAME"));

Assertions.assertEquals(1, store.size());
Assertions.assertEquals("Be\\%%", store.get("patternName"));
Assertions.assertEquals("Be\\%%", mapSqlParameterSource.getValue("patternName"));
}
}

Expand Down