mybatis判斷用insert還是update

2021-10-22 10:14:28 字數 558 閱讀 3748

下邊看一下mybatis的對映檔案。

select count(*) from teacher where teacher_id = #

0">

update event

teacher_name= #,

teacher_id = #

insert into teacher(teacher_id,teacher_name) values (#,#)12

3456

78910

1112

1314

1516

1718

19可以看到mybatis的實現思路也是先查詢id是否存在,在根據count判斷是insert還是update。

說明1.實現原理是selectkey做第一次查詢,然後根據結果進行判斷,所以這裡的order="before"是必須的,也是因before,所以沒法通過標籤來臨時儲存中間的值,只能在入參中增加屬性來存放。

2.就上面這個例子而言,就要求實體類中包含count屬性(可以是別的名字)。否則selectkey的結果沒法儲存,如果入參是個map型別,就沒有這個限制。

Mybatis日期型別的關係判斷

if test executestartdate null and executestartdate and execute time to date yyyy mm dd hh24 mi ss if if test executeenddate null and executeenddate an...

mybatis 使用if 判斷字串

mybatis 動態sql 中if判斷使用的ognl表示式,現在分3中情況說明並驗證。傳入的itemcode為引數傳入mybatis if test itemcode null and itemcode and itemcode 1 單個字元的數字型字串 例如 傳入引數 itemcode 1 以下寫...

Mybatis日期型別的關係判斷

and execute time to date yyyy mm dd hh24 mi ss if and execute time to date yyyy mm dd hh24 mi ss if 解決方法有兩種,一種是使用 來表示大於和小於關係,這樣,在解析時,這些特殊字元會被轉義成所匹配的運算...