<insert id="insertReceipt" parameterType="Receipt" useGeneratedKeys="true" keyProperty="id">
insert into tb_receipt (name,from_storage_code,to_storage_code,description,type_id,code,batch_number)
values ( #{name},#{fromStorageCode},#{toStorageCode},
<choose>
<when test="description!=null">
#{description}
</when>
<otherwise>
''
</otherwise>
</choose>,
<choose>
<when test="typeId!=null">
#{typeId}
</when>
<otherwise>
0
</otherwise>
</choose>,
<choose>
<when test="code!=null">
#{code}
</when>
<otherwise>
''
</otherwise>
</choose>,
#{batchNumber} )
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id"
Java开发: Mybatis insert 插入记录后自动返回主键(Mybatis3.x)
最新推荐文章于 2024-07-10 21:31:19 发布
在Mybatis中,使用`<insert>`标签配合`useGeneratedKeys="true"`和`keyProperty="id"`可以实现插入记录后自动返回主键ID。示例代码展示了一个`insertReceipt`方法,根据传入参数插入tb_receipt表,并在`<selectKey>`标签中获取最后插入的ID。插入成功后,通过`receiptMapper.insertReceipt(receipt)`和`receipt.getId()`即可获取到新生成的单据号。
摘要由CSDN通过智能技术生成