ibatis 批量更新(一)

2022-01-31 11:08:28 字數 4153 閱讀 1543

1.4.2.3 批量修改

支援單個動態更新、批量動態更新

<

update

id="updateconsult_scheduledynamic"

parameterclass

="map"

>

update consult_schedule

<

dynamic

prepend

="set"

>

<

isnotempty

prepend

=","

property

="org_code"

>

org_code = #org_code#

isnotempty

>

<

isnotempty

prepend

=","

property

="depent_id"

>

depent_id = #depent_id#

isnotempty

>

<

isnotempty

prepend

=","

property

="depent_name"

>

depent_name = #depent_name#

isnotempty

>

<

isnotempty

prepend

=","

property

="doctor_id"

>

doctor_id = #doctor_id#

isnotempty

>

<

isnotempty

prepend

=","

property

="doctor_name"

>

doctor_name = #doctor_name#

isnotempty

>

<

isnotempty

prepend

=","

property

="doctor_phone"

>

doctor_phone = #doctor_phone#

isnotempty

>

<

isnotempty

prepend

=","

property

="schedule_date"

>

schedule_date = to_date(#schedule_date#,'yyyy-mm-dd')

isnotempty

>

<

isnotempty

prepend

=","

property

="week_txt"

>

week_txt = #week_txt#

isnotempty

>

<

isnotempty

prepend

=","

property

="wb_type"

>

wb_type = #wb_type#

isnotempty

>

<

isnotempty

prepend

=","

property

="close_tz"

>

close_tz = #close_tz#

isnotempty

>

<

isnotempty

prepend

=","

property

="replace_tz"

>

replace_tz = #replace_tz#

isnotempty

>

<

isnotempty

prepend

=","

property

="doctor_id_tz"

>

doctor_id_tz = #doctor_id_tz#

isnotempty

>

<

isnotempty

prepend

=","

property

="doctor_name_tz"

>

doctor_name_tz = #doctor_name_tz#

isnotempty

>

<

isnotnull

prepend

=","

property

="remark"

>

remark = #remark#

isnotnull

>

dynamic

>

where consult_schedule_id in ($consult_schedule_id$)

update

>

注意:a.這種批量更新,只能統一更改某(幾)個表字段的值(狀態);

b.批量操作時,ibatis通過使用in()函式來實現,in()函式裡面的內容只能通過"$變數$"來實現,不能通過"#變數#"實現;

c.根據id進行修改,如果id是字串,前台傳參時,需手動拼接上字串。

舉例:以id是字串為例,講解前台如何獲取頁面的值,並傳至後台及ibatis如何對映傳遞的值

第一部分:js取值

見文章:核取方塊-checkbox

第二部分:dao調取sql

/**

* 動態修改排班表表記錄

* *

@param

parammap 排班表表map物件集合

* @return

返回操作影響的行數

* @throws

daoexception */

public

int updateconsult_scheduledynamic(map parammap) throws

daoexception

catch

(sqlexception e)

}

第三部分:sql,見上面

說明:a.使用 in(#consult_schedule_id#),則解析出來的結果是:in('id1,id2');(錯誤)

b.使用 in($consult_schedule_id$),解析出來的結果是:in('id1','id2');(正確)

錯誤用法

情景描述

virtual_card表中有兩個唯一的字段:virtual_id和index_id,想根據其中乙個字段進行更新;

但是,傳參可能是0個,1個或2個,考慮到能匹配不到的情況,所以使用了1=1

動態sql

update virtual_card set

index_id=#index_id#

where 1=1

<

isnotempty

prepend

="and"

property

="virtual_id"

>

virtual_id=#virtual_id#

isnotempty

>

<

isnotempty

prepend

="and"

property

="index_id"

>

index_id=#index_id#

isnotempty

>

當傳遞的引數匹配不到的時候,執行的結果是:

說明:where 1=1條件相當於沒用,與下面的sql起到的效果一致 

update virtual_card set index_id=?
將該表中該字段的值全部更新成了?,所以一定不能使用這個動態sql。

ibatis批量操作補充

ibatis批量操作 這文章的補充吧。review 的時候發現乙個頁面應用排序設定功能,原先 中,進行了迴圈update實現。雖然應用較少,不過無謂浪費資料庫鏈結實在不是乙個好的想法。所以,我覺得應該杜絕迴圈中的任何資料可操作才能避免很多地雷的埋下。修改後 批量更新語句的寫法,基於ibatis,只使...

ibatis批量處理插入例項

getsqlmapclienttemplate execute new sqlmapclientcallback sqlmapclienttemplate為org.springframework.orm.ibatis.sqlmapclienttemplate sqlmapexecutor 為ibat...

mysql 批量更新 MySQL批量更新

我有2個表 mysql data details accounts invoices 理想情況下,每個data details都應具有accounts invoices id.data details有乙個帶有accounts invoices主鍵的外來鍵 由於某種原因,有data details記...