Mybatis关联查询中分步查询可能出现的问题

我敲好了分步查询的相关代码,如下:

public interface DepartmentMapper {
    
    public Department getDaptById(Integer id);

}
<mapper namespace="com.fenga.mybatis.dao.DepartmentMapper">
    <select id="getDaptById" resultType="com.fenga.mybatis.bean.Department">
        select id,dept_name departmentName from tbl_dept where id=#{id}
    </select>
</mapper>
<resultMap type="com.fenga.mybatis.bean.Employee" id="MyEmpByStep">
          <id column="id" property="id"/>
          <result column="last_name" property="lastName"/>
          <result column="email" property="email"/>
         <result column="gender" property="gender"/>
         <association property="dept" select="com.fenga.mybatis.dao.DepartmentMapper.getDaptById" column="d_id"></association>
      </resultMap>
      
      <select id="getEmpByIdStep" resultMap="MyEmpByStep">
          select * from tbl_employee where id=#{id}
      </select>

错误信息如下:

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.fenga.mybatis.dao.DepartmentMapper.getDaptById
### The error may exist in EmployeeMapper.xml
### The error may involve com.fenga.mybatis.dao.EmployeeMapper.getEmpByIdStep
### The error occurred while handling results
### SQL: select * from tbl_employee where id=?
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.fenga.mybatis.dao.DepartmentMapper.getDaptById

没找到getDaptById方法。

然后我观察文件结构

 

 并没有错误。

然后我分别对employee和department访问数据库进行测试,发现在测试employee没问题,在department出现了下面问题:

org.apache.ibatis.binding.BindingException: Type interface com.fenga.mybatis.dao.DepartmentMapper is not known to the MapperRegistry.

居然是没有在全局变量中进行注册,注册以下信息:

<mappers>
         <mapper resource="EmployeeMapper.xml"/>
         <mapper resource="DepartmentMapper.xml"/>
     </mappers>

成功运行

 

 注意啊!!!在新建Mapping后一定要去注册

 

posted @ 2021-01-30 22:18  金玉良猿  阅读(234)  评论(0)    收藏  举报