ibatis insert 返回null问题

   

关于com.ibatis.sqlmap.client.SqlMapExecutor下 的insert (java.lang.String id, java.lang.Object parameterObject) 方法:

 

 

它的意思是说返回值是新插入记录的主键,类型为Object主要是因为主键类型可以是int也可以是String类型。

然而,如果我们使用下列的配置文件进行插入的话,返回的值为null

  1. <insert id="insertPrdcategory" parameterClass="Prdcategory">
  2. insert into Prdcategory ( name,pid,deep ) values ( #name#,
  3. #pid#, #deep# )
  4. </insert>

那我们要怎样解决这个问题,让它返回插入行的主键呢,这时我们就是使用到<selectKey>,下面仅以MSSQL数据库为例,改写 上述配置文件,具体如下:

<insert id="insertPrdcategory" parameterClass="Prdcategory"> insert into Prdcategory ( name,pid,deep ) values ( #name#, #pid#, #deep# ) <selectKey resultClass="int" keyProperty="id"> select max(id) from Prdcategory </selectKey> </insert>

 

DAO的实现类方法里面就应该这样写了(注意整型转换用Integer类名)

  1. public int insertPrdcategory(Prdcategory product) throws Exception {
  2. return (Integer)IbatisUtil.getSqlMapper().insert("insertPrdcategory", product);
  3. }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值