MyBatis提供了批量插入數(shù)據(jù)的方法,可以通過(guò)以下步驟進(jìn)行批量新增數(shù)據(jù):
void insertBatch(List<YourEntity> list);
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO your_table (column1, column2, ...) VALUES
<foreach collection="list" item="item" separator=",">
(#{item.property1}, #{item.property2}, ...)
</foreach>
</insert>
這里的your_table
是數(shù)據(jù)庫(kù)表名,column1
, column2
等是數(shù)據(jù)庫(kù)表的列名,YourEntity
是對(duì)應(yīng)的實(shí)體類(lèi),property1
, property2
等是實(shí)體類(lèi)的屬性名。
List<YourEntity> list = new ArrayList<>();
// 添加要新增的數(shù)據(jù)到list中
yourMapper.insertBatch(list);
這里的yourMapper
是你自己定義的Mapper接口的實(shí)例。
通過(guò)以上步驟,你可以使用MyBatis實(shí)現(xiàn)批量新增數(shù)據(jù)操作。