<insert id="insertUser" useGeneratedKeys="true" keyProperty="id" parameterType="UserEntity"> INSERT INTO USER VALUES(null,#{userName},#{password},#{realName}) </insert>
其中keyProperty的值就是數據庫中自增加字段名。java
@RequestMapping("addUser") public String addUser(@ModelAttribute UserEntity userEntity) { int i = userService.insertUser(userEntity);//插入記錄到數據庫,userEntity中沒有設置id的值 String result = ""; if (i > 0) { result = "inster User SUCCESS!!! ID: " + userEntity.getId();//插入成功後,將自增加的id存入到原來的model中,經過get方法就能拿到自增加的id了 } else { result = "inster User FAIL!!!"; } return result; }