背景
執行mybatis mapper中的一個方法時報錯:
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.xxx.mapper.SysDictionaryMapper.findByTypeCode
org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500)
org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240)
分析
查看SysDictionaryMapper.findByTypeCode是否存在:
SysDictionaryMapper.java
List<SysDictionaryDO> findByTypeCode(@Param("typeCode") String typeCode);
SysDictionaryMapper.xml
<select id="findByTypeCode" resultMap="BaseResultMap">
SELECT <include refid="baseColumn"/>
FROM <include refid="tableName"/>
WHERE type_code = #{typeCode} and del_status = 0
ORDER BY sort
</select>
查看配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
<property name="plugins">
<array>
<bean class="com.xxx.interceptor.MapInterceptor"/>
<bean class="com.xxx.interceptor.ParamterInterceptor"/>
</array>
</property>
</bean>
通過分析發現,SysDictionaryMapper.xml並不是在mapper目錄下,而是在其子目錄下,所以導致找不到該mapper文件;
解決方案
修改配置
<property name="mapperLocations" value="classpath:mapper/**/*Mapper.xml" />
mapper/**/*Mapper.xml表示包含目錄及其子目錄下的所有mapper文件