在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>
救命阿。