MyBatis 3.2操作数据库报错:StudentMapper未被MapperRegistry识别
问题根因
你遇到的Type interface com.example.JSF.mappings.StudentMapper is not known to the MapperRegistry错误,核心原因是MyBatis没有将你的StudentMapper接口和对应的Mapper XML文件完成绑定,最直接的配置错误在StudentMapper.xml的命名空间配置。
解决步骤
第一步:修正StudentMapper.xml的namespace属性
将StudentMapper.xml中原来的配置:<mapper namespace="com.example.JSF.bean.Student">修改为Mapper接口的全限定名:
<mapper namespace="com.example.JSF.mappings.StudentMapper">MyBatis的接口绑定机制要求Mapper XML的namespace必须和对应Mapper接口的全类名完全一致,才能完成接口和XML的关联注册。
第二步:验证其他配置一致性
确认StudentMapper.xml中所有SQL语句的id属性,和StudentMapper接口中定义的方法名完全一致,你当前的配置中findAllStudents、findStudentById、insertStudent已经完全匹配,无需修改。第三步:排查Mapper文件路径配置(可选)
如果你修改完namespace后仍报错,检查MyBatis配置文件中mapper的路径配置是否正确:
你当前配置是<mapper resource="StudentMapper.xml"/>,要求StudentMapper.xml文件必须放在类路径的根目录下。如果你的XML文件是放在和接口同目录的com/example/JSF/mappings路径下,需要修改为:<mapper resource="com/example/JSF/mappings/StudentMapper.xml"/>
完成以上修改后重启项目即可正常运行。
内容的提问来源于stack exchange,提问作者Abdalrazag Al-Shrufat

