Mybatis逆向工程之插入数据返回该记录id
在生成表的加入如下代码即可
在生成表的加入如下代码即可
使用的时候
如下
public TDataAccount insertTDataAccountAndBackId() {
TDataAccount record = new TDataAccount();
record.setCreateTime(new Date());
accountMapper.insertSelective(record );
return record;
}可以返回这条记录,这个记录里面就包含插入这条数据的Id
就是这么简单!
如果不是逆向工程可以按照下面的例子
insert into t_data_account
id,
total_money,
create_time,
#{id,jdbcType=INTEGER},
#{totalMoney,jdbcType=DOUBLE},
#{createTime,jdbcType=TIMESTAMP},
SELECT LAST_INSERT_ID()
重点在
useGeneratedKeys="true" keyColumn="id" keyProperty="id"
和
SELECT LAST_INSERT_ID()

插入数据之后,mybatis会把id放到这个实体中,你就可以直接在这个实体获取id了。