mybatis高階操作

2021-09-23 15:31:38 字數 3695 閱讀 1251

"finduserbycondition"

resultmap

="usermap"

parametertype

="user"

>

select * from user

>

test

="username != null"

>

and username = #

if>

test

="user*** != null"

>

and *** = #

if>

where

>

select

>

"finduserinids"

resultmap

="usermap"

parametertype

="queryvo"

>

refid

="defaultuser"

>

include

>

>

test

="ids != null and ids.size()>0"

>

collection

="ids"

open

="and id in ("

close

=")"

item

="uid"

separator

=","

>

#foreach

>

if>

where

>

select

>

在sqlmapconfig.xml中,

每次新建乙個連線

從資料庫連線池中取連線

一級快取是 sqlsession 範圍的快取,當呼叫 sqlsession 的修改,新增,刪除,commit(),close()等方

法時,就會清空一級快取。

user user1 = userdao.

findbyid(41

);user user2 = userdao.

findbyid(41

);//兩次查詢為同乙個物件

user user3 = userdao.

findbyid(41

);sqlsession.

close()

;sqlsession = factory.

opensession()

;user user4 = userdao.

findbyid(41

);//兩次查詢不是同乙個物件

sqlsession.

clearcache()

;//也可以清空快取

它指的是mybatis中sqlsessionfactory物件的快取。由同乙個sqlsessionfactory物件建立的sqlsession共享其快取

sqlmapconfig.xml

>

name

="cacheenabled"

value

="true"

/>

settings

>

userdao.xml

/>

"findbyid"

parametertype

="int"

resulttype

="user"

usecache

="true"

>

select * from user where id = #

select

>

@before

//用於在測試方法執行之前執行

public

void

init()

throws exception

@after

//用於在測試方法執行之後執行

public

void

destroy()

throws exception

@test

public

void

testcache()

實體類欄位與資料庫無法對應

@select

("select * from user"

)@results

(id =

"usermap"

,value=

) list

findall()

;@select

("select * from user where id = #"

)@resultmap

(value =

) user finbyid

(integer id)

;

修改實體類

@getter

@setter

@tostring

public

class

account

implements

serializable

accountdao

@select

("select * from account"

)@results

(id =

"accountmap"

,value =

) list

findall()

;

userdao

@select

("select * from user where id = #"

)@resultmap

(value =

) user finbyid

(integer id)

;

修改實體類

@getter

@setter

@tostring

public

class

user

implements

serializable

)list

findall()

;

accountdao

@select

("select * from account where uid = #"

) list

finbyid

(integer userid)

;

在dao介面類上加註解

//@cachenamespace(blocking = true)   開啟二級快取

Mybatis 高階對映

一對一對映 association 標籤的巢狀查詢 select id,username,useremail,user role.role id from user inner join user role on user role.role id user.id where user.id sel...

Mybatis高階使用

mybatis的延遲載入 何為延遲載入 通過前面的學習,我們已經掌握了 mybatis中多表查詢的配置及實現,可以實現物件的關聯查 詢。實際開發過程中很多時候我們並不需要在載入使用者資訊時就一定要載入他的訂單資訊。此時 就是我們所說的延遲載入。作用就是在需要用到資料時才進行載入,不需要用到資料時就不...

mybatis高階查詢

sqlsession的獲取 sqlsessionfactorybean bean new sqlsessionfactorybean bean.setdatasource mysqldatasource bean.setconfiglocation new pathmatchingresourcep...