在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

我要返回mybatis自增主鍵id,添加了useGeneratedKeys="true" keyProperty="id" keyColumn="id"這三個屬性,但是感覺沒什么卵用。有誰用這種方式返回成功過的嗎?
說明:數(shù)據(jù)庫用的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>

實體類

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
}
回答
編輯回答
失心人

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

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

希望對你有所幫助,謝謝

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

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

2017年10月23日 04:18