Mybatis select 一直报### The error occurred while setting parameters异常

Dustin15 2014-07-15 11:41:53
项目需要,自己搭建了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
...全文
98516 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_35911376 2016-08-19
  • 打赏
  • 举报
回复
你的代码是不是写错了 我也是这样的最后是代码错了
liuxing_hard 2016-07-25
  • 打赏
  • 举报
回复
跟楼主同样的错误,而且,我导入的就是ojdbc14-10.2.0.5.0.jar,不知道,为什么还不行,还在寻觅调试中
qq_22805767 2016-01-18
  • 打赏
  • 举报
回复
跟楼主一样的错误,照着12楼的方法改了就好了,也是郁闷了好久了,谢谢楼主的答案!
天狼10010 2015-11-13
  • 打赏
  • 举报
回复
<property name="timeBetweenEvictionRunsMillis">
<value>10000</value>
</property>
金昔过6 2015-11-06
  • 打赏
  • 举报
回复
解决问题了吗?我是好久会报一个这个错误,求告知方法
三毛学Java 2015-04-13
  • 打赏
  • 举报
回复
这个问题是应为 你设置参数的时候 不要设置成object类型 不要就会报这个错误
全称LOVE 2015-03-20
  • 打赏
  • 举报
回复
问题解决了吗,求告知解决方法
道秋adol 2015-03-20
  • 打赏
  • 举报
回复
兄弟,你解决了么
90后的80人 2015-02-04
  • 打赏
  • 举报
回复
问题解决了吗?把resultType="com.gci.common.model.User"这个改成resultMap=“”试试
dounine 2014-12-05
  • 打赏
  • 举报
回复
把 parameterType="int" 去掉就可以了
鬼山断魂 2014-12-04
  • 打赏
  • 举报
回复
给id类型换成Integer试试
鬼山断魂 2014-12-04
  • 打赏
  • 举报
回复
给id类型换成Integer试试
鬼山断魂 2014-12-04
  • 打赏
  • 举报
回复
给id换成Integer试试
Jason-xie 2014-12-04
  • 打赏
  • 举报
回复
楼主这个问题解决了吗,我今天也碰到了这个问题,楼主是怎么解决的?
Jason-xie 2014-12-04
  • 打赏
  • 举报
回复
楼主这个问题解决了吗,我今天也碰到了这个问题,而且是有时候正常有时候不正常
Jason-xie 2014-12-04
  • 打赏
  • 举报
回复
楼主这个问题解决了吗,我今天也碰到了这个问题,而且是有时候正常有时候不正常
黄诚实_ 2014-09-29
  • 打赏
  • 举报
回复
ID= #{0}
恋上蓝白 2014-09-24
  • 打赏
  • 举报
回复
你的实体类写了构造方法了吗?写构造方法的话会报这个错,好像必须使用默认构造方法。
Dustin15 2014-07-21
  • 打赏
  • 举报
回复
查到原因了,这是因为进行sprinMVC+Mybatis框架大家的过程中,引入的ojdbc6.jar驱动包出错了,不知道是包有问题还是,包不兼容,反正换ojdbc14.jar就可以了
Dustin15 2014-07-15
  • 打赏
  • 举报
回复
引用 6 楼 u012345283 的回复:
parameterType="int"改成parameterType="java.lang.Integer"试试?
试过了,也不行哦!
加载更多回复(10)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧