您的位置:首页 > 其它

IBatis.Net学习笔记八--把字段映射成一个自定义对象

2007-09-19 09:44 316 查看
在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中。
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; }


}


}


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


}


}
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属性来指定一个类型转换器。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐