View Javadoc
1   /*
2    *    Copyright 2009-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.apache.ibatis.binding;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import java.util.HashMap;
21  
22  import javax.sql.DataSource;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.annotations.Insert;
26  import org.apache.ibatis.annotations.Param;
27  import org.apache.ibatis.annotations.Select;
28  import org.apache.ibatis.mapping.Environment;
29  import org.apache.ibatis.session.Configuration;
30  import org.apache.ibatis.session.SqlSession;
31  import org.apache.ibatis.session.SqlSessionFactory;
32  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33  import org.apache.ibatis.transaction.TransactionFactory;
34  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.Test;
37  
38  class MapperMethodParamTest {
39  
40    private static SqlSessionFactory sqlSessionFactory;
41  
42    @BeforeAll
43    static void setup() throws Exception {
44      DataSource dataSource = BaseDataTest.createUnpooledDataSource(BaseDataTest.BLOG_PROPERTIES);
45      BaseDataTest.runScript(dataSource, "org/apache/ibatis/binding/paramtest-schema.sql");
46      TransactionFactory transactionFactory = new JdbcTransactionFactory();
47      Environment environment = new Environment("Production", transactionFactory, dataSource);
48      Configuration configuration = new Configuration(environment);
49      configuration.addMapper(Mapper.class);
50      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
51    }
52  
53    @Test
54    void parameterNameIsSizeAndTypeIsLong() {
55      try (SqlSession session = sqlSessionFactory.openSession()) {
56        Mapper mapper = session.getMapper(Mapper.class);
57        mapper.insert("foo", Long.MAX_VALUE);
58        assertThat(mapper.selectSize("foo")).isEqualTo(Long.MAX_VALUE);
59      }
60    }
61  
62    @Test
63    void parameterNameIsSizeUsingHashMap() {
64      try (SqlSession session = sqlSessionFactory.openSession()) {
65        HashMap<String, Object> params = new HashMap<>();
66        params.put("id", "foo");
67        params.put("size", Long.MAX_VALUE);
68        Mapper mapper = session.getMapper(Mapper.class);
69        mapper.insertUsingHashMap(params);
70        assertThat(mapper.selectSize("foo")).isEqualTo(Long.MAX_VALUE);
71      }
72    }
73  
74    interface Mapper {
75      @Insert("insert into param_test (id, size) values(#{id}, #{size})")
76      void insert(@Param("id") String id, @Param("size") long size);
77  
78      @Insert("insert into param_test (id, size) values(#{id}, #{size})")
79      void insertUsingHashMap(HashMap<String, Object> params);
80  
81      @Select("select size from param_test where id = #{id}")
82      long selectSize(@Param("id") String id);
83    }
84  
85  }