BATIS Data Mapper (A.k.a SQL Maps) : Roman Dagi" Pichlík (HTTP://WWW - Sweb.cz/pichlik)
BATIS Data Mapper (A.k.a SQL Maps) : Roman Dagi" Pichlík (HTTP://WWW - Sweb.cz/pichlik)
a SQL Maps)
Roman Dagi Pichlk (http://www.sweb.cz/pichlik)
2
iBATIS targets
provide a simple framework to provide 80% of
DB! f"#$tio#alit% "si#& o#l% '0% of t(e $ode
(elps red"$e t(e amo"#t of ava)DB! $ode t(at
is #eeded to a$$ess relatio#al data*ase
simpl% maps avaBea#s to SQL stateme#ts "si#&
a ver% simple +ML des$riptor
easil% maps avaBea#s o*,e$ts to
-reparedStateme#t parameters a#d .es"ltSets
is #ot i#te#ded to repla$e /*,e$t0.elatio#al
Mappi#& (/.M) tools s"$( as 1i*er#ate2 D/
3
iBATIS features
$o##e$tio# a#d tra#sa$tio# ma#a&eme#t
$o#verti#& SQL .es"ltSets i#to ava o*,e$ts
setti#& ava o*,e$ts as 3"er% parameters
$o#fi&"ra*le a#d fle4i*le res"lt $a$(i#&
res"lt pa&i#& a#d la5% loadi#&
$omple4 o*,e$ts &rap( mappi#& (6762 6782 M78)
la5% loadi#& a#d 896 pro*lem sol"tio#
d%#ami$all% $o#str"ed SQL a#d SQL re"se
4
iBATIS concept overview
6.provide a# o*,e$t as a parameter
'.e4e$"te t(e mapped stateme#t
:.si#&le o*,e$t2 $olle$tio# of o*,e$ts or rows effe$ted
is ret"r#ed.
5
Sample Weblog Application
6
SQ !aps config
<sqlap!on"ig#
<settings cacheo$els%nable$&'t()e' enhancement%nable$&'t()e' laz*+oa$ing%nable$&'t()e' e((o(,(acing%nable$&'"alse'
ma-Req)ests&'./' ma-0essions&'12' ma-,(ansactions&'3' )se0tatement4amespaces&'t()e' /#
<t(ansactionanage( t*pe&'5D6!'#
<$ata0o)(ce t*pe&'07P+%' #
<p(ope(t* name&'5D6!.D(i8e(' 8al)e&'com.m*sql.9$bc.D(i8e('/#
<p(ope(t* name&'5D6!.!onnection:R+'
8al)e&'9$bc:m*sql://$agi/$e8;)se:nico$e&t()e<cha(acte(%nco$ing&:,=>?'/#
<p(ope(t* name&'5D6!.:se(name' 8al)e&'bloge('/#
<p(ope(t* name&'5D6!.Passwo($' 8al)e&'changeit'/#
<p(ope(t* name&'Pool.a-im)m@cti8e!onnections' 8al)e&'12'/#
<p(ope(t* name&'Pool.a-im)m7$le!onnections' 8al)e&'3'/#
<p(ope(t* name&'Pool.a-im)m!hecko)t,ime' 8al)e&'1/2222'/#
<p(ope(t* name&'Pool.,ime,oAait' 8al)e&'322'/#
<p(ope(t* name&'Pool.PingB)e(*' 8al)e&'select now()'/#
<p(ope(t* name&'Pool.Ping%nable$' 8al)e&'"alse'/#
<p(ope(t* name&'Pool.Ping!onnectionsCl$e(,han' 8al)e&'1'/#
<p(ope(t* name&'Pool.Ping!onnections4ot:se$=o(' 8al)e&'1'/#
<p(ope(t* name&'Pool.B)ieto$e' 8al)e&'"alse'/#
</$ata0o)(ce#
</t(ansactionanage(#
<sqlap (eso)(ce&'cz/sweb/pichlik/ibatis/samples/6log.-ml'/#
<sqlap (eso)(ce&'cz/sweb/pichlik/ibatis/samples/Post.-ml'/#
<sqlap (eso)(ce&'cz/sweb/pichlik/ibatis/samples/!omment.-ml'/#
</sqlap!on"ig#
0tatements (eso)(ces
"
Select statement #efinition an# use
0tatement $e"inition
<sqlap namespace&'6log'#
<t*pe@lias alias&'blog' t*pe&'cz.sweb.pichlik.ibatis.samples.$omain.6log'/#
<D>> 0elects all blogs>>#
<select i$&'get@ll' (es)lt!lass&'blog'#
select
blogEi$ as blog7$F
blogEname as blog4ameF
bloge(E"i(stname as bloge(=i(st4ameF
bloge(Es)(name as bloge(0)(4ameF
bloge(Eemail as bloge(%mailF
c(eate$
"(om
blogs
</select#
</sqlap#
0tatement )se
Rea$e( con"igRea$e( & new 7np)t0t(eamRea$e((this.class.getReso)(ce@s0t(eam('0qlap!on"ig.-ml'))G
//0qlap!lient6)il$e( is )se$ to (ea$ con"ig)(ation settings
0qlap!lient sqlap!lient & 0qlap!lient6)il$e(.b)il$0qlap!lient(con"igRea$e()G
//0qlap!lient is th(ea$ sa"e client )se$ "o( inte(acting with 0B+apsF it is also possible to )se 0qlap0ession pe( single th(ea$
+ist<6log# blogs & sqlap!lient.q)e(*=o(+ist('6log.get@ll' F n)ll)G
$
Insert statement #efinition an# use
<inse(t i$&'sa8e' pa(amete(!lass&'blog'#
inse(t into blogs
(blogEi$F blogEnameF bloge(E"i(stnameF bloge(Es)(nameF bloge(Eemail)
8al)es
(Hblog7$HF Hblog4ameHF Hbloge(=i(st4ameHF Hbloge(0)(4ameHF Hbloge(%mailH)
</inse(t#
p)blic 8oi$ a$$6log("inal 6log blog) th(ows 0B+%-ception I
0qlap0ession session & n)llG
t(* I
session & sqlMapClient.openSession();
//Dema(cates the beginning o" a t(ansaction scope. ,(ansactions m)st be p(ope(l* committe$
//o( (olle$ back to be e""ecti8e.
session.startTransaction();
//%-ec)tes a mappe$ 0B+ 740%R, statement.
session.insert("Blog.save", blog);
//!ommits the c)((entl* sta(te$ t(ansaction.
session.commitTransaction();
J "inall* I
//%n$s a t(ansaction an$ (olls back i" necessa(*. 7" the t(ansaction has been sta(te$F b)t not committe$F it will be
// (olle$ back )pon calling en$,(ansaction().
i"(session D& n)ll)I
t(*I
session.endTransaction();
J "inall* I
session.close();
J
J
J
J
Note: Execution of update and delete statement is same, but session.update and session.delete are used
instead of session.insert. It is also possible to call stored procedures.
%
Select statement result mapping
#o wa% to spe$if% t(e t%pes of t(e o"tp"t $ol"m#s
#o wa% to a"tomati$all% load related data
($omple4 properties)
sli&(t performa#$e $o#se3"e#$e
Di(ectl* mappe$ (es)lt app(oach
<select i$&'get@ll' (es)lt!lass&'blog'#
select
blogEi$ as blog7$F
blogEname as blog4ameF
bloge(E"i(stname as bloge(=i(st4ameF
bloge(Es)(name as bloge(0)(4ameF
bloge(Eemail as bloge(%mailF
c(eate$ ....
&'
Select statement result mapping
$omple4 propert% mappi#& (6762 67 M2 87M)
$ol"m# a#d ,ava t%pe defi#itio#
#"ll val"e repla$eme#t
res"ltMap re"se (e4te#d)
Res)ltap app(oach
<(es)ltap i$&K(es)ltap4ameK class&Ksome.$omain.!lassK Le-ten$s&Kpa(ent>(es)ltapKM#
<(es)lt p(ope(t*&Kp(ope(t*4ameK col)mn&K!C+:4E4@%K
Lcol)mn7n$e-&K1KM L9a8a,*pe&KintKM L9$bc,*pe&K4:%R7!KM
Ln)llNal)e&K>OOOOOOKM Lselect&KsomeCthe(0tatementKM
/#
<(es)lt PP/#
<(es)lt PP/#
<(es)lt PP/#
</(es)ltap#
&&
Select statement result mapping
res"lt map for mappi#& ret"r#ed $ol"m#s to ,ava
*ea# properties
res"lt map $a# *e "sed for a#% sele$t stateme#t
<(es)ltap i$&'blog-result-mapping' class&'blog'#
<(es)lt col)mn&'blogEi$' p(ope(t*&'blog7$'/#
<(es)lt col)mn&'blogEname' p(ope(t*&'blog4ame'/#
<(es)lt col)mn&'bloge(E"i(stname' p(ope(t*&'bloge(=i(st4ame'/#
<(es)lt col)mn&'bloge(Es)(name' p(ope(t*&'bloge(0)(4ame'/#
<(es)lt col)mn&'bloge(Eemail' p(ope(t*&'bloge(%mail'/#
<(es)lt col)mn&'c(eate$' p(ope(t*&'c(eate$'/#
</(es)ltap#
<select i$&'"in$6*7$' (es)ltap&'blog-result-mapping'#
select Q "(om blogs whe(e blogEi$&Hblog7$H
</select#
<select i$&'"in$6*4ame' (es)ltap&'blog-result-mapping'#
select Q "(om blogs whe(e blogEname&Hblog4ameH
</select#
&2
&(& mapping )neste# propert*+
<t*pe@lias alias&'comment' t*pe&'cz.sweb.pichlik.ibatis.samples.$omain.!omment'/#
<(es)ltap i$&'comment>(es)lt>mapping' class&'comment'#
<(es)lt col)mn&'c.commentEi$' p(ope(t*&'comment7$'/#
<(es)lt col)mn&'c.te-t' p(ope(t*&'te-t'/#
<(es)lt col)mn&'c.a)tho(Ename' p(ope(t*&'a)tho(4ame'/#
<(es)lt col)mn&'c.a)tho(Eemail' p(ope(t*&'a)tho(%mail'/#
<(es)lt col)mn&'c.a)tho(E)(l' p(ope(t*&'a)tho(:R+'/#
<(es)lt col)mn&'c.c(eate$' p(ope(t*&'c(eate$'/#
<(es)lt col)mn&'p.postEi$' p(ope(t*&'post.post7$'/#
<(es)lt col)mn&'p.title' p(ope(t*&'post.title'/#
<(es)lt col)mn&'p.$esc' p(ope(t*&'post.$esc'/#
<(es)lt col)mn&'p.c(eate$' p(ope(t*&'post.c(eate$'/#
</(es)ltap#
<select i$&'get@ll6*Post7$' (es)ltap&'comment>(es)lt>mapping'#
select
c.QF
p.Q
"(om
posts pF comments c
whe(e
p.postEi$ & Hpost7$H an$ c.postEi$ & p.postEi$
</select#
&3
&(& mapping )separate select+
<t*pe@lias alias&'comment' t*pe&'cz.sweb.pichlik.ibatis.samples.$omain.!omment'/#
<(es)ltap i$&'comment>(es)lt>mapping' class&'comment'#
<(es)lt col)mn&'c.commentEi$' p(ope(t*&'comment7$'/#
<(es)lt col)mn&'c.te-t' p(ope(t*&'te-t'/#
<(es)lt col)mn&'c.a)tho(Ename' p(ope(t*&'a)tho(4ame'/#
<(es)lt col)mn&'c.a)tho(Eemail' p(ope(t*&'a)tho(%mail'/#
<(es)lt col)mn&'c.a)tho(E)(l' p(ope(t*&'a)tho(:R+'/#
<(es)lt col)mn&'c.c(eate$' p(ope(t*&'c(eate$'/#
<(es)lt col)mn&'Ipost7$&postEi$J' p(ope(t*&'post' select&'Post."in$6*7$77'/#
</(es)ltap#
<select i$&'"in$6*7$77' (es)ltap&'gene(ic>post>(es)lt>mapping'#
select
p.Q
"(om
blogs bF
posts p
whe(e
p.postEi$&Hpost7$H an$ p.blogEi$ & b.blogEi$
</select#
&4
&(! mapping
<sqlap namespace&'Post'#
<t*pe@lias alias&'post' t*pe&'cz.sweb.pichlik.ibatis.samples.$omain.Post'/#
<(es)ltap i$&'post>(es)lt>mapping' class&'post'#
<(es)lt col)mn&'p.postEi$' p(Rope(t*&'post7$'/#
<(es)lt col)mn&'p.title' p(ope(t*&'title'/#
<(es)lt col)mn&'p.$esc' p(ope(t*&'$esc'/#
<(es)lt col)mn&'p.c(eate$' p(ope(t*&'c(eate$'/#
<(es)lt col)mn&'b.blogEi$' p(ope(t*&'blog.blog7$'/#
<(es)lt col)mn&'b.blogEname' p(ope(t*&'blog.blog4ame'/#
<(es)lt col)mn&'b.bloge(E"i(stname' p(ope(t*&'blog.bloge(=i(st4ame'/#
<(es)lt col)mn&'b.bloge(Es)(name' p(ope(t*&'blog.bloge(0)(4ame'/#
<(es)lt col)mn&'b.bloge(Eemail' p(ope(t*&'blog.bloge(%mail'/#
<(es)lt col)mn&'b.blogEc(eate$' p(ope(t*&'blog.c(eate$'/#
<(es)lt col)mn&'Ipost7$&postEi$J' p(ope(t*&'comments' select&'!omment.get@ll6*Post7$' /#
</(es)ltap#
<select i$&'get@ll6*6log7$' (es)ltap&'post>(es)lt>bmapping'#
select P
</sqlap#
<sqlap namespace&'!omment'#
<t*pe@lias alias&'comment' t*pe&'cz.sweb.pichlik.ibatis.samples.$omain.!omment'/#
<select i$&'get@ll6*Post7$' (es)ltap&'comment>(es)lt>mapping'#
select ...
&5
,esult cac-ing
simpl% *% spe$if%i#& t(e $a$(eModel parameter
.ead o#l% a#d .ead);rite $a$(e s"pported
vario"s t%pe of $a$(e impleme#tatio# (L.<2
Memor%2 =I=/2 /S!a$(e)
parametri5ed fl"s( *e(avio"r (time2 stateme#t
e4e$"tio#)
<cacheo$el i$&'blog>cache' t*pe &'+R:'#
....
</cacheo$el#
<select i$&'get@ll' (es)lt!lass&'blog' cacheo$el&'blog>cache'#
....
</select#
&6
,ea# .nl* /ac-e
ret"r#s ide#ti$ i#sta#$es of $a$(ed o*,e$t
s(ared per sessio# (>>>)
o*,e$ts read from a read0o#l% $a$(e s(o"ld #ot
*e modified
<cacheo$el i$&'blog>cache' t*pe &'+R:' readnl!""true" seriali#e""$alse"#
<"l)sh7nte(8al ho)(s&'/S'/#
<"l)shCn%-ec)te statement&'6log.sa8e'/#
<"l)shCn%-ec)te statement&'6log.)p$ate'/#
<"l)shCn%-ec)te statement&'6log.$elete'/#
<p(ope(t* name&'cache>size' 8al)e&'1222' /#
</cacheo$el#
<D>> 0elects all blogs>>#
<select i$&'get@ll' (es)lt!lass&'blog' cacheo$el&'blog>cache'#
...
&"
,ea#0Write cac-e
differe#t i#sta#$es ($opies) of t(e $a$(ed
o*,e$t to ea$( sessio#
ever% $a$(ed o*,e$t m"st *e seriali5a*le
ea$( sessio# $a# safel% modif% ret"r#ed
i#sta#$e
<cacheo$el i$&'blog>cache>(w' t*pe &'+R:' readnl!""$alse" seriali#e""true"#
<"l)sh7nte(8al ho)(s&'/S'/#
<"l)shCn%-ec)te statement&'6log.sa8e'/#
<"l)shCn%-ec)te statement&'6log.)p$ate'/#
<"l)shCn%-ec)te statement&'6log.$elete'/#
<p(ope(t* name&'cache>size' 8al)e&'1222' /#
</cacheo$el#
<D>> 0elects all blogs>>#
<select i$&'get@ll' (es)lt!lass&'blog' cacheo$el&'blog>cache>(w'#
...
&$
Inline 1arameter !aps
pla$es t(e avaBea#s propert% #ames i#li#e wit(
t(e Mapped Stateme#t
a#% Mapped Stateme#t t(at (as #o e4pli$it
parameterMap spe$ified will *e parsed for i#li#e
parameters
<inse(t i$&'sa8e' pa(amete(!lass&'cz.sweb.pichlik.ibatis.samples.$omain.!omment'#
inse(t into comments
(commentEi$F postEi$F te-tF a)tho(EnameF a)tho(E)(lF c(eate$)
8al)es
(Hcomment7$HF Hpost.post7$HF Hte-tHF Ha)tho(4ameHF Ha)tho(:R+HF Hc(eate$H)
</inse(t#
&%
1arameter !aps
mappi#& avaBea#s properties to t(e parameters
of a stateme#t
<pa(amete(ap i$&Kpa(amete(ap4ameK Lclass&Kcom.$omain.P(o$)ctKM#
<pa(amete( p(ope(t* &Kp(ope(t*4ameK L9$bc,*pe&KN@R!T@RKM L9a8a,*pe&Kst(ingKM
Ln)llNal)e&K4:%R7!KM Ln)ll&K>OOOOOOOKM/#
<pa(amete( PP /#
<pa(amete( PP /#
</pa(amete(ap#
<pa(amete(ap i$&Kinse(t>p(o$)ct>pa(amK class&Kcom.$omain.P(o$)ctK#
<pa(amete( p(ope(t*&K$esc(iptionK /#
<pa(amete( p(ope(t*&Ki$K/#
</pa(amete(ap#
<statement i$&Kinse(tP(o$)ctK pa(amete(ap&inse(t>p(o$)ct>pa(amK#
inse(t into PRCD:!, (PRDED%0!R7P,7C4F PRDE7D) 8al)es (;F;)G
</statement#
2'
2*namic mappe# statement an# SQ fragment
inclu#es
<sql i$&'postsEso(tingE"(agment'#
<isP(ope(t*@8ailable p(epen$&'o($e( b*' p(ope(t*&'0CR,'#
p.c(eate$ D%0!
</isP(ope(t*@8ailable#
</sql#
<select i$&'onl*Posts' (es)lt!lass&'9a8a.)til.Tashap'#
select
p.Q
"(om
posts p
whe(e
p.blogEi$ & Hblog7$H
<incl)$e (e"i$&'postsEso(tingE"(agment'/#
</select#
2&
S3l!ap/lient an# S3l!apSession A1I
%U%!:,%
0%+%!,
6@,!T
$elete(0t(ing i$F Cb9ect pa(amete(Cb9ect)
inse(t(0t(ing i$F Cb9ect pa(amete(Cb9ect)
)p$ate(0t(ing i$F Cb9ect pa(amete(Cb9ect)
q)e(*=o(+ist(0t(ing i$F Cb9ect pa(amete(Cb9ect)
q)e(*=o(+ist(0t(ing i$F Cb9ect pa(amete(Cb9ectF int skipF int ma-)
q)e(*=o(ap(0t(ing i$F Cb9ect pa(amete(Cb9ectF 0t(ing ke*P(op)
q)e(*=o(ap(0t(ing i$F Cb9ect pa(amete(Cb9ectF 0t(ing ke*P(opF 0t(ing 8al)eP(op)
q)e(*=o(Paginate$+ist(0t(ing i$F Cb9ect pa(amete(Cb9ectF int page0ize)
q)e(*AithRowTan$le((0t(ing i$F Cb9ect pa(amete(Cb9ectF RowTan$le( (owTan$le()
q)e(*=o(Cb9ect(0t(ing i$F Cb9ect pa(amete(Cb9ect)
q)e(*=o(Cb9ect(0t(ing i$F Cb9ect pa(amete(Cb9ectF Cb9ect (es)ltCb9ect
sta(t6atch ()
e-ec)te6atch()
22
,esources
(ttp7))www.swe*.$5)pi$(lik)i*atis)*lo&er.5ip 0
e4ample appli$atio#
(ttp7))www.swe*.$5)pi$(lik)i*atis)prese#tatio#.pdf
0 t(is prese#tatio#
(ttp7))i*atis.apa$(e.or&) IBATIS (omepa&e