Mybatis 主键返回

来源:互联网 发布:js二维映射 编辑:程序博客网 时间:2024/06/11 16:39
Mybatis 主键返回
在mysql中可以使用SELECT LAST_INSERT_ID();方法取当前事务中最后生成的id。
在mybatis可以使用主键返回方法,把主键取出来返回给java程序。

<insert id="insertUser" parameterType="cn.itcast.pojo.User">     <!-- keyProperty:user对象的主键属性          resultType:主键的数据类型          order:取主键语句的执行时机。BEFORE:在插入之前执行              AFTER:插入之后执行      -->     <selectKey keyProperty="id" resultType="int" order="AFTER">          <!-- mysql取主键的函数 -->          SELECT LAST_INSERT_ID();     </selectKey>     INSERT INTO `user`(username,birthday,sex,address)     VALUES(#{username},#{birthday},#{sex},#{address})</insert>



原创粉丝点击