鍍金池/ 問(wèn)答/Java  C  數(shù)據(jù)庫(kù)/ mybatis如何返回插入記錄的主鍵id?

mybatis如何返回插入記錄的主鍵id?

我要返回mybatis自增主鍵id,添加了useGeneratedKeys="true" keyProperty="id" keyColumn="id"這三個(gè)屬性,但是感覺(jué)沒(méi)什么卵用。有誰(shuí)用這種方式返回成功過(guò)的嗎?
說(shuō)明:數(shù)據(jù)庫(kù)用的mysql

代碼如下:
Mapper.xml

    <resultMap id="BaseResultMap" type="com.freedom.clothing.domain.Goods">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
        <result column="match_num" jdbcType="INTEGER" property="matchNum"/>
        <result column="is_del" jdbcType="BIT" property="isDel"/>
        <result column="group_id" jdbcType="INTEGER" property="groupId"/>
        <result column="source_id" jdbcType="INTEGER" property="sourceId"/>
        <result column="user_id" jdbcType="INTEGER" property="userId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <insert id="insertSelective" parameterType="com.freedom.clothing.domain.Goods" useGeneratedKeys="true"
            keyProperty="id" keyColumn="id">
        insert into goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="url != null">
                url,
            </if>
            <if test="matchNum != null">
                match_num,
            </if>
            <if test="isDel != null">
                is_del,
            </if>
            <if test="groupId != null">
                group_id,
            </if>
            <if test="sourceId != null">
                source_id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="url != null">
                #{url,jdbcType=VARCHAR},
            </if>
            <if test="matchNum != null">
                #{matchNum,jdbcType=INTEGER},
            </if>
            <if test="isDel != null">
                #{isDel,jdbcType=BIT},
            </if>
            <if test="groupId != null">
                #{groupId,jdbcType=INTEGER},
            </if>
            <if test="sourceId != null">
                #{sourceId,jdbcType=INTEGER},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

實(shí)體類(lèi)

public class Goods {
    private Integer id;

    private String url;

    private Integer matchNum;

    private Boolean isDel;

    private Integer groupId;

    private Integer sourceId;

    private Integer userId;

    private Date createTime;

    private Date updateTime;
    
    //省略geter、setter
}
回答
編輯回答
失心人

個(gè)人觀點(diǎn):
1.按照以上配置,是可以實(shí)現(xiàn)在insert時(shí)不傳入id,id自動(dòng)生成的,但是這個(gè)需要數(shù)據(jù)庫(kù)表id也是自增。
2.按照以上配置,不是insert方法返回值為id,而是insert方法執(zhí)行后,原對(duì)象的id變得有值
源碼中會(huì)根據(jù)有沒(méi)有以上配置來(lái)決定要不要執(zhí)行id賦值方法populateKeys(),主要賦值代碼如下:

Object value = th.getResult(rs, i + 1);
// 反射賦值
metaParam.setValue(property, value);

希望對(duì)你有所幫助,謝謝

2018年1月27日 08:16
編輯回答
情皺

插入的時(shí)候把你的id字段刪除,在sql 《select 》 里刪除 keyColumn="id"就行

2017年10月23日 04:18