View Javadoc
1   /*
2    *    Copyright 2012-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.scripting.velocity;
17  
18  import org.apache.ibatis.executor.parameter.ParameterHandler;
19  import org.apache.ibatis.mapping.BoundSql;
20  import org.apache.ibatis.mapping.MappedStatement;
21  import org.apache.ibatis.mapping.SqlSource;
22  import org.apache.ibatis.parsing.XNode;
23  import org.apache.ibatis.scripting.LanguageDriver;
24  import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
25  import org.apache.ibatis.session.Configuration;
26  
27  /**
28   * The {@link LanguageDriver} using Velocity.
29   * <p>
30   * This class rename from {@code Driver}.
31   * </p>
32   *
33   * @since 2.1.0
34   *
35   * @author Kazuki Shimizu
36   */
37  public class VelocityLanguageDriver implements LanguageDriver {
38  
39    /**
40     * Default constructor.
41     */
42    public VelocityLanguageDriver() {
43      this(VelocityLanguageDriverConfig.newInstance());
44    }
45  
46    /**
47     * Constructor.
48     *
49     * @param driverConfig
50     *          a language driver configuration
51     */
52    public VelocityLanguageDriver(VelocityLanguageDriverConfig driverConfig) {
53      VelocityFacade.initialize(driverConfig);
54    }
55  
56    /**
57     * {@inheritDoc}
58     */
59    @Override
60    public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
61        BoundSql boundSql) {
62      return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
63    }
64  
65    /**
66     * {@inheritDoc}
67     */
68    @Override
69    public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterTypeClass) {
70      return new SQLScriptSource(configuration, script.getNode().getTextContent(),
71          parameterTypeClass == null ? Object.class : parameterTypeClass);
72    }
73  
74    /**
75     * {@inheritDoc}
76     */
77    @Override
78    public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterTypeClass) {
79      return new SQLScriptSource(configuration, script, parameterTypeClass == null ? Object.class : parameterTypeClass);
80    }
81  
82  }