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

时间:2022-07-06 18:48:02

在Spring和MyBitis继承的时候,配置mapperLocations.一开始配置是这样的.

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.eloan.base.domain;com.eloan.business.domain" />
<property name="mapperLocations" value="classpath:com/eloan/*/mapper/*Mapper.xml;" />
</bean>
结果一直报com/eloan/base/mapper下的mapper文件的某个方法找不到.

错误:Invalid bound statement (not found)

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.eloan.base.domain;com.eloan.business.domain" />
<property name="mapperLocations" value=classpath*:com/eloan/*/mapper/*Mapper.xml;" />
</bean>

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


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

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