在工程应用中,通常会遇到多表更新操作,在集成mybatis中需要在执行插入语句后返回主键id进行后续的表更新操作,下面对其实现的方式记录分享出来,以应对不同的应用场景。

1、在xml文件中应用useGeneratedKeys和keyProperty

在xml文件中,insert标签属性中,添加useGeneratedKeys和keyProperty,类似如下:

	<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>

通过这种方式插入的值,经常会返回1,原因是因为他这里的意思是返回当前影响的行数,不能准确返回你新插入的id值,有时候返回的结果是准确的。

2、在xml文件中应用selectKey

在xml文件中,insert标签属性中,添加selectKey标签属性,各个属性值含义:resultType:查询结果的类型;keyProperty:把查询的值赋给谁;order:在插入前还是后执行,id在insert语句插入之后才会生成id,所以要在插入之后执行,所以此处order=after。类似如下:

	<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>

注意,keyProperty属性的值,一定为对应对象的主键id,否则获取不到返回的主键id。

3、注解方式

在mapper层用注解的方式,用@SelectKey注解,里面属性含义:resultType:查询结果的类型,keyProperty:把查询的值赋给谁; statement:查找最后一个插入的id; keyColumn:查询的是哪一列; before:是否在插入之前执行, id在insert语句插入之后才会生成id,所以要在插入之后执行,所以此处before=false。

@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);

其中,“select last_insert_id()”这条语句,它是配合插入语句一块只用的,在insert语句执行成功后可以返回新增数据的id。

4、使用方式

在service层,类似如下:

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

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐