在MyBatis中,在写完Mapper.xml或绑定了对应的接口之后,我们需要去MyBatis.xml,即配置文件中去指定Mapper.xml或接口,指定的方式有四种,可以参考MyBatis官方文档。
如果在MyBatis.xml中,指定了Mapper.xml,然后又指定了它对应的接口,那么就会报错。
错误信息如下:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/mybatis/mapper/UserMapper.java (best guess)
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.binding.BindingException: Type interface com.mybatis.mapper.UserMapper is already known to the MapperRegistry.
MyBatis.xml文件指定信息:
<mappers>
<!--指定Mapper.xml文件-->
<mapper resource="com/mybatis/mapper/UserMapper.xml"/>
<!--注解Mapper接口-->
<package name="com.mybatis.mapper"/>
</mappers>
只需要保留一个指定即可。