1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
74 {
75 long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2));
76 assertEquals(2, count);
77 }
78
79 {
80 long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2));
81 assertEquals(2, count);
82 }
83 }
84 }
85
86 interface Mapper {
87
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
97 Long getUserCountUsingList(List<Integer> ids);
98
99
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
109 Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids);
110
111
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
121 Long getUserCountUsingListWithAliasIsList(List<Integer> ids);
122
123 }
124
125 }