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
Expand Up @@ -25,6 +25,7 @@
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.ClassPathMapperScanner;
Expand Down Expand Up @@ -84,6 +85,8 @@ public class MybatisAutoConfiguration implements InitializingBean {

private final Interceptor[] interceptors;

private final TypeHandler[] typeHandlers;

private final ResourceLoader resourceLoader;

private final DatabaseIdProvider databaseIdProvider;
Expand All @@ -92,11 +95,13 @@ public class MybatisAutoConfiguration implements InitializingBean {

public MybatisAutoConfiguration(MybatisProperties properties,
ObjectProvider<Interceptor[]> interceptorsProvider,
ObjectProvider<TypeHandler[]> typeHandlersProvider,
ResourceLoader resourceLoader,
ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.typeHandlers = typeHandlersProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
Expand Down Expand Up @@ -143,6 +148,9 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.typeHandlers)) {
factory.setTypeHandlers(this.typeHandlers);
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public SqlSessionFactory masterSqlSessionFactory() throws Exception {

<ul>
<li><a href="http://www.mybatis.org/mybatis-3/configuration.html#plugins">Interceptor</a></li>
<li><a href="http://www.mybatis.org/mybatis-3/configuration.html#typeHandlers">TypeHandler(Available since 2.1.0)</a></li>
<li><a href="http://www.mybatis.org/mybatis-3/configuration.html#databaseIdProvider">DatabaseIdProvider</a></li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
package org.mybatis.spring.boot.autoconfigure;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.sql.DataSource;

Expand All @@ -32,6 +37,8 @@
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -238,6 +245,20 @@ void testWithInterceptors() {
this.context.close();
}

@Test
void testWithTypeHandlers() {
this.context.register(EmbeddedDataSourceConfiguration.class,
MybatisTypeHandlerConfiguration.class,
MybatisAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getTypeHandlerRegistry()
.getTypeHandler(UUID.class)).isInstanceOf(MyTypeHandler.class);
this.context.close();
}

@Test
void testWithDatabaseIdProvider() {
this.context.register(EmbeddedDataSourceConfiguration.class,
Expand Down Expand Up @@ -600,6 +621,17 @@ public MyInterceptor myInterceptor() {

}

@Configuration
@EnableAutoConfiguration
static class MybatisTypeHandlerConfiguration {

@Bean
public MyTypeHandler myTypeHandler() {
return new MyTypeHandler();
}

}

@Configuration
@EnableAutoConfiguration
static class MybatisPropertiesConfigurationCustomizer {
Expand Down Expand Up @@ -719,4 +751,28 @@ static class MySqlSessionTemplate extends SqlSessionTemplate {
}
}

static class MyTypeHandler extends BaseTypeHandler<UUID> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException {

}

@Override
public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException {
return null;
}

@Override
public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return null;
}

@Override
public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}

}

}