偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

iBATIS.NET字段映射自定義對(duì)象淺析

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

iBATIS.NET字段映射是什么意思呢?在iBATIS.NET中,查詢后的結(jié)果會(huì)自動(dòng)將每一個(gè)字段映射成Domain中的一個(gè)屬性值,這個(gè)映射的過(guò)程是通過(guò)TypeHandlerFactory類(lèi)進(jìn)行的,在程序初始化時(shí)注冊(cè)了一些系統(tǒng)類(lèi)和類(lèi)型轉(zhuǎn)換類(lèi)之間的關(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ù)庫(kù)中的一個(gè)字段映射成我們自己的一個(gè)類(lèi),在這個(gè)類(lèi)中進(jìn)行一些個(gè)性化處理,應(yīng)該怎么辦呢?

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

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

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

iBATIS.NET字段映射1、

自定義Property類(lèi)

  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、

實(shí)現(xiàn)ITypeHandlerCallback接口的類(lèi)

  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方法,實(shí)現(xiàn)和數(shù)據(jù)庫(kù)之間的存取操作。

iBATIS.NET字段映射3、

修改對(duì)應(yīng)的Domain類(lèi),加入兩個(gè)屬性:

  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屬性來(lái)指定一個(gè)類(lèi)型轉(zhuǎn)換器。

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

【編輯推薦】

  1. iBATIS.NET中兩大常用的DAO淺談
  2. iBATIS.NET數(shù)據(jù)庫(kù)緩存模式淺析
  3. iBATIS.NET常用的查詢方式淺析
  4. iBATIS.NET中的多表查詢方法淺析
  5. iBATIS.NET日志處理淺析
責(zé)任編輯:仲衡 來(lái)源: 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-21 13:50:00

iBATIS.NET調(diào)

2009-07-20 14:56:18

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

2009-07-21 15:21:59

iBATIS.NET多

2009-07-20 09:51:19

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

2009-07-20 15:14:44

iBATIS.NET連

2009-07-21 16:30:15

iBATIS.NET與單元測(cè)試

2009-07-15 17:58:07

iBATIS 動(dòng)態(tài)映射

2009-07-16 13:50:31

ibatisResultMap

2009-08-06 17:13:56

ASP.NET自定義控

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-10 14:16:59

ASP.NET自定義控

2009-07-22 14:28:52

iBATIS.NET配

2009-07-21 17:06:35

iBATIS.NET執(zhí)

2009-07-22 14:11:09

配置ibatis.neiBatis.net配

2009-11-12 16:14:28

ADO.NET自定義對(duì)

2009-07-20 15:27:22

Castle.DynaiBATIS.NET

2009-07-21 14:15:00

iBATIS.NET多
點(diǎn)贊
收藏

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