MyBatis批量新增数据写法

快速生成工具一键直达

表结构:

drop table if exists `t_user`;
create table `t_user`(
    `id` bigint(20) not null auto_increment comment '主键id',
    `name` varchar(32) not null default '' comment '姓名',
    `phone` varchar(32) not null default '' comment '手机',
    `create_time` datetime not null default current_timestamp comment '创建时间',
    `update_time` datetime not null default current_timestamp on update current_timestamp comment '更新时间',
    primary key (`id`),
    unique key `phone` (`phone`),
    key `name` (`name`)
) engine=innodb auto_increment=1000 default charset=utf8mb4 comment='用户表';

正常使用insert语句插入多个值

XML文件:

<insert id="batchInsert">
    insert into t_user (`name`, `phone`, `create_time`, `update_time`)
    values
    <foreach collection="list" item="item" separator=",">
    (
        #{item.name},
        #{item.phone},
        #{item.createTime},
        #{item.updateTime}
    )
    </foreach>
</insert>

执行样例SQL:

insert into t_user (`name`, `phone`, `create_time`, `update_time`)
values
( 'ccf80555-64bd-45eb-be2f-b48bf568', '78e8b481-24c9-410c-a0de-8c5a4a10', '2023-12-04 16:02:31.802', '2023-12-04 16:02:31.802' ) ,
( '1e8eccc4-6147-4e68-ae1e-ca192814', 'ab3fbb62-a00b-478d-9251-4e10ca77', '2023-12-04 16:02:31.802', '2023-12-04 16:02:31.802' ) ,
( '1e6a1e71-9817-481b-afef-1d3cbf83', '36f3184f-8b7a-4694-904f-12a5ca9a', '2023-12-04 16:02:31.802', '2023-12-04 16:02:31.802' )

觉得内容还不错?打赏个钢镚鼓励鼓励!!👍