mybatis的mapper引數傳遞

2022-07-05 12:42:12 字數 4363 閱讀 8239

簡單引數傳遞是指:

actor selectactorbyid(long id);

<

select

id="selectactorbyid"

resulttype

="canger.study.chapter04.bean.actor"

>

select actor_id as id, first_name as firstname ,last_name as lastname

from actor

where actor_id=#

select

>

待續

boolean insertactorbymap(map map);

<

insert

id="insertactorbymap"

parametertype

="map"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

mapmap = new hashmap()

};

boolean insertactorbystring(string var1,string var2);

<

insert

id="insertactorbystring"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

system.out.println(result);

boolean insertactorbyparamstring(@param("firstname")string var1, @param("lastname")string var2);

<

insert

id="insertactorbyparamstring"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

system.out.println(result)

boolean insertactorbymixedstring(string var1, @param("lastname")string var2);

<

insert

id="insertactorbymixedstring"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

或者<

insert

id="insertactorbymixedstring"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

需要注意的是,如果此時採用命名引數(如@param("actor"))傳遞單個自定義型別引數,則不能直接引用屬性名,而需要採用級聯引用(actor.firstname或 param1.firstname)

boolean insertactor(actor actor);

<

insert

id="insertactor"

parametertype

="canger.study.chapter04.bean.actor"

>

insert into actor(first_name,last_name) values (#,#)

insert

>

actor actor = new actor("james","harden");

這種情況和傳遞多個基本引數沒有什麼打的區別,唯一需要注意的是需要使用級聯引用,才能使用自定義引數屬性的值

此時的引用規則如下:

listselectactorbyidlist( listlist);

list

selectactorbyidset( setset);

list

selectactorbyidarray( long array);

<

select

id="selectactorbyidlist"

resulttype

="canger.study.chapter04.bean.actor"

>

select actor_id as id, first_name as firstname ,last_name as lastname

from actor

where actor_id in

<

foreach

collection

="list"

item

="actorid"

index

="index"

open

="("

close

=")"

separator

=","

>

#

foreach

>

select

>

<

select

id="selectactorbyidset"

resulttype

="canger.study.chapter04.bean.actor"

>

select actor_id as id, first_name as firstname ,last_name as lastname

from actor

where actor_id in

<

foreach

collection

="collection"

item

="actorid"

index

="index"

open

="("

close

=")"

separator

=","

>

#

foreach

>

select

>

<

select

id="selectactorbyidarray"

resulttype

="canger.study.chapter04.bean.actor"

>

select actor_id as id, first_name as firstname ,last_name as lastname

from actor

where actor_id in

<

foreach

collection

="array"

item

="actorid"

index

="index"

open

="("

close

=")"

separator

=","

>

#

foreach

>

select

>

system.out.println(actors);

});new long);

此時預設名稱 collection、list、array均失效,只能使用指定的名稱或param1進行引用

listselectactorbyidlist( @param("actorlist") listlist);

<

select

id="selectactorbyidlist"

resulttype

="canger.study.chapter04.bean.actor"

>

select actor_id as id, first_name as firstname ,last_name as lastname

from actor

where actor_id = #

select

>

無特殊之處

mybatis中的mapper例項化

很多人用過mybatis,或者使用過springdata,那麼和傳統的jdbc相比,差別在 呢?傳統的dao層,介面需要有實現,需要連線發出sql,需要接收返回值,但是mybatis只需要實現介面,而不需要實現類,而且神奇的是,可以通過spring的註解,直接把介面的實現物件給取出,大家都知道,介面...

Mybatis的Mapper與sql引數傳遞

1.簡單型別 只有乙個引數 select id,role name,note fron t role where id public role getrole interger id 這裡的 的 id 可以是任意值,但一般與傳入的引數形參相同,方便理解 2.傳入引用變數 insert into t ...

mybatis中mapper配置詳解

mybatis 的真正強大在於它的對映語句,也是它的魔力所在。由於它的異常強大,對映器的 xml 檔案就顯得相對簡單。如果拿它跟具有相同功能的 jdbc 進行對比,你會立即發現省掉了將近 95 的 mybatis 就是針對 sql 構建的,並且比普通的方法做的更好。1 select 查詢 查詢語句是...