Mybatis has three ways to return the primary key id after executing the insert statement

In engineering applications, multi-table update operations are usually encountered. In integrating mybatis, it is necessary to return the primary key id after executing the insert statement for subsequent table update operations. The following is a record of how it is implemented to cope with different applications. Scenes.

1. Apply useGeneratedKeys and keyProperty in the xml file

In the xml file, in the insert tag attribute, add useGeneratedKeys and keyProperty, similar to the following:

	<insert id="insert" parameterType="com.***.Attachment" useGeneratedKeys="true" keyProperty="attachment.id"  keyColumn="id">
        insert into b_attachment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="attachment.id != -1">
                id,
            </if>
            <if test="attachment.fileName != null and attachment.fileName !=''">
                file_name,
            </if>
            <if test="attachment.remarks != null and attachment.remarks !=''">
                remarks,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="attachment.id != -1">
                #{attachment.id,jdbcType=BIGINT},
            </if>
            <if test="attachment.fileName != null and attachment.fileName !=''">
                #{attachment.fileName,jdbcType=VARCHAR},
            </if>
            <if test="attachment.remarks != null and attachment.remarks !=''">
                #{attachment.remarks,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

The value inserted in this way will often return 1. The reason is because it means to return the number of rows currently affected. It cannot accurately return the newly inserted ID value. Sometimes the returned result is accurate.

2. Apply selectKey in xml file

In the xml file, in the insert tag attribute, add the selectKey tag attribute. The meaning of each attribute value: resultType: the type of query result; keyProperty: to whom the query value is assigned; order: whether to execute before or after inserting, id in the insert statement The id will not be generated until after the insertion, so it must be executed after the insertion, so order=after here. Something like this:

	<insert id="insert" parameterType="com.***.Attachment">
        <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="attachment.id">
            select LAST_INSERT_ID()
        </selectKey>
        insert into b_attachment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="attachment.id != -1">
                id,
            </if>
            <if test="attachment.fileName != null and attachment.fileName !=''">
                file_name,
            </if>
            <if test="attachment.remarks != null and attachment.remarks !=''">
                remarks,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="attachment.id != -1">
                #{attachment.id,jdbcType=BIGINT},
            </if>
            <if test="attachment.fileName != null and attachment.fileName !=''">
                #{attachment.fileName,jdbcType=VARCHAR},
            </if>
            <if test="attachment.remarks != null and attachment.remarks !=''">
                #{attachment.remarks,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

Note that the value of the keyProperty attribute must be the primary key id of the corresponding object, otherwise the returned primary key id cannot be obtained.

3. Annotation method

Use annotation method in the mapper layer, use @SelectKey annotation, the meaning of the attributes inside: resultType: type of query result, keyProperty: to whom the query value is assigned; statement: find the last inserted id; keyColumn: which column is queried ; before: Whether to execute before inserting, id will not be generated until after the insert statement is inserted, so it must be executed after inserting, so before=false here.

@Insert(insert into b_attachment values(#{id},#{fileName},#{remarks})
@SelectKey(statement = "select last_insert_id() from dual", before = false, resultType = Interger.class, keyColumn = "id", keyProperty = "attachment.id")
int add(@Param("attachment") Attachment attachment);

Among them, the statement "select last_insert_id()" is used together with the insert statement. After the insert statement is successfully executed, the id of the newly added data can be returned.

4. How to use

In the service layer, it is similar to the following:

	...
	orderMapper.insert(order);//先执行插入order的语句
    Integer id = order.getId();//通过order.getId()获取你新插入数据的id值
    ...

Guess you like

Origin blog.csdn.net/leijie0322/article/details/132417909