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

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">
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • CSDN专家-微编程 2021-10-18 17:03

    你确定student是你接口名字(你的接口dao)?另外如果你mybatis没有配置的话只能使用全限定类名了(你的student 的dao接口全路径)

    img

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题