updateByPrimaryKey与updateByPrimaryKeySelective

在abator中可生成iBatis的代码。其中,Sql_map中带有的两个函数是:

updateByPrimaryKeySelective

updateByPrimaryKey

前者只是更新新的model中不为空的字段。

后者则会将为空的字段在数据库中置为NULL。

例如:Action

    Labor labor = new Labor();  
    labor.setIdentitycard(identitycard);  
    labor.setName(name);  
    labor.setDepartmentid(Integer.parseInt("1"));  
    labor.setEmployeenumber(Integer.parseInt("1006"));  
    labor.setLeadernumberid(Integer.parseInt("1001"));  

    result = laborAO.doSave(labor, "updateByIdentitycard");  
    if (!result.isSuccess()) {  
        return INPUT;  
    } else {  
        System.out.println("message : insert " + name + " . \n");  
        return SUCCESS;  
    }  

如果DAO中使用的是updateByPrimaryKeySelective,则按照不为空的labor值去更新。

如果使用updateByPrimaryKey,这在labor中未定义的字段更新后就没有了。

从SQL语句上分析,二者的不同:

    <update id="updateByPrimaryKeySelective" parameterClass="com.sk.maxdrive.dal.model.Labor" >  
        <!--  
          WARNING - @ibatorgenerated  
          This element is automatically generated by Apache iBATIS Ibator, do not modify.  
          This element was generated on Thu Sep 01 13:55:37 CST 2011.  
        -->  
        update labor  
        <dynamic prepend="set" >  
          <isNotNull prepend="," property="name" >  
            NAME = #name:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="tagid" >  
            TAGID = #tagid:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="departmentid" >  
            DEPARTMENTID = #departmentid:INTEGER#  
          </isNotNull>  
          <isNotNull prepend="," property="duty" >  
            DUTY = #duty:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="employeenumber" >  
            EMPLOYEENUMBER = #employeenumber:INTEGER#  
          </isNotNull>  
          <isNotNull prepend="," property="leadernumberid" >  
            LEADERNUMBERID = #leadernumberid:INTEGER#  
          </isNotNull>  
          <isNotNull prepend="," property="home" >  
            HOME = #home:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="address" >  
            ADDRESS = #address:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="emergencynumber" >  
            EMERGENCYNUMBER = #emergencynumber:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="emergencycontact" >  
            EMERGENCYCONTACT = #emergencycontact:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="hoursetel" >  
            HOURSETEL = #hoursetel:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="postcode" >  
            POSTCODE = #postcode:INTEGER#  
          </isNotNull>  
          <isNotNull prepend="," property="cellphone" >  
            CELLPHONE = #cellphone:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="email" >  
            EMAIL = #email:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="resume" >  
            RESUME = #resume:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="employeedate" >  
            EMPLOYEEDATE = #employeedate:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="headpic" >  
            HEADPIC = #headpic:VARCHAR#  
          </isNotNull>  
          <isNotNull prepend="," property="bankaccount" >  
            BANKACCOUNT = #bankaccount:VARCHAR#  
          </isNotNull>  
        </dynamic>  
        where IDENTITYCARD = #identitycard:VARCHAR#  
      </update>  

有一系列的isNull判断

<update id="updateByPrimaryKey" parameterClass="com.sk.maxdrive.dal.model.Labor" >  
    <!--  
      WARNING - @ibatorgenerated  
      This element is automatically generated by Apache iBATIS Ibator, do not modify.  
      This element was generated on Thu Sep 01 13:55:37 CST 2011.  
    -->  
    update labor  
    set NAME = #name:VARCHAR#,  
      TAGID = #tagid:VARCHAR#,  
      DEPARTMENTID = #departmentid:INTEGER#,  
      DUTY = #duty:VARCHAR#,  
      EMPLOYEENUMBER = #employeenumber:INTEGER#,  
      LEADERNUMBERID = #leadernumberid:INTEGER#,  
      HOME = #home:VARCHAR#,  
      ADDRESS = #address:VARCHAR#,  
      EMERGENCYNUMBER = #emergencynumber:VARCHAR#,  
      EMERGENCYCONTACT = #emergencycontact:VARCHAR#,  
      HOURSETEL = #hoursetel:VARCHAR#,  
      POSTCODE = #postcode:INTEGER#,  
      CELLPHONE = #cellphone:VARCHAR#,  
      EMAIL = #email:VARCHAR#,  
      RESUME = #resume:VARCHAR#,  
      EMPLOYEEDATE = #employeedate:VARCHAR#,  
      HEADPIC = #headpic:VARCHAR#,  
      BANKACCOUNT = #bankaccount:VARCHAR#  
    where IDENTITYCARD = #identitycard:VARCHAR#  
  </update> 

没有判断,直接加载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值