项目目录结构
最近学习mybatis整合,整合过程中出现Invalid bound statement(not found)这个异常,找了好久才找到
出现这个异常的原因有几个:
1.在映射文件*Mapper的namespace,如果*Mapper.xml文件和*Mapper.java文件在同一目录,肯定是匹配的,如果不在同一目录,此处为*Mapper.java的路径
<mapper namespace="com.share.dao.UserMapper">
<resultMap type="User" id="userResultMap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="age" column="age"/>
<result property="birthday" column="birthday"/>
</resultMap>
2.注意spring配置文件中包扫描位置也有
如
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.share.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
3.在*Mapper.xml中的id必须与*Mapper.java中的方法名一致,否则也会报相关的错误
*Mapper.xml
<mapper namespace="com.share.dao.UserMapper">
<resultMap type="User" id="userResultMap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="age" column="age"/>
<result property="birthday" column="birthday"/>
</resultMap>
<select id="findById" parameterType="int" resultType="User" resultMap="userResultMap">
select * from user where id=#{id}
</select>
</mapper>
*Mapper.java
public interface UserMapper {
/**
* 根据用户id查询用户
*/
public User findById(Integer id);
由于很久没用了,复习一下,居然都忘了,导致了这么多问题,关于invalid bound statement(not found) 这个问题,我将这三个地方改好就好了。