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