iBATIS.NET字段映射自定義對象淺析
iBATIS.NET字段映射是什么意思呢?在iBATIS.NET中,查詢后的結(jié)果會自動將每一個字段映射成Domain中的一個屬性值,這個映射的過程是通過TypeHandlerFactory類進(jìn)行的,在程序初始化時注冊了一些系統(tǒng)類和類型轉(zhuǎn)換類之間的關(guān)系:
- 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);
那么如果想將數(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類
- 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、
實現(xiàn)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方法,實現(xiàn)和數(shù)據(jù)庫之間的存取操作。
iBATIS.NET字段映射3、
修改對應(yīng)的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屬性來指定一個類型轉(zhuǎn)換器。
以上就是iBATIS.NET字段映射的一些基本情況,希望對你有所幫助。
【編輯推薦】