mybatis動態SQL之if標籤

2022-06-08 04:51:12 字數 522 閱讀 1723

我們根據實體類的不同取值,使用不同的 sql 語句來進行查詢。比如在 id 如果不為空時可以根據 id 查詢,如果 username 不同空時還要加入使用者名稱作為條件。這種情況在我們的多條件組合查詢中經常會碰到。

/**

* 根據使用者資訊,查詢使用者列表

* @param user

* @return

*/listfindbyuser(user user);

select * from user where 1=1

and username like #

and address like #

注意:if標籤的 test 屬性中寫的是物件的屬性名,如果是包裝類的物件要使用 ognl 表示式的寫法。另外要注意 where 1=1 的作用~!

@test

public void testfindbyuser()

}

mybatis學習之動態sql

1 select查詢 簡單的select類似如下 select id findbyid resultmap studentresult parametertype integer select from t student where id select 1 if 常用於各種查詢的條件判斷部分 se...

(七)mybatis學習之動態SQL

mybatis的核心是對sql語句進行靈活的操作,通過表示式進行判斷,對sql進行靈活拼接 組裝。動態sql包括 if choose when,otherwise where set trim foreach sql片段 if標籤比較簡單,這裡記錄一下文件內容的例子,通過看例子,就清楚的知道if是如...

MyBatis 之 對映檔案 動態sql

mybatis 之 對映檔案 動態sql f標籤 作為判斷入參來使用的,如果符合條件,則把if標籤體內的sql拼接上。注意 用 if進行判斷是否為空時,不僅要判斷 null 也要判斷空字串 where標籤 會去掉條件中的第乙個and符號。public class user public class ...