解决Error creating bean with name xxx defined in class path resource [applicationContext.xml]错误

今天使用基于MapperFactoryBean来实现MyBatis与Spring的整合,使用Mapper接口编程,一直报一个错误,调试了很久

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘customerMapper’ defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type ‘java.lang.String’ to required type ‘java.lang.Class’ for property ‘mapperInterface’; nested exception is java.lang.IllegalArgumentException: Cannot find class [mapper.CustomerMapper]

调试了很久,才发现是Mapper接口的名称和对应的Mapper.xml映射文件的名称不一致
解决Error creating bean with name xxx defined in class path resource [applicationContext.xml]错误
在使用Mapper接口编程的方式,需要遵循一些规范

  1. Mapper接口的名称和对应的Mapper.xml映射文件的名称必须一致
    解决Error creating bean with name xxx defined in class path resource [applicationContext.xml]错误
  2. Mapper.xml文件中的namespace与Mapper接口的类路径相同
    解决Error creating bean with name xxx defined in class path resource [applicationContext.xml]错误
  3. Mapper接口中的方法名和Mapper.xml中定义的每个执行语句的id相同
  4. Mapper接口中方法的输入参数类型要和Mapper.xml中定义的每个sql的parameterType的类型相同
  5. Mapper接口方法的输出参数类型要和Mapper.xml中定义的每个sql的resultType的类型相同
    解决Error creating bean with name xxx defined in class path resource [applicationContext.xml]错误