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 labor
    
      
        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#
  

一系列的isNull判断


    
    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#
  

没有判断,直接加载



你可能感兴趣的:(编程语言与框架,其它)