Skip to content

Commit 6742337

Browse files
committed
Added type handler scanning
1 parent ed626e5 commit 6742337

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, In
8585
private Interceptor[] plugins;
8686

8787
private TypeHandler[] typeHandlers;
88+
89+
private String typeHandlersPackage;
8890

8991
private Class<?>[] typeAliases;
9092

@@ -114,6 +116,18 @@ public void setTypeAliasesPackage(String typeAliasesPackage) {
114116
this.typeAliasesPackage = typeAliasesPackage;
115117
}
116118

119+
/**
120+
* Packages to search for type handlers.
121+
*
122+
* @since 1.0.1
123+
*
124+
* @param typeHandlersPackage package to scan for type handlers
125+
*
126+
*/
127+
public void setTypeHandlersPackage(String typeHandlersPackage) {
128+
this.typeHandlersPackage = typeHandlersPackage;
129+
}
130+
117131
/**
118132
* Set type handlers. They must be annotated with {@code MappedTypes} and optionally with {@code MappedJdbcTypes}
119133
*
@@ -308,6 +322,17 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
308322
}
309323
}
310324

325+
if (StringUtils.hasLength(this.typeHandlersPackage)) {
326+
String[] typeHandlersPackageArray = StringUtils.tokenizeToStringArray(this.typeHandlersPackage,
327+
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
328+
for (String packageToScan : typeHandlersPackageArray ) {
329+
configuration.getTypeHandlerRegistry().register(packageToScan);
330+
if (this.logger.isDebugEnabled()) {
331+
this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
332+
}
333+
}
334+
}
335+
311336
if (!ObjectUtils.isEmpty(this.typeHandlers)) {
312337
for (TypeHandler typeHandler : this.typeHandlers) {
313338
configuration.getTypeHandlerRegistry().register(typeHandler);

src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertSame;
2222
import static org.junit.Assert.assertTrue;
2323

24+
import java.math.BigDecimal;
2425
import java.math.BigInteger;
2526

2627
import org.apache.ibatis.session.SqlSessionFactory;
@@ -207,6 +208,16 @@ public void testSearchATypeAliasPackage() throws Exception {
207208
typeAliasRegistry.resolveAlias("testAlias2");
208209
}
209210

211+
@Test
212+
public void testSearchATypeHandlerPackage() throws Exception {
213+
setupFactoryBean();
214+
factoryBean.setTypeHandlersPackage("org/mybatis/spring/type");
215+
216+
TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
217+
assertTrue(typeHandlerRegistry.hasTypeHandler(BigInteger.class));
218+
assertTrue(typeHandlerRegistry.hasTypeHandler(BigDecimal.class));
219+
}
220+
210221
private void assertDefaultConfig(SqlSessionFactory factory) {
211222
assertConfig(factory, SqlSessionFactoryBean.class.getSimpleName(),
212223
org.mybatis.spring.transaction.SpringManagedTransactionFactory.class);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2010-2011 The myBatis Team
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+
17+
package org.mybatis.spring.type;
18+
19+
import java.math.BigDecimal;
20+
import java.sql.CallableStatement;
21+
import java.sql.PreparedStatement;
22+
import java.sql.ResultSet;
23+
import java.sql.SQLException;
24+
25+
import org.apache.ibatis.type.JdbcType;
26+
import org.apache.ibatis.type.MappedTypes;
27+
import org.apache.ibatis.type.TypeHandler;
28+
29+
/**
30+
* @version $Id: MyBatisSampleTest.java 2697 2010-10-14 13:04:41Z eduardo.macarron $
31+
*/
32+
@MappedTypes(BigDecimal.class)
33+
public class DummyTypeHandler2 implements TypeHandler {
34+
35+
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
36+
}
37+
38+
public Object getResult(ResultSet rs, String columnName) throws SQLException {
39+
return null;
40+
}
41+
42+
public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
43+
return null;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)