- <insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User">
- insert into user(userName,password,comment)
- values(#{userName},#{password},#{comment})
- </insert>
如上所示,我们在insert中指定了keyProperty="userId",其中userId代表插入的User对象的主键属性。
User.java
- public class User {
- private int userId;
- private String userName;
- private String password;
- private String comment;
- //setter and getter
- }