Skip to content

Commit 0871770

Browse files
authored
Merge pull request #458 from kazuki43zoo/defaultEnumTypeHandler
Add new property for customizing the defaultEnumTypeHandler
2 parents 774379a + 8289fda commit 0871770

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public class SqlSessionFactoryBean
120120

121121
private String typeHandlersPackage;
122122

123+
@SuppressWarnings("rawtypes")
124+
private Class<? extends TypeHandler> defaultEnumTypeHandler;
125+
123126
private Class<?>[] typeAliases;
124127

125128
private String typeAliasesPackage;
@@ -293,6 +296,18 @@ public void setTypeHandlers(TypeHandler<?>... typeHandlers) {
293296
this.typeHandlers = typeHandlers;
294297
}
295298

299+
/**
300+
* Set the default type handler class for enum.
301+
*
302+
* @since 2.0.5
303+
* @param defaultEnumTypeHandler
304+
* The default type handler class for enum
305+
*/
306+
public void setDefaultEnumTypeHandler(
307+
@SuppressWarnings("rawtypes") Class<? extends TypeHandler> defaultEnumTypeHandler) {
308+
this.defaultEnumTypeHandler = defaultEnumTypeHandler;
309+
}
310+
296311
/**
297312
* List of type aliases to register. They can be annotated with {@code Alias}
298313
*
@@ -545,6 +560,8 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
545560
});
546561
}
547562

563+
targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler);
564+
548565
if (!isEmpty(this.scriptingLanguageDrivers)) {
549566
Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {
550567
targetConfiguration.getLanguageRegistry().register(languageDriver);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2019 the original author or authors.
2+
* Copyright 2010-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
3131
import org.apache.ibatis.reflection.factory.ObjectFactory;
3232
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
3333
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
34-
import org.apache.ibatis.scripting.LanguageDriver;
3534
import org.apache.ibatis.scripting.LanguageDriverRegistry;
3635
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
3736
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
@@ -40,9 +39,9 @@
4039
import org.apache.ibatis.session.SqlSessionFactory;
4140
import org.apache.ibatis.transaction.TransactionFactory;
4241
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
42+
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
4343
import org.apache.ibatis.type.TypeAliasRegistry;
4444
import org.apache.ibatis.type.TypeException;
45-
import org.apache.ibatis.type.TypeHandler;
4645
import org.apache.ibatis.type.TypeHandlerRegistry;
4746
import org.junit.jupiter.api.Test;
4847
import org.mybatis.core.jdk.type.AtomicNumberTypeHandler;
@@ -52,7 +51,6 @@
5251
import org.mybatis.spring.type.SuperType;
5352
import org.mybatis.spring.type.TypeHandlerFactory;
5453
import org.springframework.core.io.ClassPathResource;
55-
import org.springframework.core.io.Resource;
5654

5755
import com.mockrunner.mock.jdbc.MockDataSource;
5856

@@ -423,6 +421,15 @@ void testSearchATypeHandlerPackageWithSamePackage() throws Exception {
423421
assertThat(typeHandlerRegistry.hasTypeHandler(BigDecimal.class)).isTrue();
424422
}
425423

424+
@Test
425+
void testDefaultEnumTypeHandler() throws Exception {
426+
setupFactoryBean();
427+
factoryBean.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
428+
429+
TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
430+
assertThat(typeHandlerRegistry.getTypeHandler(MyEnum.class)).isInstanceOf(EnumOrdinalTypeHandler.class);
431+
}
432+
426433
@Test
427434
void testSetObjectFactory() throws Exception {
428435
setupFactoryBean();
@@ -515,4 +522,7 @@ private static class MyLanguageDriver1 extends RawLanguageDriver {
515522
private static class MyLanguageDriver2 extends RawLanguageDriver {
516523
}
517524

525+
private static enum MyEnum {
526+
}
527+
518528
}

0 commit comments

Comments
 (0)