项目需要,自己搭建了sprinMVC+Mybatis的开发框架;在Mybatis 的使用过程中,insert、update、delete都可以正常执行;但是问题就出在select,只要select,总报### The error occurred while setting parameters的异常。现象如下:
一、UserDAO.java
package com.gci.common.dao;
import com.gci.common.model.User;
public interface UserDAO {
public User findUser(int id);
public void insertUser(User user);
}
二、UserDAO.xml
<?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">
<!-- 这里namespace必须是UserDAO接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”-->
<mapper namespace="com.gci.common.dao.UserDAO">
<!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 -->
<insert id="insertUser" parameterType="com.gci.common.model.User">
insert into t_user(id,name,age) values(#{id},#{name},#{age})
</insert>
<!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 -->
<select id="findUser" resultType="com.gci.common.model.User" parameterType="int">
select * from t_user where id=#{id}
</select>
</mapper>
3、User.java
package com.gci.common.model;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id,String name,int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
//必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,在查询数据库时,将不能反射构造出User实例
public User() {
super();
}
}
四、执行userDAO
user = userDAO.findUser(2);
五、报异常
异常信息
class org.springframework.web.util.NestedServletException : Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NullPointerException
### The error may exist in com/gci/common/dao/UserDAO.xml
### The error may involve com.gci.common.dao.UserDAO.findUser-Inline
### The error occurred while setting parameters
### SQL: select * from t_user where id=?
### Cause: java.lang.NullPointerException
------解决思路----------------------
id 没传入进来。
------解决思路----------------------
select * from t_user where id=?
数据库中的列名都是些什么?
------解决思路----------------------
这个错误应该是数据类型不明确,加上 #{变量,jdbcType=VARCHAR}