码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis返回主键

时间:2015-07-16 18:48:52      阅读:98      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!