资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

如何浅析iBATIS.NET字段映射自定义对象,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司是一家专注于成都网站设计、成都做网站、外贸网站建设与策划设计,平陆网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:平陆等地区。平陆做网站价格咨询:13518219792

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

handler = new NullableBooleanTypeHandler();  this.Register(typeof(bool?), handler);   handler = new NullableByteTypeHandler();  this.Register(typeof(byte?), handler);   handler = new NullableCharTypeHandler();  this.Register(typeof(char?), handler);   handler = new NullableDateTimeTypeHandler();  this.Register(typeof(DateTime?), handler);   handler = new NullableDecimalTypeHandler();  this.Register(typeof(decimal?), handler);   handler = new NullableDoubleTypeHandler();  this.Register(typeof(double?), handler);   handler = new NullableGuidTypeHandler();  this.Register(typeof(Guid?), handler);   handler = new NullableInt16TypeHandler();  this.Register(typeof(Int16?), handler);   handler = new NullableInt32TypeHandler();  this.Register(typeof(Int32?), handler);   handler = new NullableInt64TypeHandler();  this.Register(typeof(Int64?), handler);   handler = new NullableSingleTypeHandler();  this.Register(typeof(Single?), handler);   handler = new NullableUInt16TypeHandler();  this.Register(typeof(UInt16?), handler);   handler = new NullableUInt32TypeHandler();  this.Register(typeof(UInt32?), handler);   handler = new NullableUInt64TypeHandler();  this.Register(typeof(UInt64?), handler);   handler = new NullableSByteTypeHandler();  this.Register(typeof(SByte?), handler);   handler = new NullableTimeSpanTypeHandler();  this.Register(typeof(TimeSpan?), handler);

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

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

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

我这里实现的只是一个演示程序,演示将数据库中的Account_LastName和Account_Email字段映射成自定义的Property类型,同时把它们放入一个Hashtable中。

iBATIS.NET字段映射1、

自定义Property类

namespace GSpring.Common  {      public class Property      {          private string _dataValue;           public string DataValue          {              get { return _dataValue; }              set { _dataValue = value; }          }           private string _dataType;           public string DataType          {              get { return _dataType; }              set { _dataType = value; }          }      }  }

iBATIS.NET字段映射2、

实现ITypeHandlerCallback接口的类

namespace GSpring.Common  {      public sealed class PropertyTypeHandler : ITypeHandlerCallback      {           public object ValueOf(string Value)          {              Property obj = new Property();              obj.DataValue = Value;              return obj;          }           public object GetResult(IResultGetter getter)          {              Property obj = new Property();              if (getter.Value != null && getter.Value != System.DBNull.Value)              {                  obj.DataValue = (string)getter.Value;              }              return obj;          }           public void SetParameter(IParameterSetter setter, object parameter)          {              setter.Value = ((Property)parameter).DataValue;          }           public object NullValue          {              get { return null; }          }      }   }

主要是其中的GetResult和SetParameter方法,实现和数据库之间的存取操作。

iBATIS.NET字段映射3、

修改对应的Domain类,加入两个属性:

public Hashtable ht = new Hashtable();  Property _emailAddress1 = new Property();  public Property EmailAddress1  {      get      {          return _emailAddress1;      }      set      {          _emailAddress1.DataType = "string";          _emailAddress1.DataValue = value.DataValue;          ht["邮件"] = _emailAddress1;      }  }   Property _lastName1 = new Property();  public Property LastName1  {      get      {          return _lastName1;      }      set      {          _lastName1.DataType = "string";          _lastName1.DataValue = value.DataValue;          ht["姓名"] = _lastName1;      }  }

iBATIS.NET字段映射4、

修改配置文件:

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

主要是利用了其中的typeHandler属性来指定一个类型转换器。

关于如何浅析iBATIS.NET字段映射自定义对象问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站题目:如何浅析iBATIS.NET字段映射自定义对象
链接地址:http://cdkjz.cn/article/jpephi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220