Skip to content

Add new property for customizing the defaultEnumTypeHandler #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2020
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
17 changes: 17 additions & 0 deletions src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public class SqlSessionFactoryBean

private String typeHandlersPackage;

@SuppressWarnings("rawtypes")
private Class<? extends TypeHandler> defaultEnumTypeHandler;

private Class<?>[] typeAliases;

private String typeAliasesPackage;
Expand Down Expand Up @@ -293,6 +296,18 @@ public void setTypeHandlers(TypeHandler<?>... typeHandlers) {
this.typeHandlers = typeHandlers;
}

/**
* Set the default type handler class for enum.
*
* @since 2.0.5
* @param defaultEnumTypeHandler
* The default type handler class for enum
*/
public void setDefaultEnumTypeHandler(
@SuppressWarnings("rawtypes") Class<? extends TypeHandler> defaultEnumTypeHandler) {
this.defaultEnumTypeHandler = defaultEnumTypeHandler;
}

/**
* List of type aliases to register. They can be annotated with {@code Alias}
*
Expand Down Expand Up @@ -545,6 +560,8 @@ protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
});
}

targetConfiguration.setDefaultEnumTypeHandler(defaultEnumTypeHandler);

if (!isEmpty(this.scriptingLanguageDrivers)) {
Stream.of(this.scriptingLanguageDrivers).forEach(languageDriver -> {
targetConfiguration.getLanguageRegistry().register(languageDriver);
Expand Down
18 changes: 14 additions & 4 deletions src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2019 the original author or authors.
* Copyright 2010-2020 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 @@ -31,7 +31,6 @@
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.LanguageDriverRegistry;
import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
Expand All @@ -40,9 +39,9 @@
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.apache.ibatis.type.TypeAliasRegistry;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.jupiter.api.Test;
import org.mybatis.core.jdk.type.AtomicNumberTypeHandler;
Expand All @@ -52,7 +51,6 @@
import org.mybatis.spring.type.SuperType;
import org.mybatis.spring.type.TypeHandlerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.mockrunner.mock.jdbc.MockDataSource;

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

@Test
void testDefaultEnumTypeHandler() throws Exception {
setupFactoryBean();
factoryBean.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);

TypeHandlerRegistry typeHandlerRegistry = factoryBean.getObject().getConfiguration().getTypeHandlerRegistry();
assertThat(typeHandlerRegistry.getTypeHandler(MyEnum.class)).isInstanceOf(EnumOrdinalTypeHandler.class);
}

@Test
void testSetObjectFactory() throws Exception {
setupFactoryBean();
Expand Down Expand Up @@ -515,4 +522,7 @@ private static class MyLanguageDriver1 extends RawLanguageDriver {
private static class MyLanguageDriver2 extends RawLanguageDriver {
}

private static enum MyEnum {
}

}