求帮助,感谢
2023/5/14
问题:nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2
预计返回一个结果(或 null),但发现2个
这个错误表示在使用MyBatis进行数据库操作时,一个查询返回了多个结果,但MyBatis无法确定要返回哪一个结果,所以抛出了TooManyResultsException异常。
这种情况通常出现在:
xml
<select ... resultType="com.domain.User">
SELECT * FROM USER
</select>
java
@Results(id = "user", value = {
@Result(property = "id", column = "uid"),
@Result(property = "name", column = "username")
})
@Select("SELECT * FROM USER")
User findUser();
xml
<select ...>
SELECT
uid AS "id",
username AS "name"
FROM USER
</select>
<resultMap id="user" type="com.domain.User">
<id property="id" column="id" />
<result property="name" column="name" />
</resultMap>