spring加载资源文件中classpath*与classpath的区别

时间:2021-05-20 06:23:04

spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的.

需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper/business下的所有mapper文件


 <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml -->
<property name="mapperLocations" value="classpath:com/thomas/*/mappers/*Mapper.xml" ></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

 

结果一直报com/thomas/base/mappers下的mapper文件的某个方法找不到.

错误:Invalid bound statement (not found)

修改成这样就可以找到了.

 <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml -->
<property name="mapperLocations" value="classpath*:com/thomas/*/mappers/*Mapper.xml" ></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

原因是classpath:和classpath*:在spring加载资源的时候是不同的.

classpath:只能加载找到的第一个资源文件.(上面只匹配了com/thomas/bu/mappers/business下的mapper文件,而com/thomas/base/mappers就被忽略了)

classpath*:能加载多个路径下的资源文件.(com/thomas/bu/mappers/business和com/thomas/base/mappers都被加载进来了.)