Expected one result (or null) to be returned by selectOne(), but found: 2
Mybatis+sql出现以上问题时,说明sql只允许查询一个值 selectOne, 但是查询的结果有两条。
这时候你就要去你的sql中找到sql语句
select
a.id as "id",
a.name as "name",
a.original_price as "original_price",
a.price as "price",
a.content as "content",
a.sales as "sales",
a.classify as "classify",
a.specification as "specification",
a.unit as "unit",
a.rxsp as "rxsp",
a.remark as "remark",
a.tjr as "tjr",
a.tjsj as "tjsj",
a.xgsj as "xgsj",
b.tpdz as "tpdz",
ifnull(c.sl,0) as "sl"
from mall_goods a
left join mall_goods_photo b on b.goods_id=a.id
left join mall_goods_cart c on a.id=c.goods_id and c.zh=#{zh}
where a.id=#{id}
<if test="sfmr != null and sfmr != ''">
and b.sfmr=#{sfmr}
</if>
<if test="name != null and name != ''">
and b.name=#{name}
</if>
group by a.id
<if test="goods_id != null and goods_id != ''">
and b.goods_id=#{goods_id}
</if>
大概位置在where a.id=#{id}的地方
解决方法:
在where id=#{id}的下面 加一句 group by a.id 然后问题应该就不会报错了, 问题解决了.
本文介绍了一个常见的 MyBatis + SQL 错误:“Expected one result (or null) to be returned by selectOne()...”的问题及解决办法。错误原因是期望通过 selectOne 方法返回单一结果时却查到了多条记录。文章详细解释了如何定位问题,并提供了一种通过增加 GROUP BY 子句来解决此问题的方法。
1568

被折叠的 条评论
为什么被折叠?



