解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题

MyBatis使用问题

解决办法

解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题

遇到的问题

解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题

    1. 检查xml文件所在package名称是否和Mapper Interface 所在的包名
    <mapper namespace="com.my.example.mapper.DeptMapper">
    
    </mapper>
    
    1. DeptMapper接口的方法在DeptMapper.xml中没有,然后执行DeptMapper的方法会报错
    1. DeptMapper的方法返回值是List<Freshman>,而select元素没有正确配置ResultMap,或者只配置了ResultType
    1. 空行问题,名字是否有空行等
    1. 查看mapper的XML配置路径是否正确。
    <mappers>
        <mapper resource="mapper/FreshmanMapper.xml"/>
        <!-- <mapper resource="mapper/DeptMapper.xml"/>-->
        <mapper class="cn.com.example.mapper.DeptMapper"/>
    </mappers>
    
  • 最容易忽视的点:注意接口名与Mybatis的映射文件名一定要一模一样。

在这里插入图片描述
在这里插入图片描述

通过接口来实现MyBatis的使用

  • 1.配置mybatis-config.xml(通过映射器接口实现类的完全限定类名)
    在这里插入图片描述
  • 2.创建对应的xml文件
    在这里插入图片描述
  • 3.在xml文件中配置namespace
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.example.mapper.DeptMapper">
	<!---执行数据库操作-->
</mapper>	
  • 4.创建接口文件
    在这里插入图片描述
  • 5.提供对应的接口方法
public interface DeptMapper {
	List<Student> selectAll();
}
  • 6.在对应的xml文件中实现接口中的数据库操作(id值要与接口中方法对应resultTypeMyBatis中设置的typeAlias(不区分大小写))
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.example.mapper.DeptMapper">
	<select id="selectAll" resultType="student">
		select * from tb_stu
	</select>
</mapper>	
    1. 编写测试代码
@Test
public void testGetallStudent() {
    SqlSession sqlSession = MybatisUtils.getSession();
    DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
    List<Student> list = mapper.selectAll();
    list.forEach(student-> System.out.println(student));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值