自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iBATIS.NET字段映射自定義對象淺析

開發(fā) 后端
iBATIS.NET字段映射是什么呢?本文向你介紹如何使用iBATIS.NET把字段映射成自定義對象。

iBATIS.NET字段映射是什么意思呢?在iBATIS.NET中,查詢后的結(jié)果會自動將每一個字段映射成Domain中的一個屬性值,這個映射的過程是通過TypeHandlerFactory類進(jìn)行的,在程序初始化時注冊了一些系統(tǒng)類和類型轉(zhuǎn)換類之間的關(guān)系:

  1. handler = new NullableBooleanTypeHandler();  
  2. this.Register(typeof(bool?), handler);  
  3.  
  4. handler = new NullableByteTypeHandler();  
  5. this.Register(typeof(byte?), handler);  
  6.  
  7. handler = new NullableCharTypeHandler();  
  8. this.Register(typeof(char?), handler);  
  9.  
  10. handler = new NullableDateTimeTypeHandler();  
  11. this.Register(typeof(DateTime?), handler);  
  12.  
  13. handler = new NullableDecimalTypeHandler();  
  14. this.Register(typeof(decimal?), handler);  
  15.  
  16. handler = new NullableDoubleTypeHandler();  
  17. this.Register(typeof(double?), handler);  
  18.  
  19. handler = new NullableGuidTypeHandler();  
  20. this.Register(typeof(Guid?), handler);  
  21.  
  22. handler = new NullableInt16TypeHandler();  
  23. this.Register(typeof(Int16?), handler);  
  24.  
  25. handler = new NullableInt32TypeHandler();  
  26. this.Register(typeof(Int32?), handler);  
  27.  
  28. handler = new NullableInt64TypeHandler();  
  29. this.Register(typeof(Int64?), handler);  
  30.  
  31. handler = new NullableSingleTypeHandler();  
  32. this.Register(typeof(Single?), handler);  
  33.  
  34. handler = new NullableUInt16TypeHandler();  
  35. this.Register(typeof(UInt16?), handler);  
  36.  
  37. handler = new NullableUInt32TypeHandler();  
  38. this.Register(typeof(UInt32?), handler);  
  39.  
  40. handler = new NullableUInt64TypeHandler();  
  41. this.Register(typeof(UInt64?), handler);  
  42.  
  43. handler = new NullableSByteTypeHandler();  
  44. this.Register(typeof(SByte?), handler);  
  45.  
  46. handler = new NullableTimeSpanTypeHandler();  
  47. this.Register(typeof(TimeSpan?), handler); 

那么如果想將數(shù)據(jù)庫中的一個字段映射成我們自己的一個類,在這個類中進(jìn)行一些個性化處理,應(yīng)該怎么辦呢?

本來我想仿照StringTypeHandler類寫一個自己的類型處理類,但是通過查看iBATIS的源代碼,就算寫好了自己的類型處理類,好像也找不到注冊的接口(如果哪位兄弟找到了接口,望告知)

另一種方式是通過已經(jīng)注冊的CustomTypeHandler類型,實行其中的ITypeHandlerCallback接口來實現(xiàn)的,具體實現(xiàn)方式如下:

我這里實現(xiàn)的只是一個演示程序,演示將數(shù)據(jù)庫中的Account_LastName和Account_Email字段映射成自定義的Property類型,同時把它們放入一個Hashtable中。

iBATIS.NET字段映射1、

自定義Property類

  1. namespace GSpring.Common  
  2. {  
  3.     public class Property  
  4.     {  
  5.         private string _dataValue;  
  6.  
  7.         public string DataValue  
  8.         {  
  9.             get { return _dataValue; }  
  10.             set { _dataValue = value; }  
  11.         }  
  12.  
  13.         private string _dataType;  
  14.  
  15.         public string DataType  
  16.         {  
  17.             get { return _dataType; }  
  18.             set { _dataType = value; }  
  19.         }  
  20.     }  

iBATIS.NET字段映射2、

實現(xiàn)ITypeHandlerCallback接口的類

  1. namespace GSpring.Common  
  2. {  
  3.     public sealed class PropertyTypeHandler : ITypeHandlerCallback  
  4.     {  
  5.  
  6.         public object ValueOf(string Value)  
  7.         {  
  8.             Property obj = new Property();  
  9.             obj.DataValue = Value;  
  10.             return obj;  
  11.         }  
  12.  
  13.         public object GetResult(IResultGetter getter)  
  14.         {  
  15.             Property obj = new Property();  
  16.             if (getter.Value != null && getter.Value != System.DBNull.Value)  
  17.             {  
  18.                 obj.DataValue = (string)getter.Value;  
  19.             }  
  20.             return obj;  
  21.         }  
  22.  
  23.         public void SetParameter(IParameterSetter setter, object parameter)  
  24.         {  
  25.             setter.Value = ((Property)parameter).DataValue;  
  26.         }  
  27.  
  28.         public object NullValue  
  29.         {  
  30.             get { return null; }  
  31.         }  
  32.     }  
  33.  

主要是其中的GetResult和SetParameter方法,實現(xiàn)和數(shù)據(jù)庫之間的存取操作。

iBATIS.NET字段映射3、

修改對應(yīng)的Domain類,加入兩個屬性:

  1. public Hashtable ht = new Hashtable();  
  2. Property _emailAddress1 = new Property();  
  3. public Property EmailAddress1  
  4. {  
  5.     get  
  6.     {  
  7.         return _emailAddress1;  
  8.     }  
  9.     set  
  10.     {  
  11.         _emailAddress1.DataType = "string";  
  12.         _emailAddress1.DataValue = value.DataValue;  
  13.         ht["郵件"] = _emailAddress1;  
  14.     }  
  15. }  
  16.  
  17. Property _lastName1 = new Property();  
  18. public Property LastName1  
  19. {  
  20.     get  
  21.     {  
  22.         return _lastName1;  
  23.     }  
  24.     set  
  25.     {  
  26.         _lastName1.DataType = "string";  
  27.         _lastName1.DataValue = value.DataValue;  
  28.         ht["姓名"] = _lastName1;  
  29.     }  

iBATIS.NET字段映射4、

修改配置文件:

  1. ﹤resultMap id="account-result"  class="Account" ﹥  
  2.     ﹤result property="Id"           column="Account_ID"/﹥  
  3.     ﹤result property="FirstName"    column="Account_FirstName"/﹥  
  4.     ﹤result property="LastName1"     column="Account_LastName"  typeHandler="GSpring.Common.PropertyTypeHandler"/﹥  
  5.     ﹤result property="EmailAddress1" column="Account_Email" typeHandler="GSpring.Common.PropertyTypeHandler"/﹥  
  6. ﹤/resultMap﹥ 

主要是利用了其中的typeHandler屬性來指定一個類型轉(zhuǎn)換器。

以上就是iBATIS.NET字段映射的一些基本情況,希望對你有所幫助。

【編輯推薦】

  1. iBATIS.NET中兩大常用的DAO淺談
  2. iBATIS.NET數(shù)據(jù)庫緩存模式淺析
  3. iBATIS.NET常用的查詢方式淺析
  4. iBATIS.NET中的多表查詢方法淺析
  5. iBATIS.NET日志處理淺析
責(zé)任編輯:仲衡 來源: cnblogs
相關(guān)推薦

2009-07-22 09:07:01

iBATIS.NET

2009-07-20 13:22:47

iBATIS.Net日

2009-07-20 10:06:07

iBATIS.net查詢方式

2009-07-20 14:56:18

iBATIS.NET動態(tài)選擇DAO

2009-07-21 13:50:00

iBATIS.NET調(diào)

2009-07-21 15:21:59

iBATIS.NET多

2009-07-20 09:51:19

iBATIS.net數(shù)據(jù)庫緩存

2009-07-20 15:14:44

iBATIS.NET連

2009-07-21 16:30:15

iBATIS.NET與單元測試

2009-07-15 17:58:07

iBATIS 動態(tài)映射

2009-07-16 13:50:31

ibatisResultMap

2009-07-21 17:06:35

iBATIS.NET執(zhí)

2009-07-22 14:28:52

iBATIS.NET配

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-10 14:16:59

ASP.NET自定義控

2009-08-06 17:13:56

ASP.NET自定義控

2009-07-22 14:11:09

配置ibatis.neiBatis.net配

2009-07-21 14:15:00

iBATIS.NET多

2009-07-20 15:27:22

Castle.DynaiBATIS.NET

2009-07-17 17:57:20

NPetShop iBATIS.Net
點贊
收藏

51CTO技術(shù)棧公眾號