(轉)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提供的 需要其他數據庫的另外尋找方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM