View Javadoc
1   /*
2    *    Copyright 2015-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.freemarker;
17  
18  import java.util.List;
19  
20  import org.apache.ibatis.annotations.Lang;
21  import org.apache.ibatis.annotations.Param;
22  import org.apache.ibatis.annotations.Select;
23  
24  /**
25   * Annotations-driven mapper for {@link org.mybatis.scripting.freemarker.FreeMarkerInAnnotationsTest}.
26   *
27   * @author elwood
28   */
29  public interface NameMapper {
30    /**
31     * Simple query that is loaded from template.
32     */
33    @Lang(FreeMarkerLanguageDriver.class)
34    @Select("getAllNames.ftl")
35    List<Name> getAllNames();
36  
37    /**
38     * Simple query with prepared statement parameter.
39     */
40    @Lang(FreeMarkerLanguageDriver.class)
41    @Select("findName.ftl")
42    Name findName(@Param("name") String name);
43  
44    /**
45     * If any whitespace found inside @Select text, it is interpreted as inline script, not template name. It is
46     * convenient to avoid creating templates when script is really small.
47     */
48    @Lang(FreeMarkerLanguageDriver.class)
49    @Select("select * from names where id in (${ids?join(',')})")
50    List<Name> findNamesByIds(@Param("ids") List<Integer> ids);
51  
52    /**
53     * There are no @Param annotation on argument. This means NameParam instance will be passed into driver as is, not as
54     * Map entry. So, we need to support this case. Because in driver we need to add some another properties into template
55     * model, and NameParam is not Map, we are need to wrap passed parameter object into
56     * {@link org.mybatis.scripting.freemarker.ParamObjectAdapter} before processing template.
57     */
58    @Lang(FreeMarkerLanguageDriver.class)
59    @Select("select * from names where id = <@p name='id'/> and id = ${id}")
60    Name find(NameParam nameParam);
61  
62    /**
63     * This query is to demonstrate MyBatis odd behaviour when using String as parameter and can use properties that not
64     * exist. Both props will be use provided `name` parameter value. Goal is to write FreeMarker lang plugin to support
65     * this behaviour too (although it is confusing one).
66     */
67    @Select("select * from names" + " where firstName = #{noSuchPropertyOnString}"
68        + " or firstName = #{oneMoreUnexistingProperty}")
69    List<Name> getNamesOddBehaviourStdLang(String name);
70  
71    /**
72     * This query is to demonstrate that FreeMarker does not break the compatibility with this behaviour.
73     */
74    @Lang(FreeMarkerLanguageDriver.class)
75    @Select("select * from names" + " where firstName = <@p name='noSuchPropertyOnString'/>"
76        + " or firstName = <@p name='oneMoreUnexistingProperty'/>")
77    List<Name> getNamesOddBehaviourFreeMarkerLang(String name);
78  }