Skip to content

Commit 9c2fb55

Browse files
committed
New test case to show how to use enum constants in mapped statements
1 parent fb56e7a commit 9c2fb55

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
additional.context.attributes=trailingWildCardFormatter:org.mybatis.scripting.velocity.use.TrailingWildCardFormatter
1+
additional.context.attributes=trailingWildCardFormatter:org.mybatis.scripting.velocity.use.TrailingWildCardFormatter,enumBinder:org.mybatis.scripting.velocity.use.EnumBinder
22
userdirective=org.mybatis.scripting.velocity.use.CustomUserDirective
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2013 MyBatis.org.
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.mybatis.scripting.velocity.use;
17+
18+
/**
19+
*
20+
* @author mnesarco
21+
*/
22+
public class EnumBinder {
23+
24+
public EnumWrapper bind(String className) throws ClassNotFoundException {
25+
return new EnumWrapper(Class.forName(className));
26+
}
27+
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2013 MyBatis.org.
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.mybatis.scripting.velocity.use;
17+
18+
import java.util.HashMap;
19+
20+
/**
21+
*
22+
* @author mnesarco
23+
*/
24+
public class EnumWrapper extends HashMap<String, Integer> {
25+
26+
public EnumWrapper(Class<?> e) {
27+
if (e.isEnum()) {
28+
Object[] consts = e.getEnumConstants();
29+
for (int i = 0; i<consts.length; i++) {
30+
put(consts[i].toString(), i);
31+
}
32+
}
33+
else {
34+
throw new IllegalArgumentException("Supplied argument is not an enum class");
35+
}
36+
}
37+
38+
}

src/test/java/org/mybatis/scripting/velocity/use/VelocityLanguageTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class VelocityLanguageTest {
3939

4040
protected static SqlSessionFactory sqlSessionFactory;
4141

42+
public enum IDS {
43+
ZERO, ONE, TWO, THREE, FOUR, FIVE
44+
}
45+
4246
@BeforeClass
4347
public static void setUp() throws Exception {
4448
Connection conn = null;
@@ -145,6 +149,22 @@ public void testSelectNamesWithFormattedParam() {
145149
}
146150
}
147151

152+
@Test
153+
public void testEnumBinding() {
154+
SqlSession sqlSession = sqlSessionFactory.openSession();
155+
try {
156+
157+
List<Name> answer = sqlSession.selectList("org.mybatis.scripting.velocity.use.selectEnumBinding");
158+
assertEquals(3, answer.size());
159+
for (Name n : answer) {
160+
assertEquals("Flintstone", n.getLastName());
161+
}
162+
163+
} finally {
164+
sqlSession.close();
165+
}
166+
}
167+
148168
@Test
149169
public void testSelectNamesWithFormattedParamSafe() {
150170
SqlSession sqlSession = sqlSessionFactory.openSession();

src/test/java/org/mybatis/scripting/velocity/use/mapper.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@
107107
VALUES (@{firstName}, @{lastName})
108108
</insert>
109109

110+
<select id="selectEnumBinding" resultType="org.mybatis.scripting.velocity.use.Name">
111+
#set( $ids = $enumBinder.bind('org.mybatis.scripting.velocity.use.VelocityLanguageTest$IDS') )
112+
SELECT *
113+
FROM names
114+
WHERE
115+
id IN (@{ids.ONE}, @{ids.TWO}, @{ids.THREE})
116+
</select>
117+
110118
</mapper>

0 commit comments

Comments
 (0)