1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.thymeleaf;
17
18 import java.lang.reflect.Proxy;
19 import java.util.Map;
20
21 import org.thymeleaf.IEngineConfiguration;
22 import org.thymeleaf.context.IContext;
23 import org.thymeleaf.context.IEngineContext;
24 import org.thymeleaf.context.IEngineContextFactory;
25 import org.thymeleaf.engine.TemplateData;
26
27
28
29
30
31
32
33
34 public class MyBatisIntegratingEngineContextFactory implements IEngineContextFactory {
35 private final IEngineContextFactory delegate;
36 private final ClassLoader classLoader = getClass().getClassLoader();
37
38
39
40
41
42
43
44 public MyBatisIntegratingEngineContextFactory(IEngineContextFactory delegate) {
45 this.delegate = delegate;
46 }
47
48
49
50
51 @Override
52 public IEngineContext createEngineContext(IEngineConfiguration configuration, TemplateData templateData,
53 Map<String, Object> templateResolutionAttributes, IContext context) {
54 IEngineContext engineContext = delegate.createEngineContext(configuration, templateData,
55 templateResolutionAttributes, context);
56 return (IEngineContext) Proxy.newProxyInstance(classLoader, new Class[] { IEngineContext.class },
57 (proxy, method, args) -> {
58 if (method.getName().equals("getVariable")) {
59 String name = (String) args[0];
60 Object value;
61 MyBatisBindingContext bindingContext = MyBatisBindingContext.load(engineContext);
62 if (bindingContext.isFallbackParameterObject()) {
63 value = engineContext.containsVariable(name) ? engineContext.getVariable(name)
64 : engineContext.getVariable(SqlGenerator.ContextKeys.PARAMETER_OBJECT);
65 } else {
66 value = engineContext.getVariable(name);
67 }
68 return value;
69 }
70 return method.invoke(engineContext, args);
71 });
72 }
73
74 }