Mybatis报错——Type interface com.mapper.EmployeeMapper is already known to the MapperRegistry.
2017-12-20 23:15
676 查看
Mybatis报错——Type interface com.mapper.EmployeeMapper is already known to the MapperRegistry.
先说我发生这个错误的原因吧:EmployeeMapper.xml映射文件被单独注册一次,批量注册的包中包含也EmployeeMapper.xml动态绑定的EmployeeMapper.java。也就是说EmployeeMapper接口(与EmployeeMapper.xml动态绑定的接口)被重复注册了两次。如果你的问题解决了,那么恭喜,并不用看下去了。
报错如下
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in com/mapper/EmployeeMapper.java (best guess) ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.binding.BindingException: Type interface com.mapper.EmployeeMapper is already known to the MapperRegistry. at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ................. ................. at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:118) ... 28 more
报错背景:
用mybatis框架获取数据库信息时发生的。
mybatis-config.xml文件中,
<mappers>节点下,既有
<mapper />又有批量注册这两个子节点。代码如下:
<mappers> <mapper resource="EmployeeMapper.xml" /> <!-- 批量注册 :接口与映射文件要在同一个包下 --> <!-- 通过package元素将会把指定包下面的所有Mapper接口进行注册 --> <package name="com.mapper"/> </mappers>
再看我的项目,如图:
我的项目中EmployeeMapper.java文件存在于com.mapper包中,而EmployeeMapper.xml类动态绑定了EmployeeMapper.java,
EmployeeMapper.xml文件如图:
异常分析
以上配置就导致了,与EmployeeMapper.xml映射文件绑定的EmployeeMapper.java被注册了两次。
<!-- 第一次 --> <mapper resource="EmployeeMapper.xml" /> <!-- 第二次:批量注册:该包下所有映射文件及绑定的接口都会被注册 --> <pa ac85 ckage name="com.mapper"/>
尽管EmployeeMapper.xml映射文件不在该包下,但是与其绑定的EmployeeMapper类的源文件EmployeeMapper.java文件在该包下,在mybatis中的MapperRegistry就会认为EmployeeMapper被注册了两次。故而报错:EmployeeMapper is already known to the MapperRegistry.(EmployeeMapper已存在)。
如果EmployeeMapper.xml也在批量注册的包下,那也是一样的。
关键在于EmployeeMapper接口(与EmployeeMapper.xml动态绑定的接口)被重复注册了两次。
问题解决方案
检查是否有被重复注册的与映射文件动态绑定的接口相关文章推荐
- Type interface com.mybatis.mapper.StudentMapper is not known to the MapperRegistry
- [练习][错误]MyBatis出错:Type interface com.dao.SubjectDao is not known to the MapperRegistry
- Type interface com.mybatis.mapper.UserMapper is not known to the MapperRegistry. 解决方法
- Type interface dxm.com.mapper.UserMapperI is already known to the MapperRegistry.
- Mybatis出现Type interface XXXMapper is not known to the MapperRegistry.的解决办法。
- Type interface com.xxx.xx.xxxDao is not known to the MapperRegistry
- Type interface com.binarysoft.service.region.RegionDAO is not known to the MapperRegistry.
- Type interface com.mapper.GradeMapper is not known to the MapperRegistry的解决办法
- Type interface com.zhaoka.mapper.DatKcardKmMapper is not known to the MapperRegistry
- Type interface com.hk.ssm.mapper.StudentMapper is not known to the MapperRegistry.
- FAQ(10):Type interface com.smbms.dao.UserMapper is not known to the MapperRegistry.
- Type interface XXXMapper is not known to the MapperRegistry 解决办法
- 解决org.apache.ibatis.binding.BindingException: Type interface xxx is not known to the MapperRegistry
- Type interface is not known to the MapperRegistry.的解决办法
- Type interface mapper.UserMapper is not known to the MapperRegistry.
- Type interface mapper.XxxMapper is not known to the MapperRegistry.
- Type interface "xxxxxxxx" is not known to the MapperRegistry
- Type interface cn.itsun.dao.UserDao is not known to the MapperRegistry.
- mybatis 错误提示:is not known to the MapperRegistry.
- Mybatis注解学习--xxxMapper is not known to the MapperRegistry