Mybatis輸入輸出對映

2022-03-23 05:42:11 字數 1998 閱讀 8055

1、傳遞簡單型別

<

select

id="finduserbyid"

parametertype

="int"

resulttype

="com.test.pojo.user"

>

select * from user where id = #

select

>

2、傳遞pojo物件

mybatis使用ognl表示式解析物件欄位的值。

<

select

id="finduserbyuser"

parametertype

="com.test.pojo.user"

resulttype

="com.test.pojo.user"

>

select * from user where id=# and username like '%$%'

select

>

測試:

@test

public

void

testfinduserbyuser()

3、傳遞包裝物件

定義包裝物件將查詢條件(pojo)以類組合的方式包裝起來。

public

class

queryvo

public

void

setuser(user user)

}

<

select

id="finduserlist"

parametertype

="com.test.pojo.queryvo"

resulttype

="com.test.pojo.user"

>

select * from user where id=# and username=#

select

>

測試**:

@test

public

void

testfinduserlist()

4、傳遞hashmap

sql對映檔案:

測試:

@test

public

void

testfinduserbyhashmap()

1、輸出簡單型別

輸出簡單型別必須查詢出來的結果集有一條記錄,最終講第乙個欄位的值轉換為輸出型別。

2、輸出pojo物件

參考輸入pojo物件, 使用session呼叫selectone查詢單條記錄。

3、輸出pojo列表

使用session的selectlist方法獲取pojo列表。

注:輸出pojo物件和輸出pojo列表在sql中定義的resulttype是一樣的。

resultmap:可以指定pojo將查詢結果對映為pojo,但需要pojo的屬性名和sql查詢的列名一致方可對映成功。

如果sql查詢欄位名和pojo的屬性名不一致,可以通過resultmap將欄位名和屬性名作乙個對應關係 ,resultmap實質上還需要將查詢結果對映到pojo物件中。

resultmap可以實現將查詢結果對映為複雜型別的pojo,比如在查詢結果對映物件中包括pojo和list實現一對一查詢和一對多查詢。

定義resultmap:

測試**:

@test

public

void testfinduserlistresultmap() throws

exception

mybatis 輸入對映和輸出對映

輸入對映和輸出對映 複製昨天的工程,按照下圖進行 最終效果如下圖 parametertype 輸入型別 傳遞簡單型別 參考第一天內容。使用 佔位符,或者 進行sql拼接。傳遞pojo物件 參考第一天的內容。mybatis使用ognl表示式解析物件欄位的值,或者 括號中的值為pojo屬性名稱。傳遞po...

Mybats 輸入輸出對映

parametertype 輸入型別 pojo包裝物件 新建包裝pojo物件queryvo 包裝pojo author steven public class queryvo public void setuser user user 對映檔案與sql select from user where ...

mybatis高階 輸入對映和輸出對映

首先是輸入型別,輸入型別分為以下幾種 1.基本型別。2.pojo物件型別。3.pojo包裝物件。所謂的包裝物件,可以理解為,乙個物件裡面包含著另外乙個物件。簡單輸入型別,比較簡單。在傳入引數的時候,直接將引數型別配置到parametertype中,如下 select from user where ...