MyBatis Mapper 傳遞多個引數

2021-09-05 10:37:00 字數 1588 閱讀 5845

在pojo類對應的對映檔案中,對應的引數型別可以省略。

傳遞方式

1.介面正常書寫,對映檔案中sql語句的佔位符必須用 arg0 agr1…,或param1 param2…

介面:

public customer getcustomerwithid(integer id,string name);
對應的配置檔案中的sql語句:

select * from `customer` where cust_id=# and cust_name=#

測試類中**:

2.在介面中的形參前使用@param命名引數,對映檔案中的sql語句的佔位符用@param命名的引數

注意,用@param命名後,sql中的佔位符不僅能用命名的名字,還能用param1 param2…但是不能用arg0 arg1…

介面:

public customer getcustomerwithid(@param("id") integer id, @param("name") string name);
對應的配置檔案中的sql語句:

select * from `customer` where cust_id=# and cust_name=#

測試類中**:

3.通過map來傳遞多個引數

介面:

public customer getcustomerwithid(mapmap);
配置檔案中的sql語句:

select * from `customer` where cust_id=# and cust_name=#

測試類中的**:

hashmaphashmap = new hashmap<>();

hashmap.put("id",2);

hashmap.put("name","李白");

注意:使用map傳遞多個引數,對映檔案中的引數佔位符必須和map中的string型別的欄位名稱一樣

4.通過pojo類傳遞多個引數

與map傳遞多個引數類似,只不過對映檔案中的引數佔位符必須和pojo類中的字段完全一致才可以

介面:

public customer getcustomerwithid(customer customer);
配置檔案中的sql語句:

select * from `customer` where cust_id=# and cust_name=#

pojo類:

public class customer
測試類中的**:

customer customer = new customer();

customer.setcust_name("李白");

customer.setcust_id(2);

MyBatis Mapper對映檔案

add parametertype student usegeneratedkeys true keyproperty id insert into students name,schoolname,age,birth values insert add parametertype person o...

mybatis mapper呼叫mysql儲存過程

mybatis版本 3.4.4 select id calltest statementtype callable select 注意 parametermap已被捨棄,請直接在sql語句中定義傳參型別。注意 out引數必須指定jdbctype void calltest mapparams 注意 ...

mybatis mapper檔案裡的

簡單介紹 翻看以前在學校寫的 發現那時候有乙個sql寫的很有意思,用到了 標籤,和我現在寫的雖然有點差別,但是效果一樣 update event title event where id update event title event where id 解釋屬性,順便再補充幾個常用的屬性 inse...