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
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
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
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
62 {
63 long count = mapper.getUserCountUsingList(Arrays.asList(1, 2));
64 assertEquals(2, count);
65 }
66
67 {
68 long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2));
69 assertEquals(2, count);
70 }
71
72 {
73 long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2));
74 assertEquals(2, count);
75 }
76
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
89 {
90 long count = mapper.getUserCountUsingArray(1, 2);
91 assertEquals(2, count);
92 }
93
94 {
95 long count = mapper.getUserCountUsingArrayWithAliasArray(1, 2);
96 assertEquals(2, count);
97 }
98 }
99 }
100
101 interface Mapper {
102
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
112 Long getUserCountUsingList(List<Integer> ids);
113
114
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
124 Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids);
125
126
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
136 Long getUserCountUsingListWithAliasIsList(List<Integer> ids);
137
138
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
148 Long getUserCountUsingArray(Integer... ids);
149
150
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
160 Long getUserCountUsingArrayWithAliasArray(Integer... ids);
161
162
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
172 Long getUserCountUsingList_RowBounds(RowBounds rowBounds, List<Integer> ids);
173 }
174
175 }