iBatis 数据库空值的映射处理

上次提到的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,请大家基于业务选择合适的方法。

转载于:https://www.cnblogs.com/sleepywang/archive/2008/12/25/1362543.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值