打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
合理运用remapResults属性
Proper Usage of remapResults="true"

The remapResults attribute is availableon 【statement】, 【select】, and 【procedure】 mapped statements. It is anoptional attribute and the default value is false.

The remapResults attribute should be setto 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 bedifferent. Because of the usage of * in the SELECT clause, theresulting columns names could be different, as well.
在这个例子中,因为在Select中使用了*匹配符,所以查询的列会随着表名的不同而发生变化。

Since the overhead to introspectthe result set metadata is not trivial, iBATIS will remember what wasreturned the last time the query was run. This could create problems insituations 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 byassuming that fld1 and fld2 will always be in the result set on eachsubsequent 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 findfld1 and fld2 in the result set,
thus returning improper results, iBATIS won‘t know about fld3 and fld4because 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 setmetadata every time the query is run and will always return the properresults. This feature comes at some performance cost, so only use it ifyou really need it – when the columns in the result set are variable,either directly, like in the first example, or indirectly, because of avariable table.

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis Generator 详解
学用HTML代码做帖
Delphi XE6 FireDAC
Bootstrap Table
Struts2学习笔记(五):Action属性注入值和指定Struts 2处理的请求
PHPMyadmin配置文件详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服