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了,很神奇吧
  • 相关阅读:
    题库重整
    计算几何练习题――直线交点
    An Easy Task
    简单排序
    IBM Minus One
    Binary Numbers
    去掉VS2010代码中文注释的红色下划线
    【转】Windows socket基础
    【STL】vector的insert方法详解
    window7下 cocos2dx android交叉编译环境部署小结
  • 原文地址:https://www.cnblogs.com/plf112233/p/4167889.html
Copyright © 2011-2022 走看看