代码直接放在Github仓库【https://github.com/Damaer/Mybatis-Learning/tree/master/mybatis-05-CURD 】
需要声明的是:此Mybatis
学习笔记,是从原始的Mybatis
开始的,而不是整合了其他框架(比如Spring
)之后,个人认为,这样能对它的功能,它能帮我们做什么,有更好的理解,后面再慢慢叠加其他的功能。
1.useGeneratedKeys 设置主键自增
2.使用selectKey 查询主键
其实一开始的思路是我插入之后,再执行一次select,根据一个唯一的字段来执行select
操作,但是Student
这个类如果插入后再根据名字或者年龄查出来,这根本就是不可行的!!!重名与同年龄的人一定不少。我们的测试方法如下,我们可以看到插入前是没有值的,插入后就有了值:
/**
* 测试插入后获取id
*/
@Test
public void testinsertStudentCacheId(){
Student student=new Student("helloworld",17,85);
System.out.println("插入前:student="+student);
dao.insertStudentCacheId(student);
System.out.println("插入后:student="+student);
}
1.useGeneratedKeys 设置主键自增
<insert id="insertStud