O/R映射框架的延遲加載技術實現大體上有這么4種(參看Martin Fowler的意見):
(http://www.martinfowler.com/eaaCatalog/lazyLoad.html)
There are four main varieties of lazy load. Lazy Initialization uses a special marker value (usually null) to indicate a field isn't loaded. Every access to the field checks the field for the marker value and if unloaded, loads it. Virtual Proxy is an object with the same interface as the real object. The first time one of its methods are called it loads the real the object and then delegates. Value Holder is an object with a getValue method. Clients call getValue to get the real object, the first call triggers the load. A ghost is the real object without any data. The first time you call a method the ghost loads the full data into its fields.
通過閱讀源代碼,發現iBATIS中的延遲加載是用上述方式中的虛擬代理實現的.
在動態代理的實現上, iBATIS有Java動態代理和CGLIB兩種實現方案,iBATIS把用CGLIB實現的方案稱為Enhanced的方案,可見CGLIB的效率會比java的動態代理效率要高.
在iBATIS首先判斷是否定義了延遲加載,如果定義了,則利用Lazy的Loader來提取數據(返回一個Proxy).如沒有執行對這個的任何操作,或者只是不再使用(finalize),則不做處理,否者就加載真正的對象.
可以通過閱讀類
com.ibatis.sqlmap.engine.mapping.result.loader.LazyResultLoader
的源碼獲取更多的細節.
@2008 楊一. 版權所有. 保留所有權利