mybatisを使用してデータを一括挿入するときにプライマリ・キーの自己増加IDの値を入力します.
一、データベースを挿入するオブジェクトモデルを定義する
idは、データベース内の自己増加プライマリ・キーidに対応します.大量にデータを挿入した後、データベースに割り当てられたid値で属性を埋め戻します.
二、mapper層インタフェースの定義
userListパラメータに@Param注記は付けません.
三、XMLの構成
注意:collectionの値はlistと固定されています.
package com.postgres.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 。
*/
@ApiModel(value = " ")
@Data
public class User {
/**
* ID
*/
@ApiModelProperty(value = "id", example = "-1")
private int id = -1;
/**
*
*/
@ApiModelProperty(value = " ", example = " ")
private String name = "";
}
idは、データベース内の自己増加プライマリ・キーidに対応します.大量にデータを挿入した後、データベースに割り当てられたid値で属性を埋め戻します.
二、mapper層インタフェースの定義
package com.postgres.mapper;
import com.postgres.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserMapper {
/**
* 。
*
* @param userList
*/
void insertUserV3(List userList);
}
userListパラメータに@Param注記は付けません.
三、XMLの構成
INSERT INTO t_user (name)
VALUES
(#{item.name})
注意:collectionの値はlistと固定されています.