org.apache.ibatis.exceptions.PersistenceException:记录mybatis 查询结果映射异常

4 篇文章 0 订阅
3 篇文章 0 订阅

mybatis 查询返回值映射异常

具体异常

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set.  Cause: java.sql.SQLException: Invalid value for getInt() - '张三'
### The error may exist in com/conlin/mapper/UserMapper.xml
### The error may involve com.conlin.mapper.UserMapper.findAll
### The error occurred while handling results
### SQL: select * from mybatis.user
### Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set.  Cause: java.sql.SQLException: Invalid value for getInt() - '张三'

异常理解

异常信息表示,在查询结果中name值映射为int类型的值,如下:

Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'name' from result set.  Cause: java.sql.SQLException: Invalid value for getInt() - '张三'

原因是在实体中加了有参的构造方法,而未实现无参构造方法或全参构造方法。

解决方法

这个异常主要是因为实体属性与sql结果无法对应导致。
1、实现无参构造或全参构造,这样用 select * from mybatis.user 这个sql 查出所用字段将与user属性一一对应。
2、在SQL中将 * 替换为需要的字段,与实体中的构造方法参数对应,如下:

  <select id="findAll" resultType="com.conlin.entity.User">
        select name,age from mybatis.user
  </select>
public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

这样的话也可以解决上面的异常,但是查询结果映射为实体后,其他没有对应的字段将为空值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值