Skip to content

Commit 2fc7339

Browse files
committed
Add the typeAliasesSuperType on MybatisProperties
Fixes gh-230
1 parent 0130dee commit 2fc7339

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed

mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
143143
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
144144
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
145145
}
146+
if (this.properties.getTypeAliasesSuperType() != null) {
147+
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
148+
}
146149
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
147150
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
148151
}

mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure/MybatisProperties.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 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.
@@ -56,6 +56,12 @@ public class MybatisProperties {
5656
*/
5757
private String typeAliasesPackage;
5858

59+
/**
60+
* The super class for filtering type alias.
61+
* If this not specifies, the MyBatis deal as type alias all classes that searched from typeAliasesPackage.
62+
*/
63+
private Class<?> typeAliasesSuperType;
64+
5965
/**
6066
* Packages to search for type handlers. (Package delimiters are ",; \t\n")
6167
*/
@@ -131,6 +137,20 @@ public void setTypeAliasesPackage(String typeAliasesPackage) {
131137
this.typeAliasesPackage = typeAliasesPackage;
132138
}
133139

140+
/**
141+
* @since 1.3.3
142+
*/
143+
public Class<?> getTypeAliasesSuperType() {
144+
return typeAliasesSuperType;
145+
}
146+
147+
/**
148+
* @since 1.3.3
149+
*/
150+
public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
151+
this.typeAliasesSuperType = typeAliasesSuperType;
152+
}
153+
134154
public boolean isCheckConfigLocation() {
135155
return this.checkConfigLocation;
136156
}

mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 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.
@@ -314,6 +314,8 @@ public void testMixedWithConfigurationFileAndTypeAliasesPackageAndMapperLocation
314314
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
315315
assertThat(configuration.getMappedStatementNames()).contains("selectCityById");
316316
assertThat(configuration.getMappedStatementNames()).contains("org.mybatis.spring.boot.autoconfigure.repository.CityMapperImpl.selectCityById");
317+
assertThat(configuration.getTypeAliasRegistry().getTypeAliases()).containsKey("city");
318+
assertThat(configuration.getTypeAliasRegistry().getTypeAliases()).containsKey("name");
317319
}
318320

319321
@Test
@@ -509,6 +511,23 @@ public void testMySqlSessionTemplate() {
509511
assertThat(this.context.getBean(SqlSessionTemplate.class)).isInstanceOf(MySqlSessionTemplate.class);
510512
}
511513

514+
@Test
515+
public void testTypeAliasesSuperTypeIsSpecify() {
516+
EnvironmentTestUtils
517+
.addEnvironment(
518+
this.context,
519+
"mybatis.type-aliases-package:org.mybatis.spring.boot.autoconfigure.domain",
520+
"mybatis.type-aliases-super-type:org.mybatis.spring.boot.autoconfigure.domain.Domain");
521+
this.context.register(EmbeddedDataSourceConfiguration.class,
522+
MybatisBootMapperScanAutoConfiguration.class);
523+
this.context.refresh();
524+
525+
org.apache.ibatis.session.Configuration configuration = this.context.getBean(
526+
SqlSessionFactory.class).getConfiguration();
527+
assertThat(configuration.getTypeAliasRegistry().getTypeAliases()).containsKey("city");
528+
assertThat(configuration.getTypeAliasRegistry().getTypeAliases()).doesNotContainKey("name");
529+
}
530+
512531
@Configuration
513532
@EnableAutoConfiguration
514533
@MapperScan("org.mybatis.spring.boot.autoconfigure.mapper")

mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/domain/City.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.mybatis.spring.boot.autoconfigure.domain;
1717

18-
public class City {
18+
public class City implements Domain {
1919

2020
private Long id;
2121

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2015-2018 the original author or authors.
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+
package org.mybatis.spring.boot.autoconfigure.domain;
17+
18+
interface Domain {
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2015-2018 the original author or authors.
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+
package org.mybatis.spring.boot.autoconfigure.domain;
17+
18+
public class Name {
19+
20+
private Long first;
21+
22+
private String last;
23+
24+
public Long getFirst() {
25+
return first;
26+
}
27+
28+
public void setFirst(Long first) {
29+
this.first = first;
30+
}
31+
32+
public String getLast() {
33+
return last;
34+
}
35+
36+
public void setLast(String last) {
37+
this.last = last;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)