iBatis - 标头详细问题

发布于 2024-08-17 14:35:46 字数 787 浏览 0 评论 0原文

我正在尝试将一些新功能放入使用IBATIS的现有应用程序中,但我有点受到设计决策的困扰。

有一个现有的类(称其为A类),我想添加一些新字段。这些字段将是B型。

查询将通过外部连接加入B。

因此,这将是

public class A {

   //... existing fields
   private List<B> bList; // may use a Map rather than a list?

   // etc.
}

public class B {
   private int id; // primary key
   private int type;
   private String description;

   // etc.

}

我在Web应用程序中使用它。在第一页上,我想返回“ A”的列表,然后在B旁边放置链接。

例如:

LinktoRecordA1 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA2 - LinktoB1          LinktoB3
LinktoRecordA3 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA4 

等。

(NB:记录A4与任何b的链接没有链接 - 因此上述外部联接)

在“ A”的初始获取中,我只想知道B记录存在,而它的主要键是通过B详细记录介绍链接。因此,我的问题是,如何在“ A”对象上创建填充的“ B”列表的情况下如何?

I'm trying to put some new functionality into an existing app that uses iBatis but I'm a little stuck with one of the design decisions.

There is an existing class (call it class A) which I want to add some new fields to. These fields will be of type B.

The query will join B via an outer join.

So it will be something like

public class A {

   //... existing fields
   private List<B> bList; // may use a Map rather than a list?

   // etc.
}

public class B {
   private int id; // primary key
   private int type;
   private String description;

   // etc.

}

I'm using this in a web app. On the first page I want to return a list of "A's", and then put links beside for the B's.

Eg:

LinktoRecordA1 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA2 - LinktoB1          LinktoB3
LinktoRecordA3 - LinktoB1 LinktoB2 LinktoB3
LinktoRecordA4 

etc.

(NB: Record A4 has no links to any B's - hence the outer join mentioned above)

In the initial fetch of the "A's", I only want to know that the B record exists, and what it's primary key is to present the link through to the B detail record. So my problem is, how do I do this without creating a fully populated list of "B's" on the "A" object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有一腔孤勇 2024-08-24 14:35:46

关于您的评论:

How else should I do it? I think what I'm having trouble understading is 
this- should I return a list of "A" objects with only minimal data 
populated, or would should I create some kind of new object?

在诸如您描述的情况下(返回列表并且用户可以从中选择某些内容时),我注意到用户通常会选择一个或两个记录以查看更多详细信息。在这种情况下,您最终返回了一个完全填充的对象的列表。

在那种情况下,我要做的就是从选择对象标识符而不是成熟的对象的后备。

因此,您可以仅使用此数据创建一个新类:

public class C {
  private Integer idForA;
  private List<Integer> listOfIdsForB = new ArrayList<Integer>();
  //...
}

然后,您将查询编写您的查询,以返回A和B的标识符,并返回类似的结果:

IDtoRecordA1 | IDtoB1 
IDtoRecordA1 | IDtoB2 
IDtoRecordA1 | IDtoB3

IDtoRecordA2 | IDtoB1          
IDtoRecordA2 | IDtoB3
IDtoRecordA3 | IDtoB1 

IDtoRecordA3 | IDtoB2 
IDtoRecordA3 | IDtoB3

IDtoRecordA4 | null

在此阶段resultmap标签派上用场,可以将结果转换为c对象包含以下方式的列表:

C1: IDtoRecordA1, [IDtoB1, IDtoB2, IDtoB3]
C2: IDtoRecordA2, [IDtoB1, IDtoB3]
C3: IDtoRecordA3, [IDtoB1, IDtoB2, IDtoB3]
C4: IDtoRecordA4, []

这对于避免n+1查询也非常有用。当您返回带有B列表的对象时,您可能已经在执行此操作,但是如果您使用单独的查询来检索列表(即select属性 actect> result tag),则比我建议您查看IBATIS数据映射器开发人员指南中的“避免n+1选择”部分,以最大程度地减少返回数据所需的查询数量。

底线是...

...如果您处理大量数据,则返回完整加载的对象只是为了显示少量信息,可能是过分的。

..另一方面,如果您处理少量数据,介绍了处理IDS的另一个类可能会使事物复杂化,因此您可能同样返回A和B的完整对象(但是避免n+1查询)。

Regarding your comment:

How else should I do it? I think what I'm having trouble understading is 
this- should I return a list of "A" objects with only minimal data 
populated, or would should I create some kind of new object?

In situations like the one you describe (when a list is returned and the user can select something from it), I have noticed that the user usually selects one or two records to see more details. In this case you end up returned a list of fully populated objects for nothing.

In that case, what I do, is fallback to just select object identifiers instead of full-fledged objects.

So you could create a new class with only this data:

public class C {
  private Integer idForA;
  private List<Integer> listOfIdsForB = new ArrayList<Integer>();
  //...
}

You then write your query with an outer join to return A's and B's identifiers and return a result similar to this:

IDtoRecordA1 | IDtoB1 
IDtoRecordA1 | IDtoB2 
IDtoRecordA1 | IDtoB3

IDtoRecordA2 | IDtoB1          
IDtoRecordA2 | IDtoB3
IDtoRecordA3 | IDtoB1 

IDtoRecordA3 | IDtoB2 
IDtoRecordA3 | IDtoB3

IDtoRecordA4 | null

At this stage the groupBy attribute of the resultMap tag comes in handy to transform the result in a list of C objects containing this:

C1: IDtoRecordA1, [IDtoB1, IDtoB2, IDtoB3]
C2: IDtoRecordA2, [IDtoB1, IDtoB3]
C3: IDtoRecordA3, [IDtoB1, IDtoB2, IDtoB3]
C4: IDtoRecordA4, []

This is also very useful in avoiding N+1 queries. You may already be doing this when you return A objects with list of B’s, but if you are using separate queries to retrieve the list (i.e select attribute on result tag), than I suggest you take a look at the "Avoiding N+1 Selects" sections in the iBatis Data Mapper developer guide, to minimize the number of queries needed to return the data.

Bottom line is...

...if you handle a large volume of data, then returning full loaded objects just to display a small amount of info, is probably overkill.

..on the other hand, if you handle a small amount of data, introducing another class that handles the IDs will probably complicate the things, so you might just as well return full objects of A’s and B’s (but avoiding N+1 queries of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文