mybatis傳參的幾種方式

2022-02-22 08:50:38 字數 1020 閱讀 7588

1,@param

@參考文章

@select("select s_id id,s_name name,class_id classid from student where  s_name= # and class_id = #")

publicstudent select(@param("aaaa") string name,@param("bbbb")intclass_id);

@select(....)註解的作用就是告訴mybatis框架,執行括號內的sql語句

where  s_name= # and class_id = # 表示sql語句要接受2個引數,乙個引數名是aaaa,乙個引數名是bbbb,如果要正確的傳入引數,那麼就要給引數命名,因為不用xml配置檔案,那麼我們就要用別的方式來給引數命名,這個方式就是@param註解

給入參 string name 命名為aaaa,然後sql語句....where  s_name= # 中就可以根據aaaa得到引數值了

2,不用@param

@參考文章

@select("select * from company where cid=# and status!=-1")

listgetallcombyid(int cid);

3,傳實體類(前提實體類具有該屬性,且該屬性有值或有預設值)

@參考文章

@select("select * from company where cid=# and status!=-1")

listgetallcombyid(company c);

4,分頁(不在本專題研究範圍內)

Mybatis的幾種傳參方式詳解

前言 單個pttzndv引數 單個引數的傳參比較簡單,可以是任意形式的,比如 或者 但是為了開發規範,盡量使用和入參時一樣。mapper如下 userinfo selectbyuserid string userid xml如下 select from user info where user id...

Mybatis傳參方式

mybatis傳多個引數 三種解決方案 據我目前接觸到的傳多個引數的方案有三種。第一種方案 dao層的函式方法 1public user selectuser string name,string area 123select from user user t where user name and...

Mybatis傳參方式總結

mybatis 在對 sql 語句進行預編譯之前,會對 sql 進行動態解析,解析為乙個 boundsql 物件,也是在此處對動態 sql 進行處理的。在動態 sql 解析階段,和 會有不同的表現 select from user where name 在動態解析的時候,會解析成乙個引數標記符。就是...