代码直接放在Github仓库【 https://github.com/Damaer/Myb... 】
须要声明的是:此Mybatis
学习笔记,是从原始的Mybatis
开始的,而不是整合了其余框架(好比Spring
)以后,我的认为,这样能对它的功能,它能帮咱们作什么,有更好的理解,后面再慢慢叠加其余的功能。
咱们知道不少时候咱们有一个需求,咱们须要把插入数据后的id返回来,以便咱们下一次操做。java
其实一开始的思路是我插入以后,再执行一次select,根据一个惟一的字段来执行select
操做,可是Student
这个类若是插入后再根据名字或者年龄查出来,这根本就是不可行的!!!重名与同年龄的人必定很多。
咱们的测试方法以下,咱们能够看到插入前是没有值的,插入后就有了值:mysql
/** * 测试插入后获取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); }
<insert id="insertStudentCacheId" useGeneratedKeys="true" keyProperty="id" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) </insert>
须要注意的点:git
useGeneratedKeys="true"
表示设置属性自增keyProperty="id"
设置主键的字段parameterType="Student"
设置传入的类型<insert id="insertStudentCacheId" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定结果类型resultType,keyProperty是属性,自动返回到属性id中,order是次序,after是指获取id是在于插入后 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select @@identity </selectKey> </insert>
或者写成:github
<insert id="insertStudentCacheId" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定结果类型resultType,keyProperty是属性,自动返回到属性id中,order是次序,after是指获取id是在于插入后 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> </insert>
两种方式的结果:
[](http://markdownpicture.oss-cn...sql
注意要点:数据库
<insert></insert>
没有返回属性(resultType)
,可是里面的<selectKey></selectKey>
是有返回值类型的。order="AFTER"
表示先执行插入,以后才执行selectkey
语句的。select @@identity
和select LAST_INSERT_ID()
都表示选出刚刚插入的最后一条数据的id。insert
的那个对象中,咱们能够直接使用就能够了。其实,咱们的接口中能够有返回值,可是这个返回值不是id,而是表示插入后影响的行数,此时sql中仍和上面同样,不须要写返回值。apache
<insert id="insertStudentCacheIdNoReturn" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定结果类型resultType,keyProperty是属性,自动返回到属性id中,order是次序,after是指获取id是在于插入后 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> </insert>
接口中:markdown
// 增长新学生并返回id返回result public int insertStudentCacheId(Student student);
接口的实现类:mybatis
public int insertStudentCacheId(Student student) { int result; try { sqlSession = MyBatisUtils.getSqlSession(); result =sqlSession.insert("insertStudentCacheId", student); sqlSession.commit(); } finally { if (sqlSession != null) { sqlSession.close(); } } return result; }
Test中:框架
public void testinsertStudentCacheId(){ Student student=new Student("helloworld",17,101); System.out.println("插入前:student="+student); int result = dao.insertStudentCacheId(student); System.out.println(result); System.out.println("插入后:student="+student); }
结果证实:result的值为1,表示插入了一行,查看数据库,确实插入了数据。
PS:若是没法建立链接,须要把Mysql
的jar包升级:
<!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency>
若是报如下的错误,那么须要将&改为转义后的符号&
:
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 107; 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
在xml里面配置须要转义,不在xml文件里面配置则不须要
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&serverTimezone=UTC"/>
【做者简介】:
秦怀,公众号【秦怀杂货店】做者,技术之路不在一时,山高水长,纵使缓慢,驰而不息。这个世界但愿一切都很快,更快,可是我但愿本身能走好每一步,写好每一篇文章,期待和大家一块儿交流。
此文章仅表明本身(本菜鸟)学习积累记录,或者学习笔记,若有侵权,请联系做者核实删除。人无完人,文章也同样,文笔稚嫩,在下不才,勿喷,若是有错误之处,还望指出,感激涕零~