insert新建对象后,返回主键值

举例子:

@Insert("insert into p_exchange_prop_log (userid,from_propid,to_propid,type,`describe`,`status`,"
+ "exchange_time,real_user_name,real_moblie_phone,real_shipping_address,match_id)"
+ "values(#{userid},#{from_propid},#{to_propid},#{type},#{describe},#{status},SYSDATE(),#{real_user_name},"
+ "#{real_moblie_phone},#{real_shipping_address},#{match_id})")
@SelectKey(statement = "SELECT last_insert_id() as id", keyProperty = "id", before = false, resultType = Integer.class, statementType = StatementType.STATEMENT)
public void addExchangePropLog(PExchangePropLog log);



实际使用的时候

PExchangePropLog log = new PExchangePropLog();
log.setUserid(userid);
log.setFrom_propid(propid);
log.setTo_propid("104999");//兑换为金币
log.setType(PExchangePropLog_type.exchange_gold);
log.setDescribe(prop_name+"兑换为金币");
log.setStatus(PExchangePropLog_status.not_handle);
log.setMatch_id(mess.getContent());
gameMallMapper.addExchangePropLog(log);

执行插入操作以后,log 对象的id属性已经赋予了数据库的id值,通过getId()即可获得,

这种情况可用于新建对象后又update对象的情况(一般这种情况下,update时只能通过id获得唯一数据)。

你可能感兴趣的:(mybatis)