mybatis傳入多個引數

2021-08-04 15:50:42 字數 1918 閱讀 2094

寫在開頭:

#,

還可以通過for來進行遍歷。

一、單個引數:

public list getxxbeanlist(@param("id")string id);  

select t.* from tablename t where t.id= # select> 其中方法名和id一致,#{}中的引數名與方法中的引數名一致, 這裡採用的是@param這個引數,實際上@param這個最後會被mabatis封裝為map型別的。

select 後的字段列表要和bean中的屬性名一致, 如果不一致的可以用 as 來補充。

二、多引數:

方案1

public list<***bean> get***beanlist(string xxid, string xxcode);  

select t.* from tablename where id = # and name = # select> 由於是多引數那麼就不能使用parametertype, 改用#{index}是第幾個就用第幾個的索引,索引從0開始

方案2(推薦)基於註解

public list<***bean> get***beanlist(@param("id")string id, @param("code")string code);  

select t.* from tablename where id = # and name = # select> 由於是多引數那麼就不能使用parametertype, 這裡用@param來指定哪乙個

三、map封裝多引數:

public list<***bean> get***beanlist(hashmap map);  

select 欄位... from *** where id=# code = # select> 其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那個就在#{}使用那個,map如何封裝就不用了我說了吧。

四、list封裝in:

public list<***bean> get***beanlist(list list);    select 欄位... from *** where id in        #   foreach> select> foreach 最後的效果是select 欄位... from *** where id in ('1','2','3','4') 

五、selectlist()只能傳遞乙個引數,但實際所需引數既要包含string型別,又要包含list型別時的處理方法:

將引數放入map,再取出map中的list遍歷。如下:

list list_3 = new arraylist();

map map2 = new hashmap(); list.add("1");

list.add("2");
map.put("list", list); //**id

map.put("sitetag", "0");//**型別

public list getsysinfo(map map2) 

   select t.syssiteid, t.sitename, t1.mznum as sitetagnum, t1.mzname as sitetag, t.url, t.iconpath from td_web_syssite t left join td_mz_mzdy t1 on t1.mznum = t.sitetag and t1.mztype = 10 where t.sitetag = # and t.syssiteid not in  # foreach> select>

分類: 

mybatis

mybatis 傳入多個引數

一 單個引數 public list getxxbeanlist param id string id select t.from tablename t where t.id select 其中方法名和id一致,中的引數名與方法中的引數名一致,這裡採用的是 param這個引數,實際上 param這...

MyBatis傳入多個引數

一 單個引數 複製 public list getxxbeanlist string xxcode select t.from tablename t where t.id 其中方法名和id一致,中的引數名與方法中的引數名一直,我這裡採用的是 bean是採用的短名字,select 後的字段列表要和b...

MyBatis傳入多個引數的問題

原文 一 單個引數 public listgetxxbeanlist string xxcode select t.from tablename t where t.id 其中方法名和id一致,中的引數名與方法中的引數名一直,我這裡採用的是 bean是採用的短名字,select 後的字段列表要和be...