zoukankan      html  css  js  c++  java
  • (转)Mybatis insert后返回主键给实体对象(Mysql数据库)

    <insert id="insert" parameterType="com.zqgame.game.website.models.Team">  
                <selectKey resultType="java.lang.Integer" keyProperty="teamId" order="AFTER">  
                    SELECT LAST_INSERT_ID()  
                </selectKey>  
                INSERT INTO kz_team  
                <trim prefix="(" suffix=")" suffixOverrides=",">  
                    <if test="teamId != null">  
                        `team_id`,  
                    </if>  
                    <if test="teamName != null">  
                        `team_name`,  
                    </if>  
                    <if test="regionId != null">  
                        `region_id`,  
                    </if>  
                    <if test="provinceId != null">  
                        `province_id`,  
                    </if>  
                    <if test="cityId != null">  
                        `city_id`,  
                    </if>  
                    <if test="address != null">  
                        `address`,  
                    </if>  
                    <if test="idImg != null">  
                        `id_img`,  
                    </if>  
                    <if test="teamLogo != null">  
                        `team_logo`,  
                    </if>  
                    <if test="introduce != null">  
                        `introduce`,  
                    </if>  
                    <if test="accountId != null">  
                        `account_id`,  
                    </if>  
                </trim>  
                <trim prefix="values (" suffix=")" suffixOverrides=",">  
                    <if test="teamId != null">  
                        #{teamId,jdbcType=INTEGER},  
                    </if>  
                    <if test="teamName != null">  
                        #{teamName,jdbcType=VARCHAR},  
                    </if>  
                    <if test="regionId != null">  
                        #{regionId,jdbcType=INTEGER},  
                    </if>  
                    <if test="provinceId != null">  
                        #{provinceId,jdbcType=INTEGER},  
                    </if>  
                    <if test="cityId != null">  
                        #{cityId,jdbcType=INTEGER},  
                    </if>  
                    <if test="address != null">  
                        #{address,jdbcType=VARCHAR},  
                    </if>  
                    <if test="idImg != null">  
                        #{idImg,jdbcType=VARCHAR},  
                    </if>  
                    <if test="teamLogo != null">  
                        #{teamLogo,jdbcType=VARCHAR},  
                    </if>  
                    <if test="introduce != null">  
                        #{introduce,jdbcType=VARCHAR},  
                    </if>  
                    <if test="accountId != null">  
                        #{accountId,jdbcType=INTEGER},  
                    </if>  
                </trim>  
        </insert>  

    <selectKey resultType="Java.lang.Integer" keyProperty="teamId" order="AFTER">  //teamId实体类主键
         SELECT LAST_INSERT_ID()
    </selectKey>

    或者

    给<insert id="xx"   useGeneratedKeys="true" keyProperty="teamId"> 加入2个属性就可以省略上面那句<selectKey>xxxxxx</selectKey>

    在mybatis中标红的那句话 能将插入的主键返回给实体对象

    if (StringUtils.isEmpty(team.getTeamLogo())) {  
        team.setTeamLogo(defaultTeamLogo);  
    }  
        team.setAccountId(accountId);  
        insert(team);  
      
        System.out.println(team.getTeamId());  

    对于业务中需要取得插入后的主键id值得童鞋来说很方便

    不过那个函数貌似是MySQL提供的 需要其他数据库的另外寻找方法

  • 相关阅读:
    nginx启动失败
    Windows下载安装docker详细步骤
    Consul
    Idea导入多个maven项目到同一目录下
    使用V-chart时踩过的一些坑
    java集合超详解
    HashMap
    curl和wget的区别和使用
    SourceTree3.2.6版本跳过注册办法
    单点登录
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/7421666.html
Copyright © 2011-2022 走看看