zoukankan      html  css  js  c++  java
  • mybatis插入的同时获取主键id

    <insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole"
            useGeneratedKeys="true" keyProperty="id">
            insert into app_cms_role
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="name != null">
                    name,
                </if>
                <if test="createTime != null">
                    create_time,
                </if>
                <if test="updateTime != null">
                    update_time,
                </if>
                <if test="appId != null">
                    app_id,
                </if>
                <if test="roleName != null">
                    role_name,
                </if>
                <if test="rolePrivilege != null">
                    role_privilege,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="name != null">
                    #{name,jdbcType=VARCHAR},
                </if>
                <if test="createTime != null">
                    #{createTime,jdbcType=TIMESTAMP},
                </if>
                <if test="updateTime != null">
                    #{updateTime,jdbcType=TIMESTAMP},
                </if>
                <if test="appId != null">
                    #{appId,jdbcType=INTEGER},
                </if>
                <if test="roleName != null">
                    #{roleName,jdbcType=VARCHAR},
                </if>
                <if test="rolePrivilege != null">
                    #{rolePrivilege,jdbcType=VARCHAR},
                </if>
            </trim>
        </insert>

    需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。

    方法:在mapper中指定keyProperty属性,示例如上:

    AppCmsRole role=new AppCmsRole();
    appCmsRoleExMapper.insertAndReturnId(role);
    role.getId();//就能获取到id了,很神奇吧
  • 相关阅读:
    软工实践个人总结
    Beta版本演示
    Beta 5
    Beta 4
    Beta 3
    Beta 2
    2020系统综合实践 期末大作业 02组
    2020系统综合实践 第7次实践作业 2组
    2020系统综合实践 第6次实践作业 2组
    2020系统综合实践 第5次实践作业
  • 原文地址:https://www.cnblogs.com/plf112233/p/4167889.html
Copyright © 2011-2022 走看看