Mybatis gets the primary key value id of the newly inserted data

After the insertion operation, the primary key ID is returned immediately. I consulted a lot of information and used keyProperty and useGeneratedKeys attributes. The useGeneratedKeys parameter only takes effect for the insert statement and defaults to false. When set to true, it means that if the inserted table has an auto-increment column as the primary key, JDBC is allowed to support automatic generation of the primary key, and the automatically generated primary key can be returned.

Mainly in this way:

<insert id="insertFileRecord" useGeneratedKeys="true" keyProperty="id">
      insert xxx
    </insert>

But it didn't take effect, so I looked for a way to use useGeneratedKeys="true" and found that I wrote less.

<insert id="insertFileRecord" parameterType="FileRecord" useGeneratedKeys="true" keyProperty="id">
      insert xxx
    </insert>

parameterType can only tell it, in which entity class, add the attributes "useGeneratedKeys" and "keyProperty" in the Mybatis Mapper file, where keyProperty is the attribute name of the Java object, not the field name of the table.

But it still didn't work. I began to suspect that my auto-incremented id was of long type, but in fact, it was not. I added such an annotation in the mapper interface class.

int insertFileRecord(@Param("fileRecord") FileRecord fileRecord);

And in the insert statement fileRecord.fileName, remove @Param, finally see the id,

Finally, the complete writing is attached:

mapper interface:

    /**
     * @Description: 新增数据
     * @Author: wj
     * @param: cc.jz.work.entity.FileRecord
     * @Return: 影响行数
     * @Date: 2022-03-31 10:37:18
     */
    int insertFileRecord(FileRecord fileRecord);

 xml:

<insert id="insertFileRecord" parameterType="cc.jz.work.entity.FileRecord"  useGeneratedKeys="true" keyProperty="id">
        insert into
        file_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fileAddress != null and fileAddress != ''">
                file_address,
            </if>
            <if test="fileName != null and fileName != ''">
                file_name,
            </if>
            <if test="uploadUserId != null">
                upload_user_id,
            </if>
            <if test="uploadTime != null">
                upload_time,
            </if>
            <if test="status != null and status != ''">
                status,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fileAddress != null and fileAddress != ''">
                #{fileAddress,jdbcType=VARCHAR},
            </if>
            <if test="fileName != null and fileName != ''">
                #{fileName,jdbcType=VARCHAR},
            </if>
            <if test="uploadUserId != null">
                #{uploadUserId,jdbcType=INTEGER},
            </if>
            <if test="uploadTime != null">
                #{uploadTime,jdbcType=TIMESTAMP},
            </if>
            <if test="status != null and status != ''">
                #{status,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
 

Guess you like

Origin blog.csdn.net/heni6560/article/details/124194277