获取插入的数据的id

本文介绍如何在SQL中获取新插入记录的自增ID。通常在数据库操作中,当插入一条新数据并需要立即获取其唯一标识时,可以使用LAST_INSERT_ID()函数或者类似的API。详细步骤和示例代码将帮助你理解这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<insert id="insert" parameterType="com.zknw.sysmanage.model.SysUser" useGeneratedKeys="true" keyProperty="userId">
<!--   keyProperty:DO类中的id属性   keyColumn:数据库的id类  -->
<!--   resultType:声明数据类型 order:声明执行时间  after/before-->
    <selectKey keyProperty="userId" keyColumn="userid" resultType="Integer" order="AFTER">
        select last_insert_id();
    </selectKey>
    insert into sys_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="userName != null">
            username,
        </if>
        <if test="passWord != null">
            password,
        </if>
        <if test="unEnabled != null">
            unenabled,
        </if>
        <if test="dateCreated != null">
            datecreated,
        </if>
        <if test="dateLastPasswordChange != null and dateLastPasswordChange!=''">
            datelastpasswordchange,
        </if>
        <if test="dateLastLogin != null and dateLastPasswordChange!=''">
            datelastlogin,
        </if>
        <if test="dateLastActivity != null and dateLastPasswordChange!=''">
            datelastactivity,
        </if>
        <if test="organ_id != null and organ_id!=''">
            organ_id,
        </if>
        <if test="organ_name != null and organ_name!=''">
            organ_name,
        </if>
        <if test="team != null and team!=''">
            team,
        </if>
        <if test="job != null and job!=''">
            job,
        </if>
        <if test="jobType != null and jobType!=''">
            jobType,
        </if>
        <if test="dob != null and dob!=''">
            dob ,
        </if>
        <if test="grade != null and grade!=''">
            grade,
        </if>
        <if test="create_user_id != null and create_user_id!=''">
            create_user_id,
        </if>
        <if test="update_date != null and update_date!=''">
            update_date,
        </if>
        <if test="create_user_name != null and create_user_name!=''">
            create_user_name,
        </if>
        <if test="update_user_name != null and update_user_name!=''">
            update_user_name,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="userName != null">
            #{userName},
        </if>
        <if test="passWord != null">
            #{passWord},
        </if>
        <if test="unEnabled != null">
            #{unEnabled},
        </if>
        SYSDATE(),
        <if test="dateLastPasswordChange != null and dateLastPasswordChange!='' ">
            dateLastPasswordChange,
        </if>
        <if test="dateLastLogin != null and dateLastLogin!=''">
            dateLastLogin,
        </if>
        <if test="dateLastActivity != null and dateLastActivity!=''">
            dateLastActivity,
        </if>
        <if test="organ_id != null">
            #{organ_id},
        </if>
        <if test="organ_name != null">
            #{organ_name},
        </if>
        <if test="team != null">
            #{team},
        </if>
        <if test="job != null">
            #{job},
        </if>
        <if test="jobType != null">
            #{jobType},
        </if>
        <if test="dob != null">
            #{dob},
        </if>
        <if test="grade != null">
            #{grade},
        </if>
        <if test="create_user_id != null">
            #{create_user_id},
        </if>
        <if test="update_date != null">
            #{update_date},
        </if>
        <if test="create_user_name != null">
            #{create_user_name},
        </if>
        <if test="update_user_name != null">
            #{update_user_name},
        </if>
    </trim>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值