zoukankan      html  css  js  c++  java
  • mybatis插入实体到数据库后获取自增的主键

    话不多说,直接说方法。

    1.在insert语句中加入如下的代码。

    <insert id="insertSelective" parameterType="com.qgranite.entity.TCategory">
            insert into t_category
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="categoryId != null">
                    category_id,
                </if>
                <if test="categoryName != null">
                    category_name,
                </if>
                <if test="categoryRemark != null">
                    category_remark,
                </if>
                <if test="categoryType != null">
                    category_type,
                </if>
                <if test="isRoot != null">
                    is_root,
                </if>
                <if test="categoryLevel != null">
                    category_level,
                </if>
                <if test="rootCategoryId != null">
                    root_category_id,
                </if>
                <if test="parentCategoryId != null">
                    parent_category_id,
                </if>
                <if test="parentCategoryName != null">
                    parent_category_name,
                </if>
                <if test="sortNum != null">
                    sort_num,
                </if>
                <if test="createTime != null">
                    create_time,
                </if>
                <if test="updateTime != null">
                    update_time,
                </if>
                <if test="isDel != null">
                    is_del,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="categoryId != null">
                    #{categoryId,jdbcType=INTEGER},
                </if>
                <if test="categoryName != null">
                    #{categoryName,jdbcType=VARCHAR},
                </if>
                <if test="categoryRemark != null">
                    #{categoryRemark,jdbcType=VARCHAR},
                </if>
                <if test="categoryType != null">
                    #{categoryType,jdbcType=INTEGER},
                </if>
                <if test="isRoot != null">
                    #{isRoot,jdbcType=INTEGER},
                </if>
                <if test="categoryLevel != null">
                    #{categoryLevel,jdbcType=INTEGER},
                </if>
                <if test="rootCategoryId != null">
                    #{rootCategoryId,jdbcType=INTEGER},
                </if>
                <if test="parentCategoryId != null">
                    #{parentCategoryId,jdbcType=INTEGER},
                </if>
                <if test="parentCategoryName != null">
                    #{parentCategoryName,jdbcType=VARCHAR},
                </if>
                <if test="sortNum != null">
                    #{sortNum,jdbcType=INTEGER},
                </if>
                <if test="createTime != null">
                    #{createTime,jdbcType=TIMESTAMP},
                </if>
                <if test="updateTime != null">
                    #{updateTime,jdbcType=TIMESTAMP},
                </if>
                <if test="isDel != null">
                    #{isDel,jdbcType=INTEGER},
                </if>
            </trim>
            <selectKey resultType="java.lang.Integer" order="AFTER"
                keyProperty="categoryId">
                SELECT @@IDENTITY AS categoryID
            </selectKey>
        </insert>

    最底部的那段代码

    <selectKey resultType="java.lang.Integer" order="AFTER"
                keyProperty="categoryId">
                SELECT @@IDENTITY AS categoryID
            </selectKey>

    其中categoryId是实体中的自增的主键。

    2.在代码中直接通过“实体.主键”的方式调用。

     baseDao.addT("TCategoryMapper.insertSelective",
                            category);//存储
     System.out.println("主键:"+category.getCategoryId());//获取主键
  • 相关阅读:
    Java08_Lambda表达式
    Java基础02
    Java基础07
    JAVA基础06
    Java基础05
    Java基础01
    面向对象与面向过程
    Java常识2
    CSS常用属性记录
    geoserver发布热力图服务
  • 原文地址:https://www.cnblogs.com/roy-blog/p/7061111.html
Copyright © 2011-2022 走看看