View Javadoc
1   /*
2    *    Copyright 2009-2023 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.apache.ibatis.submitted.param_name_resolve;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  import java.io.Reader;
21  import java.sql.Connection;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.ibatis.annotations.Select;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.jdbc.ScriptRunner;
28  import org.apache.ibatis.session.RowBounds;
29  import org.apache.ibatis.session.SqlSession;
30  import org.apache.ibatis.session.SqlSessionFactory;
31  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  class ActualParamNameTest {
36  
37    private static SqlSessionFactory sqlSessionFactory;
38  
39    @BeforeAll
40    static void setUp() throws Exception {
41      // create an SqlSessionFactory
42      try (Reader reader = Resources
43          .getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml")) {
44        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45        sqlSessionFactory.getConfiguration().addMapper(Mapper.class);
46      }
47  
48      // populate in-memory database
49      try (Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
50          Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql")) {
51        ScriptRunner runner = new ScriptRunner(conn);
52        runner.setLogWriter(null);
53        runner.runScript(reader);
54      }
55    }
56  
57    @Test
58    void testSingleListParameterWhenUseActualParamNameIsTrue() {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        Mapper mapper = sqlSession.getMapper(Mapper.class);
61        // use actual name
62        {
63          long count = mapper.getUserCountUsingList(Arrays.asList(1, 2));
64          assertEquals(2, count);
65        }
66        // use 'collection' as alias
67        {
68          long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2));
69          assertEquals(2, count);
70        }
71        // use 'list' as alias
72        {
73          long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2));
74          assertEquals(2, count);
75        }
76        // use actual name #2693
77        {
78          long count = mapper.getUserCountUsingList_RowBounds(new RowBounds(0, 5), Arrays.asList(1, 2));
79          assertEquals(2, count);
80        }
81      }
82    }
83  
84    @Test
85    void testSingleArrayParameterWhenUseActualParamNameIsTrue() {
86      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87        Mapper mapper = sqlSession.getMapper(Mapper.class);
88        // use actual name
89        {
90          long count = mapper.getUserCountUsingArray(1, 2);
91          assertEquals(2, count);
92        }
93        // use 'array' as alias
94        {
95          long count = mapper.getUserCountUsingArrayWithAliasArray(1, 2);
96          assertEquals(2, count);
97        }
98      }
99    }
100 
101   interface Mapper {
102     // @formatter:off
103     @Select({
104         "<script>",
105         "  select count(*) from users u where u.id in",
106         "  <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>",
107         "    #{item}",
108         "  </foreach>",
109         "</script>"
110       })
111     // @formatter:on
112     Long getUserCountUsingList(List<Integer> ids);
113 
114     // @formatter:off
115     @Select({
116         "<script>",
117         "  select count(*) from users u where u.id in",
118         "  <foreach item='item' index='index' collection='collection' open='(' separator=',' close=')'>",
119         "    #{item}",
120         "  </foreach>",
121         "</script>"
122       })
123     // @formatter:on
124     Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids);
125 
126     // @formatter:off
127     @Select({
128         "<script>",
129         "  select count(*) from users u where u.id in",
130         "  <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
131         "    #{item}",
132         "  </foreach>",
133         "</script>"
134       })
135     // @formatter:on
136     Long getUserCountUsingListWithAliasIsList(List<Integer> ids);
137 
138     // @formatter:off
139     @Select({
140         "<script>",
141         "  select count(*) from users u where u.id in",
142         "  <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>",
143         "    #{item}",
144         "  </foreach>",
145         "</script>"
146       })
147     // @formatter:on
148     Long getUserCountUsingArray(Integer... ids);
149 
150     // @formatter:off
151     @Select({
152         "<script>",
153         "  select count(*) from users u where u.id in",
154         "  <foreach item='item' index='index' collection='array' open='(' separator=',' close=')'>",
155         "    #{item}",
156         "  </foreach>",
157         "</script>"
158       })
159     // @formatter:on
160     Long getUserCountUsingArrayWithAliasArray(Integer... ids);
161 
162     // @formatter:off
163     @Select({
164         "<script>",
165         "  select count(*) from users u where u.id in",
166         "  <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>",
167         "    #{item}",
168         "  </foreach>",
169         "</script>"
170       })
171     // @formatter:on
172     Long getUserCountUsingList_RowBounds(RowBounds rowBounds, List<Integer> ids);
173   }
174 
175 }