您的位置:首页 > 编程语言 > Java开发

Java开发: Mybatis insert 插入记录后自动返回主键(Mybatis3.x)

2012-08-01 17:08 316 查看
<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" >
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
在创建成功后会自动返回单据号,可以通过:
receiptMapper.insertReceipt(receipt);
   	System.out.println("单据创建成功,单据号为:"+receipt.getId());//获取即可(Mybatis3.x适用,2.0的网上有很多讲解就不再赘述)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  insert java