【转】mybatis 获取自增id
转自:http://www.cnblogs.com/rhythmK/p/4047142.html
1、环境:
mybatis : 3.2.3
spring-mybatis: 1.2.1
mysql:5.5.29
实体:
public class sys_user {
private int userid;
private String username;
public int getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
insert 节点配置:
<insert id="insert" parameterType="com.mamaguwen.entity.sys_user" useGeneratedKeys="true" keyProperty="userid">
<!-- <selectKey keyProperty="userid" resultType="int" order="BEFORE">
SELECT LAST_INSERT_ID() as userid
</selectKey> -->
insert into sys_user ( UserName)
values (#{username})
</insert>
keyProperty:对应为实体字段值
调用代码:
sys_UserDao.insert(model);
System.out.println("getUserid:"+ model.getUserid());