mysql批量操作

备注:以下写法是结合ibatis,如果使用mybatis换成相应的格式即可
1.mysql批量插入
insert into t_wx_attach
(target_id, target_type, original_name, size, suffix, url,
creator_id, modifier_id, create_time, modify_time, del_flag)
values

(#[].targetId#,
#[].targetType#,
#[].originalName#,
#[].size#,
#[].suffix#,
#[].url#,
#[].creatorId#,
#[].creatorId#,
NOW(),
NOW(),
0)

//这种批量写法不需要进行额外的其他配置。

2.mysql批量更新:

update t_wx_repair_project_rel pr set
pr.main_worker_id = #[].mainWorkerId#,
pr.other_worker_ids = #[].otherWorkerIds#,
pr.estimated_start_working_time = #[].estimatedStartWorkingTime#,
pr.estimated_finish_time = #[].estimatedFinishTime#,
pr.modify_time = #[].modifyTime#,
pr.modifier_id = #[].modifierId#,
pr.modifier_name = #[].modifierName#
where pr.id = #[].id#

//这种写法其实就是执行多条语句,原本mysql不支持执行多条语句,需要在mysql连接的信息上加上两个参数 &rewriteBatchedStatements=true&allowMultiQueries=true

3.mysql批量插入/更新,当一条语句主键或者唯一索引重复的时候,进行更新否则进行插入
写法一:
insert into

(repair_sheet_id,repair_factory_id,first_end_date,
pick_up_car_time,return_car_time,is_deleted,status,
in_schedule,out_schedule) values

(#list[].repairSheetId#,#list[].repairFactoryId#,#list[].firstEndDate#,
#list[].pickUpCarTime#,#list[].returnCarTime#,#list[].isDeleted#,#list[].status#,
#list[].inSchedule#,#list[].outSchedule#)

on duplicate key update
repair_sheet_id = VALUES(repair_sheet_id),
repair_factory_id = VALUES(repair_factory_id),
first_end_date = VALUES(first_end_date),
pick_up_car_time = VALUES(pick_up_car_time),
return_car_time = VALUES(return_car_time),
is_deleted = VALUES(is_deleted),
status = VALUES(status),
in_schedule = VALUES(in_schedule),
out_schedule = VALUES(out_schedule)

写法二:
replace into t_wx_attach
(target_id, target_type, original_name, size, suffix, url,
creator_id, modifier_id, create_time, modify_time, del_flag)
values

(#[].targetId#,
#[].targetType#,
#[].originalName#,
#[].size#,
#[].suffix#,
#[].url#,
#[].creatorId#,
#[].creatorId#,
NOW(),
NOW(),
0)

//网上说replace into 如果主键或者唯一索引重复情况下,是先删除后插入,但是通过binlog日志查看,是直接执行update语句,这个有待讨论。

你可能感兴趣的:(mysql)