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中。
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; }
}
}

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















































那么如果想将数据库中的一个字段映射成我们自己的一个类,在这个类中进行一些个性化处理,应该怎么办呢?
本来我想仿照StringTypeHandler类写一个自己的类型处理类,但是通过查看IBatis的源代码,就算写好了自己的
类型处理类,好像也找不到注册的接口(如果哪位兄弟找到了接口,望告知)
另一种方式是通过已经注册的CustomTypeHandler类型,实行其中的ITypeHandlerCallback接口来实现的,具体实现方式如下:
我这里实现的只是一个演示程序,演示将数据库中的Account_LastName和Account_Email字段映射成自定义的Property类型,同时把它们放入一个Hashtable中。
1、自定义Property类























































主要是其中的GetResult和SetParameter方法,实现和数据库之间的存取操作。
3、修改对应的Domain类,加入两个属性:






























4、修改配置文件:






主要是利用了其中的typeHandler属性来指定一个类型转换器。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?