项目场景
今天在调试项目的时候,控制台出现了如下的报错信息,从下面报错信息可以明白大致的意思是:出现了过多的结果返回值,只希望得到一个值,但是现在返回了2个

问题描述
通过控制台打印的SQL
,在数据库里面执行,显然出现两个结果返回值。
select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from sys_post where post_code = 'zongjingli';

再来看看我们定义的接口是怎样的?
SysPostMapper.java
SysPost selectPostByPostCode(String postCode);
SysPostMapper.xml
<select id="selectPostByPostCode" parameterType="String" resultMap="SysPostResult">
<include refid="selectPostVo"/>
where post_code = #{postCode}
</select>
原因分析
通过SQL
查询出来的结果用于接收的是单个对象,但SQL
查询出来的结果却是一个列表(多个) 导致无法接收所有的查询结果值。
解决方案
下面给予了两种解决方案:
-
把 Mybatis
对应的mapper
接口的返回值修改为List
List<SysPost> selectPostByPostCode(String postCode);
-
根据业务场景对查询 SQL
语句做返回数量的限制
select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from
sys_post where post_code = 'zongjingli' limit 1;

原文始发于微信公众号(SimpleMemory):【避坑指南】记录项目中使用Mybatis遇到的BUG
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137783.html