在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>
没有判断,直接加载