Mapper.xml代码如下:

<insert id="batchInsert" parameterType="java.util.List">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
        SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT INTO table_name (field1, field2, field3, field4, field5)
    VALUES
    <foreach collection="list" item="item" separator=",">
        (#{item.field1}, #{item.field2}, #{item.field3}, #{item.field4}, #{item.field5})
    </foreach>
</insert>

Java代码如下:

public interface TableMapper {
    @Insert("CALL batchInsert()")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void batchInsert(List<Table> list);
}

其中,Table为表对应的实体类,batchInsert为存储过程的名称。执行该方法后,插入成功后id字段会被自动赋值,并返回给调用方

假设一张表有5个字段java代码中通过存储过程批量插入到该表并回显id的操作并显示mapperxml的代码

原文地址: https://www.cveoy.top/t/topic/hbWc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录