在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的.
需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper/business下的所有mapper文件
1 <!-- 配置mybatis的sqlSessionFactory --> 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource" /> 4 <!-- 自动扫描mappers.xml --> 5 <property name="mapperLocations" value="classpath:com/thomas/*/mappers/*Mapper.xml" ></property> 6 <!-- mybatis配置文件 --> 7 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 8 </bean>
结果一直报com/thomas/base/mappers下的mapper文件的某个方法找不到.
错误:Invalid bound statement (not found)
修改成这样就可以找到了.
1 <!-- 配置mybatis的sqlSessionFactory --> 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource" /> 4 <!-- 自动扫描mappers.xml --> 5 <property name="mapperLocations" value="classpath*:com/thomas/*/mappers/*Mapper.xml" ></property> 6 <!-- mybatis配置文件 --> 7 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 8 </bean> 9
原因是classpath:和classpath*:在spring加载资源的时候是不同的.
classpath:只能加载找到的第一个资源文件.(上面只匹配了com/thomas/bu/mappers/business下的mapper文件,而com/thomas/base/mappers就被忽略了)
classpath*:能加载多个路径下的资源文件.(com/thomas/bu/mappers/business和com/thomas/base/mappers都被加载进来了.)