在insert標(biāo)簽中,加入keyProperty和useGeneratedKeys兩個(gè)屬性:?keyProperty 當(dāng)前主鍵名 ,useGeneratedKeys="true"使用了主鍵自動(dòng)生成
? ? <!-- MySQL中獲取主鍵并插入1 -->
? ? <insert id="insertUser" parameterType="user" keyProperty="id" useGeneratedKeys="true">
? ? ? ? insert into USER(id, name, pwd) values(#{Id}, #{Name}, #{Password})
? ? </insert>
java:
? ? int result=xxxService.insertUser(user);
//? ? result=0||1表示插入幾條數(shù)據(jù)
//獲取插入數(shù)據(jù)的id
????user.getId();//重要?。。。?!