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());//获取主键
  • 相关阅读:
    Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码
    ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
    ASP.NET Core之跨平台的实时性能监控(2.健康检查)
    应用程序的8个关键性能指标以及测量方法
    ASP.NET Core之跨平台的实时性能监控
    浅析Entity Framework Core2.0的日志记录与动态查询条件
    开发短网址平台的思路
    Nginx解决错误413 Request Entity Too Large
    配置Nginx实现负载均衡
    Windows下Nginx的安装及使用方法入门
  • 原文地址:https://www.cnblogs.com/roy-blog/p/7061111.html
Copyright © 2011-2022 走看看