---
title: "Mybatis插入后返回主键"
author: "ware"
date: "2013-05-28T13:04:12Z"
tags: []
categories: ["编程语言"]
slug: "mybatis"
draft: false
lastmod: "2013-05-28T13:04:12Z"
id: 1878762
url: "https://ware.iteye.com/blog/1878762"
---

# [Mybatis插入后返回主键](http://www.cnblogs.com/zhenmingliu/archive/2012/03/21/2410004.html)

&lt;insert id="add" parameterType="vo.Category"&gt;

&lt;selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id"&gt;

SELECT LAST\_INSERT\_ID()

&lt;/selectKey&gt;

insert into category (name\_zh, parent\_id,

show\_order, delete\_status, description

)

values (#{nameZh,jdbcType=VARCHAR},

#{parentId,jdbcType=SMALLINT},

#{showOrder,jdbcType=SMALLINT},

#{deleteStatus,jdbcType=BIT},

#{description,jdbcType=VARCHAR}

)

&lt;/insert&gt;

 

在插入操作完成之后，参数category的id属性就已经被赋值了

执行完mapper.insert(category);获取插入返回的主键用category.getId()就可以了