Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
* @author Clinton Begin
Expand Down Expand Up @@ -254,6 +255,9 @@ private void settingsElement(Properties props) throws Exception {
configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
@SuppressWarnings("unchecked")
Class<? extends TypeHandler> typeHandler = (Class<? extends TypeHandler>)resolveClass(props.getProperty("defaultEnumTypeHandler"));
configuration.setDefaultEnumTypeHandler(typeHandler);
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,6 +88,7 @@
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;

/**
Expand Down Expand Up @@ -461,6 +462,18 @@ public TypeHandlerRegistry getTypeHandlerRegistry() {
return typeHandlerRegistry;
}

/**
* Set a default {@link TypeHandler} class for {@link Enum}.
* A default {@link TypeHandler} is {@link org.apache.ibatis.type.EnumTypeHandler}.
* @param typeHandler a type handler class for {@link Enum}
* @since 3.4.5
*/
public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler) {
if (typeHandler != null) {
getTypeHandlerRegistry().setDefaultEnumTypeHandler(typeHandler);
}
}

public TypeAliasRegistry getTypeAliasRegistry() {
return typeAliasRegistry;
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final class TypeHandlerRegistry {

private static final Map<JdbcType, TypeHandler<?>> NULL_TYPE_HANDLER_MAP = new HashMap<JdbcType, TypeHandler<?>>();

private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;

public TypeHandlerRegistry() {
register(Boolean.class, new BooleanTypeHandler());
register(boolean.class, new BooleanTypeHandler());
Expand Down Expand Up @@ -155,6 +157,16 @@ public TypeHandlerRegistry() {
register(char.class, new CharacterTypeHandler());
}

/**
* Set a default {@link TypeHandler} class for {@link Enum}.
* A default {@link TypeHandler} is {@link org.apache.ibatis.type.EnumTypeHandler}.
* @param typeHandler a type handler class for {@link Enum}
* @since 3.4.5
*/
public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler) {
this.defaultEnumTypeHandler = typeHandler;
}

public boolean hasTypeHandler(Class<?> javaType) {
return hasTypeHandler(javaType, null);
}
Expand Down Expand Up @@ -224,7 +236,7 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
if (clazz.isEnum()) {
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz);
if (jdbcHandlerMap == null) {
register(clazz, new EnumTypeHandler(clazz));
register(clazz, getInstance(clazz, defaultEnumTypeHandler));
return TYPE_HANDLER_MAP.get(clazz);
}
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/site/es/xdoc/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
</td>
</tr>
<tr>
<td>
defaultEnumTypeHandler
</td>
<td>
Specifies the <code>TypeHandler</code> used by default for Enum. (Since: 3.4.5)
</td>
<td>
A type alias or fully qualified class name.
</td>
<td>
org.apache.ibatis.type.EnumTypeHandler
</td>
</tr>
<tr>
<td>
callSettersOnNulls
Expand Down
14 changes: 14 additions & 0 deletions src/site/ja/xdoc/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
</td>
</tr>
<tr>
<td>
defaultEnumTypeHandler
</td>
<td>
Enum型に適用するデフォルトの <code>TypeHandler</code> を指定します。(導入されたバージョン: 3.4.5)
</td>
<td>
タイプエイリアスまたは完全修飾クラス名
</td>
<td>
org.apache.ibatis.type.EnumTypeHandler
</td>
</tr>
<tr>
<td>
callSettersOnNulls
Expand Down
14 changes: 14 additions & 0 deletions src/site/ko/xdoc/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
</td>
</tr>
<tr>
<td>
defaultEnumTypeHandler
</td>
<td>
Specifies the <code>TypeHandler</code> used by default for Enum. (Since: 3.4.5)
</td>
<td>
A type alias or fully qualified class name.
</td>
<td>
org.apache.ibatis.type.EnumTypeHandler
</td>
</tr>
<tr>
<td>
callSettersOnNulls
Expand Down
14 changes: 14 additions & 0 deletions src/site/xdoc/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
</td>
</tr>
<tr>
<td>
defaultEnumTypeHandler
</td>
<td>
Specifies the <code>TypeHandler</code> used by default for Enum. (Since: 3.4.5)
</td>
<td>
A type alias or fully qualified class name.
</td>
<td>
org.apache.ibatis.type.EnumTypeHandler
</td>
</tr>
<tr>
<td>
callSettersOnNulls
Expand Down
14 changes: 14 additions & 0 deletions src/site/zh/xdoc/configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environ
org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
</td>
</tr>
<tr>
<td>
defaultEnumTypeHandler
</td>
<td>
Specifies the <code>TypeHandler</code> used by default for Enum. (Since: 3.4.5)
</td>
<td>
A type alias or fully qualified class name.
</td>
<td>
org.apache.ibatis.type.EnumTypeHandler
</td>
</tr>
<tr>
<td>
callSettersOnNulls
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2016 the original author or authors.
Copyright 2009-2017 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,7 @@
<setting name="logImpl" value="SLF4J"/>
<setting name="vfsImpl" value="org.apache.ibatis.io.JBoss6VFS"/>
<setting name="configurationFactory" value="java.lang.String"/>
<setting name="defaultEnumTypeHandler" value="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
</settings>

<typeAliases>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.io.InputStream;
import java.io.StringReader;
import java.math.RoundingMode;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -50,6 +51,8 @@
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
Expand Down Expand Up @@ -96,6 +99,7 @@ public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
assertNull(config.getLogPrefix());
assertNull(config.getLogImpl());
assertNull(config.getConfigurationFactory());
assertThat(config.getTypeHandlerRegistry().getTypeHandler(RoundingMode.class), is(instanceOf(EnumTypeHandler.class)));
}

enum MyEnum {
Expand Down Expand Up @@ -197,6 +201,7 @@ public void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
assertThat(config.getTypeHandlerRegistry().getTypeHandler(Long.class), is(instanceOf(CustomLongTypeHandler.class)));
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class), is(instanceOf(CustomStringTypeHandler.class)));
assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR), is(instanceOf(CustomStringTypeHandler.class)));
assertThat(config.getTypeHandlerRegistry().getTypeHandler(RoundingMode.class), is(instanceOf(EnumOrdinalTypeHandler.class)));

ExampleObjectFactory objectFactory = (ExampleObjectFactory)config.getObjectFactory();
assertThat(objectFactory.getProperties().size(), is(1));
Expand Down