iBATIS.NET字段映射自定义对象浅析

开发 后端
iBATIS.NET字段映射是什么呢?本文向你介绍如何使用iBATIS.NET把字段映射成自定义对象。

iBATIS.NET字段映射是什么意思呢?在iBATIS.NET中,查询后的结果会自动将每一个字段映射成Domain中的一个属性值,这个映射的过程是通过TypeHandlerFactory类进行的,在程序初始化时注册了一些系统类和类型转换类之间的关系:

  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); 

那么如果想将数据库中的一个字段映射成我们自己的一个类,在这个类中进行一些个性化处理,应该怎么办呢?

本来我想仿照StringTypeHandler类写一个自己的类型处理类,但是通过查看iBATIS的源代码,就算写好了自己的类型处理类,好像也找不到注册的接口(如果哪位兄弟找到了接口,望告知)

另一种方式是通过已经注册的CustomTypeHandler类型,实行其中的ITypeHandlerCallback接口来实现的,具体实现方式如下:

我这里实现的只是一个演示程序,演示将数据库中的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、

实现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方法,实现和数据库之间的存取操作。

iBATIS.NET字段映射3、

修改对应的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属性来指定一个类型转换器。

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

【编辑推荐】

  1. iBATIS.NET中两大常用的DAO浅谈
  2. iBATIS.NET数据库缓存模式浅析
  3. iBATIS.NET常用的查询方式浅析
  4. iBATIS.NET中的多表查询方法浅析
  5. iBATIS.NET日志处理浅析
责任编辑:仲衡 来源: cnblogs
相关推荐

2009-07-22 09:07:01

iBATIS.NET

2009-07-20 13:22:47

iBATIS.Net日

2009-07-20 14:56:18

iBATIS.NET动态选择DAO

2009-07-20 10:06:07

iBATIS.net查询方式

2009-07-21 13:50:00

iBATIS.NET调

2009-07-21 15:21:59

iBATIS.NET多

2009-07-20 09:51:19

iBATIS.net数据库缓存

2009-07-20 15:14:44

iBATIS.NET连

2009-07-21 16:30:15

iBATIS.NET与单元测试

2009-07-15 17:58:07

iBATIS 动态映射

2009-07-16 13:50:31

ibatisResultMap

2009-08-10 14:16:59

ASP.NET自定义控

2009-07-28 09:32:41

ASP.NET自定义控

2009-08-06 17:13:56

ASP.NET自定义控

2009-07-21 17:06:35

iBATIS.NET执

2009-07-22 14:28:52

iBATIS.NET配

2009-11-12 16:14:28

ADO.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
点赞
收藏

51CTO技术栈公众号