解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.mapper.方法
在Spring与Mybatis整合时,可能会遇到这样的报错
原因:
其原因为mapper路径的映射错误,表示在尝试执行某个 Mapper 接口的方法时,MyBatis 无法找到对应的 SQL 语句。
造成该错误的可能方式为:
在mapper接口文件上使用了@Mapper注解,在controller调用方法时则无法找到该方法,只能使用注解方式写sql语句才能被识别到,生成到mapper.xml文件中的sql语句无法被使用
修改方法:
1.将@Mapper注解改为@Reposity
2.在MybatisConfig配置文件中加入扫描mapper映射文件的语句
不要觉得在 MybatisConfig配置文件中的MapperScannerConfigurer类加入mapperScannerConfigurer.setBasePackage("com.xxx.mapper");,程序就可以扫描到xxxMapper.xml文件了,其实不然,还需要在SqlSessionFactoryBean类中加入一个语句:
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));这样才能正确扫描到xxxMapper.xml文件和Mapper文件。