mybatis 輸入,輸出對映 一對一,一對多

2021-08-20 19:12:07 字數 3641 閱讀 3548

傳遞簡單型別

使用#{}佔位符,或者${}進行sql拼接。

(除order by等字段所需引數其他引數盡可能使用#{}這樣可以避免sql注入問題詳解看下)

link:

link: 

傳遞pojo物件

mybatis使用ognl表示式解析物件欄位的值,#{}或者${}括號中的值為pojo屬性名稱。

傳遞pojo包裝物件

開發中通過可以使用pojo傳遞查詢條件。

查詢條件可能是綜合的查詢條件,不僅包括使用者查詢條件還包括其它的查詢條件(比如查詢使用者資訊的時候,將使用者購買商品資訊也作為查詢條件),這時可以使用包裝物件傳遞輸入引數。

包裝物件:pojo類中的乙個屬性是另外乙個pojo。

$  $

如果引數是list集合或者陣列使用list,array(mybatis底層使用的是map集合對陣列別名是array)

resulttype(介面方法的返回值,輸出型別)

輸出簡單型別

如果有配置別名,直接寫別名,基本型別直接resulttype=別名

介面返回list集合(泛型user) 直接寫user的別名 框架會自動封裝為list集合 

resultmap

通過mybatis提供的各種標籤方法實現動態拼接sql。

需求:根據性別和名字查詢使用者

查詢sql:

select id, username, birthday, ***, address from `user` where *** = 1 and username like '%張%'
if標籤

select id, username, birthday, ***, address from `user`

where 1=1

and *** = #

and username like '%$%'

where標籤

上面的sql還有where 1=1 這樣的語句,很麻煩

可以使用where標籤進行改造

select id, username, birthday, ***, address from `user`

and *** = #

and username like

'%$%'

sql片段

sql中可將重複的sql提取出來,使用時用include引用即可,最終達到sql重用的目的。

把上面例子中的id, username, birthday, ***, address提取出來,作為sql片段,如下:

id, username, birthday, ***, address

select from `user`
foreach標籤

向sql傳遞陣列或list,mybatis使用foreach解析,如下:

根據多個id查詢使用者資訊

查詢sql:

select * from user where id in (1,10,24)
使用包裝類宣告list屬性:

select * from `user`

#

注意:因為乙個訂單資訊只會是乙個人下的訂單,所以從查詢訂單資訊出發關聯查詢使用者資訊為一對一查詢。如果從使用者資訊出發查詢使用者下的訂單資訊則為一對多查詢,因為乙個使用者可以下多個訂單

sql語句:

select

o.id,

o.user_id userid,

o.number,

o.createtime,

o.note,

u.username,

u.address

from

`order` o

left join `user` u on o.user_id = u.id

方法一:使用resulttype
select

o.id,

o.user_id

userid,

o.number,

o.createtime,

o.note,

u.username,

u.address

from

`order` o

left join `user` u on o.user_id = u.id

@test

public void testqueryorderuser()

// mybatis和spring整合,整合之後,交給spring管理

sqlsession.close();

}

方法二:使用resultmap

select

o.id,

o.user_id,

o.number,

o.createtime,

o.note,

u.username,

u.address

from

`order` o

left join `user` u on o.user_id = u.id

select

o.id,

o.user_id,

o.number,

o.createtime,

o.note,

u.username,

u.address

from

`order` o

left join `user` u on o.user_id = u.id

@test

public void testqueryorderuserresultmap()

// mybatis和spring整合,整合之後,交給spring管理

sqlsession.close();

}

案例:查詢所有使用者資訊及使用者關聯的訂單資訊。

使用者資訊和訂單資訊為一對多關係。

sql語句:

select

u.id,

u.username,

u.birthday,

u.***,

u.address,

o.id oid,

o.number,

o.createtime,

o.note

from

`user` u

left join `order` o on u.id = o.user_id

@test

public void testqueryuserorder()

// mybatis和spring整合,整合之後,交給spring管理

sqlsession.close();

}

Mybatis輸入輸出對映

1 傳遞簡單型別 select id finduserbyid parametertype int resulttype com.test.pojo.user select from user where id select 2 傳遞pojo物件 mybatis使用ognl表示式解析物件欄位的值。s...

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 ...