Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从myBatis(Java)返回列表(对象内部)_Java_Mybatis - Fatal编程技术网

从myBatis(Java)返回列表(对象内部)

从myBatis(Java)返回列表(对象内部),java,mybatis,Java,Mybatis,这里我需要一些帮助,我正在尝试使用myBatis返回父对象内部的对象列表 问题是: 在开始阅读下面的代码之前,我得到的错误是: SqlSession operation; nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4102 这里有趣的是,这意味着我

这里我需要一些帮助,我正在尝试使用myBatis返回父对象内部的对象列表

问题是: 在开始阅读下面的代码之前,我得到的错误是:

SqlSession operation; nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4102
这里有趣的是,这意味着我正在访问存储过程,看到了存储过程中的数据量,并且由于myBatis认为我使用的是selectOne(),结果太多而出错-这不是真的吗?我要补充的是,4102是我试图从中提取数据的表中记录的确切数量

我的代码: 以下是父对象和子对象的结果映射:

<resultMap id="ParentObjectMap" type="com.company.product.mybatis.model.ParentObject">
    <collection property="children" resultMap="childrenMap"/>
</resultMap>

<resultMap id="childrenMap" type="com.company.product.mybatis.model.ChildObject">
    <id column="ChildId" jdbcType="BIGINT" property="childId" />
    <result column="Name" jdbcType="VARCHAR" property="name" />
</resultMap>
下面是我正在调用的存储过程,其目的是返回数据:

ALTER PROCEDURE dbo.PR_Children_Get
AS

SET NOCOUNT ON

BEGIN
    SELECT 
        tp.ChildId,
        tp.Name
    FROM dbo.Children tp WITH (NOLOCK)
END
GO
下面是我如何在映射器中执行该过程:

<select id="getChildren" resultMap="ParentObjectMap">
    exec [dbo].[PR_Children_Get]
</select>
@Override
public ParentObject getChildren() throws Exception {
    ParentObject result = ParentObjectMapper.getChildren();
    return result;
}
以下是该ParentObjectMapper的接口:

/* Hiding imports for brevity */

public interface ParentObjectMapper {

    // Get the list children, the list should be a property within the parent object.
    ParentObject getChildren();
}

我假设您通过映射器接口执行,方法是:
ParentObject getChildren()这显式地期望一个结果,因为这不是一个集合类型,然后是您得到的错误

但我不是告诉你应该改成
List getChildren()要避免此错误。。。或者你应该。。。有一段时间,因为这有助于理解问题:


在代码中,Mybatis会为每个结果行创建一个新的ParentObject,因为parentId永远不会返回,Mybatis也不会假设您只有一个父对象。然后查询将返回一个parentId列,ParentObjectMap以
开头,这样Mybatis就知道如何按父对象对子对象进行分组。

您使用的是什么数据库?使用SQL databaseHi@Black向导,我已经为我的mapper接口添加了代码,这应该表明我是如何使用实际的mapper XML的——很抱歉没有包括这一点,我原以为我已经做到了!你知道我做错了什么吗?调用
parentobjectresult=ParentObjectMapper.getChildren()
有误导性:它看起来像是对接口的静态调用!?我想这只是一个输入错误,工作代码看起来更像
parentobjectresult=this.mapper.getChildren()总之,我的回答是:你需要选择并映射parentId,这样Mybatis就可以按父项对子项进行分组。。。尽管只有一个父对象,但我对此有点困惑——因为我不是从数据库中获取父对象,而是在运行时创建它,然后填充其中的列表——如果我完全废弃父对象会更好吗?有没有办法把名单拿回来?因为我其实不需要有父母。。。感谢您在这方面的帮助:)然后
getChildren
应该返回
List
,只需
parent.setChildrenMap(childrenList)
;非常感谢!!这让我找回了我的数据!!你在myBatis的几个问题上帮了我大忙!再次感谢!
@Override
public ParentObject getChildren() throws Exception {
    ParentObject result = ParentObjectMapper.getChildren();
    return result;
}
/* Hiding imports for brevity */

public interface ParentObjectMapper {

    // Get the list children, the list should be a property within the parent object.
    ParentObject getChildren();
}