代码间接放在Github仓库【https://github.com/Damaer/Myb... 】
须要申明的是:此Mybatis学习笔记,是从原始的Mybatis开始的,而不是整合了其余框架(比方Spring)之后,集体认为,这样能对它的性能,它能帮咱们做什么,有更好的了解,前面再缓缓叠加其余的性能。

咱们晓得很多时候咱们有一个需要,咱们须要把插入数据后的id返回来,以便咱们下一次操作。

其实一开始的思路是我插入之后,再执行一次select,依据一个惟一的字段来执行select操作,然而Student这个类如果插入后再依据名字或者年龄查出来,这根本就是不可行的!!!重名与同年龄的人肯定不少。
咱们的测试方法如下,咱们能够看到插入前是没有值的,插入后就有了值:

/** * 测试插入后获取id */@Testpublic void testinsertStudentCacheId(){    Student student=new Student("helloworld",17,85);    System.out.println("插入前:student="+student);    dao.insertStudentCacheId(student);    System.out.println("插入后:student="+student);}

useGeneratedKeys 设置主键自增

    <insert id="insertStudentCacheId" useGeneratedKeys="true" keyProperty="id" parameterType="Student">        insert into student(name,age,score) values(#{name},#{age},#{score})    </insert>

须要留神的点:

  • 1.useGeneratedKeys="true"示意设置属性自增
  • 2.keyProperty="id"设置主键的字段
  • 3.parameterType="Student"设置传入的类型
  • 4.留神:尽管有返回类型,然而咱们不须要手动设置返回的类型,这个是由框架帮咱们实现的,所以对应的接口办法也是没有返回值的,会批改咱们插入的对象,设置id值。
  • 5.实体类中id属性字段肯定须要set以及get办法

应用selectKey 查问主键
    <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>

或者写成:

    <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...

留神要点:

  • 1.最外层的<insert></insert>没有返回属性(resultType),然而外面的<selectKey></selectKey>是有返回值类型的。
  • 2.order="AFTER"示意先执行插入,之后才执行selectkey语句的。
  • 3.select @@identityselect LAST_INSERT_ID()都示意选出刚刚插入的最初一条数据的id。
  • 4.实体类中id属性字段肯定须要set以及get办法
  • 5.此时,接口中仍不须要有返回值,框架会主动将值注入到咱们insert的那个对象中,咱们能够间接应用就能够了。

其实,咱们的接口中能够有返回值,然而这个返回值不是id,而是示意插入后影响的行数,此时sql中仍和下面一样,不须要写返回值。

<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>

接口中:

// 减少新学生并返回id返回resultpublic int insertStudentCacheId(Student student);

接口的实现类:

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>

如果报以下的谬误,那么须要将&改成本义后的符号&amp;

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&amp;serverTimezone=UTC"/>

【作者简介】
秦怀,公众号【秦怀杂货店】作者,技术之路不在一时,山高水长,纵使迟缓,驰而不息。这个世界心愿所有都很快,更快,然而我心愿本人能走好每一步,写好每一篇文章,期待和你们一起交换。

此文章仅代表本人(本菜鸟)学习积攒记录,或者学习笔记,如有侵权,请分割作者核实删除。人无完人,文章也一样,文笔稚嫩,在下不才,勿喷,如果有谬误之处,还望指出,感激不尽~