---
title: "MyBatis获取插入记录的自增长字段值"
author: "hellostory"
date: "2011-09-19T15:38:00Z"
tags: []
slug: "mybatis"
draft: false
lastmod: "2012-02-22T11:13:15Z"
id: 1416477
url: "https://hellostory.iteye.com/blog/1416477"
---

第一步：

在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”，其中keyProperty是Java对象的属性名！

第二步：

Mybatis执行完插入语句后，自动将自增长值赋值给对象Spares的属性id。因此，可通过Spares对应的getter方法获取！