Skip to content

Commit

Permalink
🐛 Native Support mybatis.mapper-locations
Browse files Browse the repository at this point in the history
optimize mybatis.config-location

mybatis#994
  • Loading branch information
xuxiaowei-com-cn committed Aug 25, 2024
1 parent 878fbbf commit 12a6492
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class MyBatisBeanFactoryInitializationAotProcessor

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

private static final String MAPPER_LOCATIONS = MybatisProperties.MYBATIS_PREFIX + ".mapper-locations";

private static final Set<Class<?>> EXCLUDE_CLASSES = new HashSet<>();

static {
Expand All @@ -75,6 +77,7 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL

Environment environment = beanFactory.getBean(Environment.class);
configLocation(environment, hints);
mapperLocations(environment, hints);

for (String beanName : beanNames) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName.substring(1));
Expand All @@ -101,14 +104,23 @@ private void configLocation(Environment environment, RuntimeHints hints) {
if (StringUtils.hasText(configLocation)) {
Resource resource = RESOURCE_RESOLVER.getResource(configLocation);
if (resource.exists()) {
Stream.of(configLocation.replace(ResourceUtils.CLASSPATH_URL_PREFIX, ""))
.forEach(hints.resources()::registerPattern);
Stream.of(resource).forEach(hints.resources()::registerResource);
} else {
logger.error("{}: {} does not exist", CONFIG_LOCATION, configLocation);
}
}
}

private void mapperLocations(Environment environment, RuntimeHints hints) {
String[] mapperLocations = environment.getProperty(MAPPER_LOCATIONS, String[].class);
if (mapperLocations != null) {
for (String mapperLocation : mapperLocations) {
Stream.of(mapperLocation.replace(ResourceUtils.CLASSPATH_URL_PREFIX, ""))
.forEach(hints.resources()::registerPattern);
}
}
}

private void registerMapperRelationships(Class<?> mapperInterfaceType, RuntimeHints hints) {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(mapperInterfaceType);
for (Method method : methods) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"resources": {
"includes": [
{ "pattern": "sample/mybatis/graalvm/xml/mapper/CityMapper.xml" },
{ "pattern": "sample/mybatis/graalvm/xml/mapper/HotelMapper.xml" }
{ "pattern": "sample/mybatis/graalvm/xml/mapper/CityMapper.xml" }
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
#

mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:sample/mybatis/graalvm/xml/custommapper/*.xml
logging.level.root=WARN
logging.level.sample.mybatis.graalvm.xml.mapper=TRACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
</typeAliases>
<mappers>
<mapper resource="sample/mybatis/graalvm/xml/mapper/CityMapper.xml"/>
<mapper resource="sample/mybatis/graalvm/xml/mapper/HotelMapper.xml"/>
</mappers>
</configuration>

0 comments on commit 12a6492

Please sign in to comment.