mybatis-org.apache.ibatis.executor.ExecutorException

在使用mybatis进行一对多嵌套查询时, 报错如下

org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.
The error may exist in com/bigdata/mapper/UserMapper.xml
The error may involve com.bigdata.mapper.UserMapper.findAllOrders
The error occurred while handling results
SQL: select * from orders where uid = ?
Cause: org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected.

错误涉及代码如下
mybatis-org.apache.ibatis.executor.ExecutorException_第1张图片

错误大体是说, 箭头指向的sql语句返回的数据行数大于1.
这个在业务上是OK的, 因为orders表是订单表, 一个用户可能有多个订单.
但是在这里结果多行这件事被作为错误对待, 说明应该是哪里没有告知mybatis应该使用集合(或数组)承接结果

最终查找到问题在, property属性值在com.bigdata.domain.User的定义中是Orders, 而不是可以进行遍历的List类型
mybatis-org.apache.ibatis.executor.ExecutorException_第2张图片

所以处理方式也很明显了, 将该成员变量改为可迭代类型, 将相应地修改get/set方法以及toString()方法即可

你可能感兴趣的:(mybatis)