Mybatis使用IN語句查詢

2021-09-09 06:20:36 字數 1043 閱讀 1429

一、簡介

在sql語法中如果我們想使用in的話直接可以像如下一樣使用:

select * from healthcoupon where usetype in ( '4' , '3' )

但是如果在mybatis中的使用in的話,像如下去做的話,肯定會報錯:

mapselectbyuserid(@param("usetype") string usetype)

select * from healthcoupon where usetype in (#)

其中usetype="2,3";這樣的寫法,看似很簡單,但是mybatis不支援。。但是mybatis中提供了foreach語句實現in查詢,foreach語法如下:

(一)、selectbyidset(list idlist)

如果引數的型別是list, 則在使用時,collection屬性要必須指定為 list

listselectbyidset(list idlist);

select

from t_user

where id in#

(二)、listselectbyidset(string idlist)

如果引數的型別是array,則在使用時,collection屬性要必須指定為 array

listselectbyidset(string idlist);

select

from t_user

where id in#

(三)、引數有多個時

當查詢的引數有多個時,有兩種方式可以實現,一種是使用@param("***")進行引數繫結,另一種可以通過map來傳引數。

3.1 @param("***")方式

listselectbyidset(@param("name")string name, @param("ids")string idlist);

select

from t_user

where  name=# and id in#

3.2 map方式

--------------------- 

mybatis條件查詢語句

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

mybatis使用foreach語句實現IN查詢

foreach語句中,collection屬性的引數型別可以使 list 陣列 map集合 item 表示在迭代過程中每乙個元素的別名,可以隨便起名,但是必須跟元素中的 裡面的名稱一樣。index 表示在迭代過程中每次迭代到的位置 下標 open 字首,sql語句中集合都必須用小括號 括起來 clo...

Mybatis呼叫儲存過程返回查詢語句

mybatis呼叫儲存過程返回查詢語句 1.首先要在mybatis的配置檔案中配置好引數,eg call budget deal.ap ex getyearly 這個返回型別,已經規定的返回值的各個屬性 2.其中parametertype的引數型別是 map params new hashmap y...