上次提到的iBatis关于空值的映射处理,primitive数据类型容易出现此问题,今天服务就因此问题造成无法启动。
 
我查了一下资料又做了验证,目前有两种方法可解决此问题:
 
1
使用映射xml的 nullValue 属性(attribute)。例如,下面红色标注的nullValue='0'指当表tb_role中栏目role_Max为数据库空时,设置相应bean对象属性为0;反之如果对象com.hp.hpe.service.dataService.dao.domain.Role的roleMax 属性(property)为0时, 新增/更新数据库时,使用数据库的null值进行更新。
 
 <resultMap id="result" class="com.hp.hpe.service.dataService.dao.domain.Role">
  <result property="roleId" column="role_id"/>
  <result property="roleName" column="role_name"/>
  <result property="roleMax" column="role_max" nullValue='0'/>
  <result property="roleContext" column="role_context"/>
  <result property="gameRound" column="game_round"/>
 </resultMap>
 
 
2 
另一种方法是不需要修改映射文件,修改bean的声明,使用primitive类型对应的封装对象类型。例如: int <--> Integer,此时Java的null 与 DB的null对应。
 
public Integer getRoleMax() {
  return roleMax;
 }
 public void setRoleMax(Integer roleMax) {
  this.roleMax = roleMax;
 }
 
当然,修改schema非空也是一种办法,anyway,请大家基于业务选择合适的方法。
posted on 2008-12-25 19:41  sleepy  阅读(634)  评论(0编辑  收藏  举报