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語句,這個有待討論。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章