zoukankan      html  css  js  c++  java
  • 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对象作为属性。

  • 相关阅读:
    使用RestTemplate进行服务调用的几种方式
    springmvc学习指南 之---第32篇 mybatis 嵌套的处理
    springmvc学习指南 之---第31篇 使用墨客进行测试报错
    springmvc学习指南 之---第30篇 异常的全局处理
    Effective Java 阅读笔记--之(一) 建造者模式(Builder)
    使用mybatis-generator.xml 生成PO 对象
    springmvc学习指南 之---第29篇 springmvc 返回json对象, 不想创建类的前提下
    springmvc学习指南 之---第28篇 springmvc的controller 如何解析视图view? 如何解析json,html,jsp?
    springmvc学习指南 之---第27篇 spring如何实现servlet3.0无web.xml 配置servlet对象的
    springmvc学习指南 之---第26篇 在idea中如何debug跟踪到tomcat内部代码
  • 原文地址:https://www.cnblogs.com/anderslly/p/ibatisNullableType.html
Copyright © 2011-2022 走看看