Skip to content

Commit e8f61e7

Browse files
🐛 Native Support mybatis.mapper-locations
optimize mybatis.config-location #994
1 parent e1b9ab9 commit e8f61e7

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class MyBatisBeanFactoryInitializationAotProcessor
5858

5959
private static final String CONFIG_LOCATION = MybatisProperties.MYBATIS_PREFIX + ".config-location";
6060

61+
private static final String MAPPER_LOCATIONS = MybatisProperties.MYBATIS_PREFIX + ".mapper-locations";
62+
6163
private static final Set<Class<?>> EXCLUDE_CLASSES = new HashSet<>();
6264

6365
static {
@@ -75,6 +77,7 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
7577

7678
Environment environment = beanFactory.getBean(Environment.class);
7779
configLocation(environment, hints);
80+
mapperLocations(environment, hints);
7881

7982
for (String beanName : beanNames) {
8083
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName.substring(1));
@@ -101,14 +104,23 @@ private void configLocation(Environment environment, RuntimeHints hints) {
101104
if (StringUtils.hasText(configLocation)) {
102105
Resource resource = RESOURCE_RESOLVER.getResource(configLocation);
103106
if (resource.exists()) {
104-
Stream.of(configLocation.replace(ResourceUtils.CLASSPATH_URL_PREFIX, ""))
105-
.forEach(hints.resources()::registerPattern);
107+
Stream.of(resource).forEach(hints.resources()::registerResource);
106108
} else {
107109
logger.error("{}: {} does not exist", CONFIG_LOCATION, configLocation);
108110
}
109111
}
110112
}
111113

114+
private void mapperLocations(Environment environment, RuntimeHints hints) {
115+
String[] mapperLocations = environment.getProperty(MAPPER_LOCATIONS, String[].class);
116+
if (mapperLocations != null) {
117+
for (String mapperLocation : mapperLocations) {
118+
Stream.of(mapperLocation.replace(ResourceUtils.CLASSPATH_URL_PREFIX, ""))
119+
.forEach(hints.resources()::registerPattern);
120+
}
121+
}
122+
}
123+
112124
private void registerMapperRelationships(Class<?> mapperInterfaceType, RuntimeHints hints) {
113125
Method[] methods = ReflectionUtils.getAllDeclaredMethods(mapperInterfaceType);
114126
for (Method method : methods) {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"resources": {
33
"includes": [
4-
{ "pattern": "sample/mybatis/graalvm/xml/mapper/CityMapper.xml" },
5-
{ "pattern": "sample/mybatis/graalvm/xml/mapper/HotelMapper.xml" }
4+
{ "pattern": "sample/mybatis/graalvm/xml/mapper/CityMapper.xml" }
65
]
76
}
87
}

mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
#
1616

1717
mybatis.config-location=classpath:mybatis-config.xml
18+
mybatis.mapper-locations=classpath:sample/mybatis/graalvm/xml/custommapper/*.xml
1819
logging.level.root=WARN
1920
logging.level.sample.mybatis.graalvm.xml.mapper=TRACE

mybatis-spring-boot-samples/mybatis-spring-boot-sample-graalvm-xml/src/main/resources/mybatis-config.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@
2525
</typeAliases>
2626
<mappers>
2727
<mapper resource="sample/mybatis/graalvm/xml/mapper/CityMapper.xml"/>
28-
<mapper resource="sample/mybatis/graalvm/xml/mapper/HotelMapper.xml"/>
2928
</mappers>
3029
</configuration>
File renamed without changes.

0 commit comments

Comments
 (0)