首页 > 其他 > 详细

MyBatis返回主键

时间:2015-07-16 18:47:28      阅读:307      评论:0      收藏:0      [点我收藏+]

网上给的例子都很简单 , 只要用useGeneratedKey就行了.

    @Insert({ "INSERT INTO money_record_increasement (id, createTime) VALUES (null, #{createTime})" })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertMoneyRecordIncreasement(MoneyRecordIncreasement record);

 

可这时候如果直接使用返回的这个int型的变量,发现是1 , 也就是成功返回1 . 返回的主键是不能这么用的 ......返回的主键是从插入的对象中获取的 .  如下

 

MoneyRecordIncreasement moneyRecordIncreasement = new MoneyRecordIncreasement();
        moneyRecordIncreasement.setCreateTime(createTime);
        int id = moneyRecordIncreasementMapper.insertMoneyRecordIncreasement(moneyRecordIncreasement);

        return moneyRecordIncreasement.getId();

这里id是是否成功的标示 , 要返回主键id , 需要从对象身上获取....

MyBatis返回主键

原文:http://www.cnblogs.com/taojintianxia/p/4651598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!