论坛首页 Java企业应用论坛

合理运用remapResults属性

浏览 10210 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-16  
Proper Usage of remapResults="true"

The remapResults attribute is available on 【statement】, 【select】, and 【procedure】 mapped statements. It is an optional attribute and the default value is false.

The remapResults attribute should be set to true when a query has a variable set of return columns. For example, consider the following queries:

在 【statement】, 【select】, 和【procedure】 标签中存在一个可选的属性【remapResults】,默认值是false.
如果每次查询的列不定的话,这个属性需要设置为true.如下所例:

xml 代码
  1. SELECT $fieldList$   
  2.   FROM table  
In this example, the list of column names is dynamic, even though the table is always the same.
在这个例子中,虽然检索的是同一张表,可是每次查询的列却是可变的。
xml 代码
  1. SELECT *   
  2.   FROM $someTable$  

In this example, the table could be different. Because of the usage of * in the SELECT clause, the resulting columns names could be different, as well.
在这个例子中,因为在Select中使用了*匹配符,所以查询的列会随着表名的不同而发生变化。

Since the overhead to introspect the result set metadata is not trivial, iBATIS will remember what was returned the last time the query was run. This could create problems in situations similar to the examples above.
为了避免经常的对返回的结果进行内省,iBATIS会记录上一次查询结果的元数据,这样,在遇到上面的例子时就会出现问题

 


 

Let's consider what iBATIS will do for the first example depending on the usage of remapResults.

让我们了解一下,iBATIS是如何依赖remapResults属性的。

Without remapResults, or remapResults="false":

当没有设置remapResults属性,或者设置remapResults为false时:

Let's say $fieldList$ is set to "fld1, fld2" the first time the query is executed, thus giving the query:
假设在第一次查询时,$fieldList$设置为"fld1, fld2" ,查询语句等效下面的SQL:

xml 代码
  1. SELECT fld1, fld2   
  2.   FROM table  

iBATIS will try to be efficient by assuming that fld1 and fld2 will always be in the result set on each subsequent execution of the query.
The application will run into trouble if the value for $fieldList$ changes, such as "fld3, fld4". Not only will iBATIS be unable to find fld1 and fld2 in the result set,
thus returning improper results, iBATIS won't know about fld3 and fld4 because they weren't in the query on its initial execution.

iBATIS为了效率,假设这条SQL语句今后的查询结果都会返回fld1和fld2列。
如果后面$fieldList$发生变化,比如设置为 "fld3, fld4",程序将会遇到一些麻烦。不仅仅是iBATIS找不到fld1和fld2列那么简单,
麻烦的是,iBATIS也不会识别fld3和fld4,因为在SQL初始化的时候,这两列并没有包括在内。

With remapResults="true":

iBATIS will introspect the result set metadata every time the query is run and will always return the proper results. This feature comes at some performance cost, so only use it if you really need it – when the columns in the result set are variable, either directly, like in the first example, or indirectly, because of a variable table.

当设置remapResults为"true"时:
iBATIS会在每次查询的时候内省查询结果来设置元数据,来保证返回恰当的结果。这个属性会造成一定的性能损失,所以要谨慎使用,只在你需要的时候使用--查询列发生变化,直接的,如第一个例子一样,或者隐含的,如第二个例子,检索的表发生变化。

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics