diff --git a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java index c64328ec51..01d7790ae0 100644 --- a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java +++ b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java @@ -366,6 +366,10 @@ public void setConfiguration(Configuration configuration) { * * @param mapperLocations * location of MyBatis mapper files + * @throws IOException + * in case of I/O errors. + * Class.getResource() returns null when trying to get mapperLocations from newly generated files or JAR files, + * ClassLoader.getResources() is advisable. */ public void setMapperLocations(Resource... mapperLocations) { this.mapperLocations = mapperLocations; diff --git a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java index 7ada0e4164..4772a8199c 100644 --- a/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java +++ b/src/test/java/org/mybatis/spring/SqlSessionFactoryBeanTest.java @@ -20,6 +20,7 @@ import com.mockrunner.mock.jdbc.MockDataSource; +import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Properties; @@ -53,6 +54,8 @@ 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 org.springframework.core.io.support.PathMatchingResourcePatternResolver; class SqlSessionFactoryBeanTest { @@ -335,6 +338,30 @@ void testMapperLocationsWithNullEntry() throws Exception { assertDefaultConfig(factoryBean.getObject()); } + @Test + void testSingleResourceWildcardMapperLocationsShouldThrowException() { + setupFactoryBean(); + + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + Resource resource = resolver.getResource("classpath*:org/mybatis/spring/*Mapper.xml"); + + factoryBean.setMapperLocations(resource); + + assertThrows(IOException.class, () -> factoryBean.getObject()); + } + + @Test + void testMultiResourceMapperLocations() throws Exception { + setupFactoryBean(); + + PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + Resource[] resource = resolver.getResources("classpath*:org/mybatis/spring/*Mapper.xml"); + + factoryBean.setMapperLocations(resource); + + assertNotNull(factoryBean.getObject()); + } + @Test void testAddATypeHandler() throws Exception { setupFactoryBean();