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

created at 10-21-2021 views: 84

error

There was a problem when learning mybatis with Wbe in eclipse.

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

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) {
//Read configuration file
         String resource="mybatis-config.xml";
         try {
             InputStream in=Resources.getResourceAsStream(resource);
             //Create SQLSessionFactory object
             SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
             //Create SqlSession object
             SqlSession sqlSession=factory.openSession();
             //Call the selectOne() method of the SqlSession object to execute the query
             Student student=sqlSession.selectOne("student.findStudentBySid",1);
             System.out.println(student.toString());
             //Close SqlSession
             sqlSession.close();
         }catch(IOException e) {
             e.printStackTrace();
        }
    }
}

StudentMapper.xml
pclipse always reminds me that the sid is not written correctly, but if I turn off the spell check, the error is still the same

<?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>
   <!-- Configuration Environment -->
   <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>
<!-- Configure the location of the mapping file -->
<mappers>
<mapper resource="com/qfedu/mapper/StudentMapper.xml"/>
</mappers>
</configuration>

Solution

The namespace corresponds to the fully qualified path of the interface.

<mapper namespace="student">
created at:10-21-2021
edited at: 10-21-2021: