MyBatis异常 之 Mapped Statements collection does not contain value for xxx

背景

执行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文件

评论

暂无评论