linzhengzhenga 2021-10-18 16:53 采纳率: 50%
浏览 2994
已结题

Error querying database. Cannot invoke "Object.hashCode()" because "key" is null

在eclipse用Wbe学习mybatis的时候出现了问题。

Error querying database. Cause: java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null

The error may exist in com/qfedu/mapper/StudentMapper.xml

The error may involve student.findStudentBySid

The error occurred while executing a query

Cause: java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

TestFindBySid.java

package com.qfedu.test;
import java.io.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import com.qfedu.pojo.Student;

public class TestFindBySid {
    public static void main(String [] a) {
        //读取配置文件
        String resource="mybatis-config.xml";
        try {
            InputStream in=Resources.getResourceAsStream(resource);
            //创建SQLSessionFactory 对象
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
            //创建SqlSession 对象
            SqlSession sqlSession=factory.openSession();
            //调用SqlSession 对象的selectOne()方法执行查询
            Student student=sqlSession.selectOne("student.findStudentBySid",1);
            
            System.out.println(student.toString());
            //关闭SqlSession
            sqlSession.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
        
    }

}

StudentMapper.xml
pclipse 老是提醒我sid不是正确编写,但我是否关了拼写检查,错误还是一样

<?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="student">
  <select id="findStudentBySid" parameterType="Integer"
     resultType="com.qfedu.pojo.Student">
     select *from Student where sid= #{sid}
     </select>
</mapper>


mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mabatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <!-- 配置环境 -->
   <properties><property name="driver" value="com.mysql.jdbc.Driver"/></properties>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="url"
      value="jdbc:mysql://localhost:3306/chapter01"/>
<property name="username" value="root"/>
<property name="password" value="594852956asdw"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件的位置 -->
<mappers>
<mapper resource="com/qfedu/mapper/StudentMapper.xml"/>
</mappers>
</configuration>


救命阿。

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-10-18 16:55
    关注

    namespace对应的是接口全限定路径。

    <mapper namespace="student">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 创建了问题 10月18日

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题