iBATIS.NET Tips & Tricks(1) : 使用Nullable类型

简介:

iBatis中,我们面对的通常会是Domain Model,而不是DataSetDataTable。这样在处理业务逻辑时,就可以不必再关心数据持久相关的东东了。

Domain Model类型的属性常常会对应数据库中表的一个字段(也可能是其它Domain类型的)。比如下面的Product类:

[Serializable]
public  partial  class  Product
{
    
#region  private fields

    
private   int  _productid;

    
private   string  _productname  =  String.Empty;
    
private   int  _categoryid;
    
private   string  _description  =  String.Empty;

    
#endregion

    
#region  constructors

     public  Product() { }

     public  Product( int  productid)
    {
        
this ._productid  =  productid;
    }

    
#endregion

    
#region  Public Properties

    
public   int  Productid
    {
        
get  {  return  _productid; }
        
set  { _productid  =  value; }
    }


    
public   string  Productname
    {
        
get  {  return  _productname; }
        
set  { _productname  =  value; }
    }

    
public   int  CategoryId
    {
        
get  {  return  _categoryid; }
        
set  { _categoryid  =  value; }
    }

    
public   string  Description
    {
        
get  {  return  _description; }
        
set  { _description  =  value; }
    }

    
#endregion
}

ProductId属性对应表ProductProductId字段,CategoryId对应CategoryId。为了数据的参照完整性,我们可以将CategoryId设置为外键。

这样问题就来了,如果我们通过new Product()的方式新建一个实例,但没有设置CategoryId的值,那么CategoryId的值实际上为int类型的默认值0,这样在插入数据的时候就会发生外键冲突,因为Category表中不存在主键为0的记录。

另外,如果有类型为DateTime的属性,也会有麻烦。DateTime类型的默认值为0001  1  1 日午夜 12:00:00,而这不在SQL Serverdatetime类型的范围内,在插入数据时也会引发错误。难道我们要手工处理所有这些字段?

幸好还有Nullable类型。如果类型的属性为Nullable类型,那么其默认值将为null。从而省却了上面的烦恼。

我们可将Product类的定义改为:

[Serializable]
public  partial  class  Product
{
    
#region  private fields

    
private   int  _productid;

    
private   string  _productname  =  String.Empty;
    
private  int?  _categoryid;
    
private   string  _description  =  String.Empty;

    
#endregion

    
#region  constructors

    
public  Product() { }

    
public  Product( int  productid)
    {
        
this ._productid  =  productid;
    }

    
#endregion

    
#region  Public Properties

    
public   int  Productid
    {
        
get  {  return  _productid; }
        
set  { _productid  =  value; }
    }


    
public   string  Productname
    {
        
get  {  return  _productname; }
        
set  { _productname  =  value; }
    }

    
public   int?  CategoryId
    {
        
get  {  return  _categoryid; }
        
set  { _categoryid  =  value; }
    }

    
public   string  Description
    {
        
get  {  return  _description; }
        
set  { _description  =  value; }
    }

    
#endregion
}

注意:实际应用中可能不会使用CategoryId,而是使用Category对象作为属性。


本文转自一个程序员的自省博客园博客,原文链接:http://www.cnblogs.com/anderslly/archive/2007/06/28/ibatisNullableType.html,如需转载请自行联系原作者。

目录
相关文章
|
6月前
poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
109 0
|
7月前
flowable项目报错:java.sql.SQLSyntaxErrorException: Table ‘psr_flowable_test.act_ge_property’ doesn’t exi
flowable项目报错:java.sql.SQLSyntaxErrorException: Table ‘psr_flowable_test.act_ge_property’ doesn’t exi
JavaFX-MediaPlayer 参数URI格式问题:java.net.URISyntaxException: Illegal character in path at index X
JavaFX-MediaPlayer 参数URI格式问题:java.net.URISyntaxException: Illegal character in path at index X
|
Java
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
java实战小结-Controller报错:Content type ‘multipart/form-data;boundary=----WebKitFormBoundaryxxxx not supp
312 0
|
Java
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
255 0
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
DEFAULT keyword in ABAP and Optional Class in Java
DEFAULT keyword in ABAP and Optional Class in Java
DEFAULT keyword in ABAP and Optional Class in Java
|
测试技术
一起谈.NET技术,三种属性操作性能比较:PropertyInfo + Expression Tree + Delegate.CreateDelegate
  在《上篇》中,我比较了三种属性操作的性能:直接操作,单纯通过PropertyInfo反射和IL Emit。本篇继续讨论这个话题,我们再引入另外两种额外的属性操作方式:Expression Tree(这和IL Emit基本一致)和通过Delegate的静态方法CreateDelegate创建相应的委托进行属性的赋值和取值。
859 0
|
算法 C# Java
使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, 另外两步虽然具体内容不一样, 但是都做做的同一类工作.
1315 0