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属性来指定一个类型转换器。
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属性来指定一个类型转换器。
相关文章推荐
- IBatis.Net学习笔记八--把字段映射成一个自定义对象
- IBatis.Net学习笔记八--把字段映射成一个自定义对象 (转)
- hibernate 自定义字段查询映射为pojo对象的新思路
- 用HIBERNATE实现领域对象的自定义字段
- 给phpcms v9专题添加一个radio单选按钮,可以多次利用专题创建不同的栏目进行内容编辑,给专题添加一个自定义字段
- MongoDB高级查询(二):对一个表中的2个字段中的"内嵌对象"中的字段进行比较----------- aggregate聚合查询
- 自定义的一个JS对象Cutover
- Reflect 通过反射获取自定义注解值给另外一个对象赋值
- 用Hibernate实现领域对象的自定义字段
- DateTable映射到对象的字段和属性
- 使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。
- pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
- EAS如何给一个对象字段赋值(dep加的字段)
- Hibernate 实体类注解 Entity方式5、@Basic(fetch=FetchType,optional=true) 可选 @Basic表示一个简单的属性到数据库表的字段的映射,对于没有
- 织梦自定义表单如何取出一个字段的值,并形成option
- 在oracle中判定一个用户自定义对象是否存在
- 把一个对象的字段和值进行格式化输出(多层嵌套)
- ASP.Net中用ViewState存储自定义复杂对象后类型转换的一个问题
- JDBC:数据库自定义类型与Java类的映射—将对象存储在关系数据库中(一)
- NBearMapping - 开源通用对象映射组件v1.0.0.2 beta - 支持枚举类型字段