zoukankan      html  css  js  c++  java
  • mybatis获取刚插入数据的ID

    1.很多时候,sql的语句的住建都是逐渐递增的,mybatis给我们提供了获取ID的方法
    2.代码,方法一
    1.useGeneratedKeys=“true” keyProperty=“id”

      <insert id="insert" parameterType="com.yd.pojo.DemoUser" useGeneratedKeys="true" keyProperty="id" >
        insert into demo_user (id, name, age, 
          address, love)
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, 
          #{address,jdbcType=VARCHAR}, #{love,jdbcType=VARCHAR})
      </insert>
    

    方法二

    select LAST_INSERT_ID()

      <insert id="insertSelective" parameterType="com.yd.pojo.DemoUser" >
        <selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER">
          select LAST_INSERT_ID()
        </selectKey>
        insert into demo_user
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="name != null" >
            name,
          </if>
          <if test="age != null" >
            age,
          </if>
          <if test="address != null" >
            address,
          </if>
          <if test="love != null" >
            love,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null" >
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="age != null" >
            #{age,jdbcType=INTEGER},
          </if>
          <if test="address != null" >
            #{address,jdbcType=VARCHAR},
          </if>
          <if test="love != null" >
            #{love,jdbcType=VARCHAR},
          </if>
        </trim>
      </insert>
    

    3.运行结果
    在这里插入图片描述

  • 相关阅读:
    ESXi创建磁盘命令
    TNS-12518,TNS-12536,TNS-00506,Linux Error: 11: Resource temporarily unavailable
    监听的instance status blocked分析
    Oracle 用户、对象权限、系统权限
    MIME详解
    11g等待事件之library cache: mutex X
    Latch Free
    PowerDesigner小技巧
    yum本地源配置
    内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置
  • 原文地址:https://www.cnblogs.com/szls-666/p/12494172.html
Copyright © 2011-2022 走看看