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());//获取主键

 

转载于:https://www.cnblogs.com/roy-blog/p/7061111.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值