在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>
没有判断,直接加载
相关推荐
通用Mapper介绍与使用 Mybatis通用Mapper是一种基于Mybatis的插件,旨在解决单表增删改查操作的烦恼。它可以自动生成CRUD操作的SQL语句,无需手动编写SQL语句,极大地提高了开发效率。 通用Mapper的优点 1. 高度...
updateByPrimaryKeySelective 按主键更新所选字段 updateByPrimaryKey 按主键更新 selectOne 查询一个 selectList 查询列表 selectAll 查询所有记录 insertBatch 批插入 deleteBatchIds 按多个ID批量删除 ...
例如,`selectByPrimaryKey`、`insert`、`updateByPrimaryKey`和`deleteByPrimaryKey`等。 4. **Mapper XML配置文件**:这些XML文件包含了SQL语句的具体实现,与Mapper接口相对应。MBG会自动生成INSERT、UPDATE、...
MyBatis是一个流行的Java持久层框架,它简化了数据库操作,通过XML或注解的方式将SQL与Java代码绑定。在MyBatis中,Mapper接口和Example类是两个核心概念,它们帮助开发者方便地进行CRUD(创建、读取、更新、删除)...
- **BaseMapper.java**:这是基础Mapper接口,包含了Mybatis的CRUD操作,如`deleteByPrimaryKey`, `insert`, `insertSelective`, `updateByPrimaryKeySelective` 和 `updateByPrimaryKey`。这些方法由Mybatis的逆向...
6. `updateByPrimaryKey()`:根据主键更新对象,同样会忽略值为 `null` 的字段。 7. `delete()` 和 `deleteByPrimaryKey()`:删除数据,前者根据对象条件删除,后者直接按主键删除。 此外,MyBatis 还提供了 `...