MyBatis條件查詢

2021-08-22 00:04:48 字數 1474 閱讀 1271

1. 單條件查詢

public

//通過使用者名單條件查詢

public listgetuserlistbyusername(string username);}

select * from smbms_user where username like concat('%',#,'%')

select>

2.多條件查詢(將查詢條件封裝成物件入參)

public

//通過使用者名稱和密碼多條件查詢

public listgetuserlistbyusernameandpassword(user user);}

select * from smbms_user where username like concat('%',#,'%') and userpassword = #

select>

3.多條件查詢(將查詢條件封裝成map物件入參)

public

//map通過使用者名稱和密碼多條件查詢

public listgetuserlistbymap(mapusermap);}

select * from smbms_user where username like concat('%',#,'%') and userpassword = #

select>

//這種做法更加靈活,不管是什麼型別的引數,或者多少個引數,我們都可以把它封裝成map資料結構進行入參,通過map即可獲取傳入的值。

4.使用@param註解實現多引數入參(4個引數以下推薦使用)

public

//註解@param:通過使用者名稱和密碼多條件查詢

public listgetuserlistbyparam(@param("username")string username,@param("userpassword")string userpassword)}

select * from smbms_user where username like concat('%',#,'%') and userpassword = #

select>

//用@param來指定哪乙個,就不能使用parametertype

5.使用@param註解實現多引數入參(第二種)

public

public listgetuserlistbyuseridandpwd(string username,string userpassword);

}

//不需要寫parametertype引數

select * from smbms_user where username= # and userpassword= #

select>

//#{index}是第幾個就用第幾個的索引,索引從0開始

mybatis條件查詢語句

經常見到前端頁面上有列表展示資料的形式。有的列表每乙個欄位列可能都會配有乙個上三角下三角按鈕,提示該列資料正序或者倒敘排列,對應的sql的關鍵字就是asc,desc。以下給出乙個模板。select user id,user name,user age from user order by user ...

Mybatis 多條件動態查詢

例 簡單的乙個查詢 select from student tbl st where st.student name like concat concat 但是此時如果studentname是null或空字串,此語句很可能報錯或查詢結果為空。此時我們使用if動態sql語句先進行判斷,如果值為null...

mybatis對映和條件查詢

mybatis繫結介面 mybatis對映 資料庫的字段與實體類的對映 把公共的的sql語句提取出來 select link id,site id,link name,link url,link image,clicks,create time,update time,sort,is show,st...