Java開發: Mybatis insert 插入記錄後自動返回主鍵(Mybatis3.x)

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章