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  import static org.junit.Assert.fail;
20  
21  import java.io.Reader;
22  import java.sql.Connection;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.apache.ibatis.annotations.Select;
27  import org.apache.ibatis.exceptions.PersistenceException;
28  import org.apache.ibatis.io.Resources;
29  import org.apache.ibatis.jdbc.ScriptRunner;
30  import org.apache.ibatis.session.SqlSession;
31  import org.apache.ibatis.session.SqlSessionFactory;
32  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Test;
35  
36  class NoActualParamNameTest {
37  
38    private static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeAll
41    static void setUp() throws Exception {
42      // create an SqlSessionFactory
43      try (Reader reader = Resources
44          .getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml")) {
45        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46        sqlSessionFactory.getConfiguration().addMapper(Mapper.class);
47        sqlSessionFactory.getConfiguration().setUseActualParamName(false);
48      }
49  
50      // populate in-memory database
51      try (Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
52          Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql")) {
53        ScriptRunner runner = new ScriptRunner(conn);
54        runner.setLogWriter(null);
55        runner.runScript(reader);
56      }
57    }
58  
59    @Test
60    void testSingleListParameterWhenUseActualParamNameIsFalse() {
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        Mapper mapper = sqlSession.getMapper(Mapper.class);
63        // use actual name -> no available and index parameter("0") is not available too
64        {
65          try {
66            mapper.getUserCountUsingList(Arrays.asList(1, 2));
67            fail();
68          } catch (PersistenceException e) {
69            assertEquals("Parameter 'ids' not found. Available parameters are [collection, list]",
70                e.getCause().getMessage());
71          }
72        }
73        // use 'collection' as alias
74        {
75          long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2));
76          assertEquals(2, count);
77        }
78        // use 'list' as alias
79        {
80          long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2));
81          assertEquals(2, count);
82        }
83      }
84    }
85  
86    interface Mapper {
87      // @formatter:off
88      @Select({
89          "<script>",
90          "  select count(*) from users u where u.id in",
91          "  <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>",
92          "    #{item}",
93          "  </foreach>",
94          "</script>"
95          })
96      // @formatter:on
97      Long getUserCountUsingList(List<Integer> ids);
98  
99      // @formatter:off
100     @Select({
101         "<script>",
102         "  select count(*) from users u where u.id in",
103         "  <foreach item='item' index='index' collection='collection' open='(' separator=',' close=')'>",
104         "    #{item}",
105         "  </foreach>",
106         "</script>"
107         })
108     // @formatter:on
109     Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids);
110 
111     // @formatter:off
112     @Select({
113         "<script>",
114         "  select count(*) from users u where u.id in",
115         "  <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
116         "    #{item}",
117         "  </foreach>",
118         "</script>"
119         })
120     // @formatter:on
121     Long getUserCountUsingListWithAliasIsList(List<Integer> ids);
122 
123   }
124 
125 }