第一种方法:
insert INTO student(name) VALUES("南亚");
SELECT @@identity
第二种方法:
insert INTO student(name) VALUES("南亚");
SELECT LAST_INSERT_ID()
在mapper.xml中写法:
<insert id="insertStudentCacheId" >
insert into student(name,age,score) values(#{name},#{age},#{score})
<!-- order属性:表示先插入还是先生成id;mysql是先插入后再生成id,oracle是先生成id再插入 -->
<!-- SELECT @@identity 查询出的值初始化keyProperty属性 -->
<selectKey resultType="int" keyProperty="id" order="AFTER">
SELECT @@identity
</selectKey>
</insert>
测试代码:
@Test
public void test02(){
Student student = new Student("南非", 20, 99.5);
System.out.println("插入前:student="+student);
dao.insertStudentCacheId(student);
System.out.println("插入后:student="+student);
}