<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的網上有很多講解就不再贅述)