Mybatis動態sql技術

2022-05-30 22:30:12 字數 879 閱讀 4488

mybatis中常用動態sql:

choose when otherwise if  trim where foreach 

1,元素被用來有條件地嵌入sql片段,如果測試條件被賦值為true,則相應地sql片段將會被新增到sql語句中。

select * from courses

where tutor_id= #

and name like #

2,choose,when 和 otherwise 條件(相當於if ,else if,else)

標籤屬性:

和標籤都可以用trim標籤實現,並且底層就是通過trimsqlnode實現的。

5.where(常用於select語句)

where標籤的作用:如果該標籤包含的元素中有返回值,就插入乙個where;如果where後面的字元是以and和or開頭的,就講他們剔除。

select * from user

and name like concat('%', #, '%')

or phone=#

6.set(常用於update語句)

標籤的作用:如果該標籤包含的元素中有返回值,就插入乙個set;如果set後面的字串是以逗號結尾的,就將這個逗號剔除。

update user

email=#,

phone=#,

where uid=#

mybatis 動態sql詳解

內容 轉到原網頁insert into t blog title,content,owner values select from t blog where id update t blog set title content owner where id select from t blog se...

mybatis入門 動態sql

mybatis核心就是對sql語句進行靈活操作,通過表示式進行判斷,對sql進行靈活拼接 組裝。現有需求如下 需要查詢使用者,輸入的是使用者類,如果使用者的性別類不為空,則將性別作為查詢條件之一,如果使用者的姓名不為空,則將使用者姓名作為查詢條件之一。如果使用者兩個屬性都為空,則查詢所有使用者。將上...

MyBatis動態sql查詢

什麼是動態sql mybatis核心 對sql語句進行靈活操作,通過表示式進行判斷,對sql進行靈活拼接 組裝。需求public class userpo if test userpo.id null and id if if where select id findusercount parame...