org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
解释:一般是由于Mapper接口和对应的Mapper.xml对应出现错误,导致爆出这个Exception,主要有几个注意事项:
- 检查xml文件所在的package名称是否和interface对应的package名称一致
比如:接口名是NameMapper.java;对应的xml就应该是NameMapper.xml
- mapper.xml的命名空间(namespace)是否跟mapper接口的包名一致
在这啰嗦一下,如果spring_mybatis.xml配置文件中扫描xml时限制要求,那么一定要按照格式写,否则不会扫描到该xml,即使接口与xml都一致。
比如,接口包名是com.abc.dao,接口名是NameMapper.java,那么mapper.xml的namespace应该是com.abc.dao.NameMapper
--------------------------------------------------------------------------------------------------------
啰嗦一下(这次我就是犯的这个错误,导致一个小时抓狂):
配置文件:<property name="mapperLocations" value="classpath*:cn/gcheng/modules/**/*Mapper.xml"/>
错误代码:
xxxInfoMapperNew.java xxxInfoMapperNew.xml 相关配置文件限制,此xml是扫描不到,导致这个错误
- 检查方法名称能否一致
比如,接口中方法名为findListById,那么mapper.xml中的SQL为<select id="findListById">****</select>
- 检查传入参数是否一致
为防止出现错误,无论单参数或多参数尽量用注解标记@Param(“xxx”),xml中直接取#{xxx},若为pojo对象,使用parameterType=“pojo”)(为防止出现错误,无论单参数或多参数尽量用注解标记@Param(“xxx”),xml中直接取#{xxx},若为pojo对象,使用parameterType=“pojo”
比如,接口中方法为findListById(@Param("id")String id),那么mapper.xml中的SQL为<select id="findListById">*** a.ID = #{id}***</select>
-----------------------------------------------------------------------------------------------------------------------------------
比如,接口中方法为findListByUser(User user),那么mapper.xml中的SQL为<select id="findListByUser" parameterType="cn.gcheng.entity.User">*** a.NAME = #{name}***</select>
- 检查返回值是否一致
值得注意:尽量使用resultMap返回参数,但保证resultMap配置正确。