gpt4 book ai didi

java - 用iBatis映射父子关系

转载 作者:太空宇宙 更新时间:2023-11-04 08:55:12 24 4
gpt4 key购买 nike

我有经典设置

public class Parent {
Integer id;
...
// No generics
Collection someCollectionAttribute;
...
public void setChildren(Collection c) {
...
}
}

public class Child {
Integer id;
...
}

我正在尝试使用 iBatis(版本 2.30...不要问)将其映射到常用的表设置上。

create table parents (
ID integer primary key
...
)

create table children (
ID integer primary key
PARENT_ID integer references parents(id)
...
)

我的映射文件如下所示

<resultMap id="ParentResult" groupBy="id">
<result property="id" column="ID" />
...
<result property="children" resultMap="ChildResult" />
</resultMap>

<resultMap id="ChildResult">
<result property="id" column="ID" />
<result property="parentId" column="PARENT_ID" />
...
</result>

<sql id="loadParent" resultMap="ParentResult">
select P.ID as p1, ..., C.ID as c1, C.PARENT_ID as c2 ...
from parents P
join children C on (P.ID = C.PARENT_ID)
where P.ID = #id#
order by P.ID
</sql>

执行通常的 sqlMap.queryForObject("loadParent", new Integer(42)) 起初会在 setChildren setter 中导致 NullPointerException,显然是使用 null 参数调用的(我的错)。修复 setter 一切工作正常,但日志显示在运行单个 SQL 语句之前仅调用 setChildren 一次,仍然带有 null 参数,所以我想知道这里发生了什么。有人有任何线索吗?

最佳答案

我认为问题在于结果集中的列名(指定的别名)与 iBatis 结果映射中的映射不匹配。您的示例看起来不完整,所以我无法准确判断。

无论如何,以下是一个与您的设置相匹配的完整工作示例(使用 iBatis 2.3.0.677 进行测试)。如果你将它与你拥有的进行比较,也许你就会找出问题所在。

数据库表:

create table parents (
ID integer primary key,
BLA VARCHAR(100)
);

insert into parents(id, bla) values
(1, 'text1'),
(2, 'text2');

create table children (
ID integer primary key,
PARENT_ID integer references parents(id),
BLA_CHILD VARCHAR(100)
);

insert into children(id, parent_id, bla_child) values
(10, 1, 'child for 1 1'),
(11, 1, 'child for 1 2'),

(12, 2, 'child for 2 1'),
(13, 2, 'child for 2 2'),
(14, 2, 'child for 2 3'),
(15, 2, 'child for 2 4');

bean 类:

package model.pack;
import java.util.Collection;
public class Parent {
private Integer id;
private String bla;
private Collection children;

public Collection getChildren() {
return children;
}
public void setChildren(Collection children) {
this.children = children;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBla() {
return bla;
}
public void setBla(String bla) {
this.bla = bla;
}
}

package model.pack;
public class Child {
private Integer id;
private Integer parentId;
private String bla;

public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBla() {
return bla;
}
public void setBla(String bla) {
this.bla = bla;
}
}

SQL 映射:

<sqlMap>
<resultMap id="childResult" class="model.pack.Child">
<result property="id" column="childId" />
<result property="parentId" column="childParentId" />
<result property="bla" column="childBla" />
</resultMap>

<resultMap id="parentResult" class="model.pack.Parent" groupBy="id">
<result property="id" column="parentId" />
<result property="bla" column="parentBla" />
<result property="children" resultMap="childResult" />
</resultMap>

<select id="loadParent" parameterClass="int" resultMap="parentResult">
select p.id as parentId, p.bla as parentBla, c.id as childId,
c.bla_child as childBla, c.parent_id as childParentId
from parents p left outer join children c
on c.parent_id = p.id
where p.id = #id#
order by p.id
</select>
</sqlMap>

如果您随后运行 sqlMap.queryForObject("loadParent", new Integer(1))sqlMap.queryForObject("loadParent", new Integer(2)),您应该分别获得一个具有两个子级的父对象和一个具有四个子级的父级对象。

关于java - 用iBatis映射父子关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2447891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com