背景
执行mybatis mapper中的一个方法时报错:
1java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.xxx.mapper.SysDictionaryMapper.findByTypeCode 2 org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:672) 3 org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:507) 4 org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:500) 5 org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:240)
分析
查看SysDictionaryMapper.findByTypeCode是否存在:
1 SysDictionaryMapper.java 2 List<SysDictionaryDO> findByTypeCode(@Param("typeCode") String typeCode); 3 4 SysDictionaryMapper.xml 5 <select id="findByTypeCode" resultMap="BaseResultMap"> 6 SELECT <include refid="baseColumn"/> 7 FROM <include refid="tableName"/> 8 WHERE type_code = #{typeCode} and del_status = 0 9 ORDER BY sort 10 </select>
查看配置:
1 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 2 <property name="dataSource" ref="dataSource" /> 3 <property name="configLocation" value="classpath:mybatis-config.xml" /> 4 <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" /> 5 <property name="plugins"> 6 <array> 7 <bean class="com.xxx.interceptor.MapInterceptor"/> 8 <bean class="com.xxx.interceptor.ParamterInterceptor"/> 9 </array> 10 </property> 11 </bean>
通过分析发现,SysDictionaryMapper.xml并不是在mapper目录下,而是在其子目录下,所以导致找不到该mapper文件;
解决方案
修改配置
<property name="mapperLocations" value="classpath:mapper/**/*Mapper.xml" />
mapper/**/*Mapper.xml表示包含目录及其子目录下的所有mapper文件