MyBatis3UserGuide.docx
《MyBatis3UserGuide.docx》由会员分享,可在线阅读,更多相关《MyBatis3UserGuide.docx(122页珍藏版)》请在冰豆网上搜索。
MyBatis3UserGuide
MyBatis3
UserGuide
WarningaboutCopyingCodefromthisDocument
No,thisisnotalegalwarning.Itisonetohelpyoukeepyoursanity.Modernword
processorsdoagreatjobofmakingtextreadableandformattedinanaesthetically
pleasingway.However,theyalsotendtocompletelyruincodeexamplesby
insertingspecialcharacters,sometimesthatlookexactlythesameastheoneyou
thinkyouwant.“Quotes”andhyphensareaperfectexample–thequotesand
hyphenyouseetotheleftwillnotworkasquotesinanIDEortexteditor,atleast
notthewayyouintend.
Soreadthisdocument,enjoyitandhopefullyitishelpfultoyou.Whenitcomesto
codeexamples,seekouttheexamplesincludedwiththedownload(includingunit
testsetc.),orexamplesfromthewebsiteormailinglist.
Helpmakethisdocumentationbetter…
Ifyoufindthisdocumentationlackinginanyway,ormissingdocumentationforafeature,
thenthebestthingtodoislearnaboutitandthenwritethedocumentationyourself!
Weacceptpublicdocumentationcontributionsthroughourwikiat:
You’rethebestauthorofthisdocumentation,peoplelikeyouhavetoreadit!
Contents
WhatisMyBatis?
.........................................................................................................................................5
GettingStarted.............................................................................................................................................5
BuildingSqlSessionFactoryfromXML.......................................................................................................5
BuildingSqlSessionFactorywithoutXML..................................................................................................6
AcquiringaSqlSessionfromSqlSessionFactory........................................................................................6
ExploringMappedSQLStatements..........................................................................................................7
ANoteaboutNamespaces....................................................................................................................8
ScopeandLifecycle..................................................................................................................................9
MapperConfigurationXML........................................................................................................................10
properties...............................................................................................................................................11
settings...................................................................................................................................................12
typeAliases.............................................................................................................................................13
typeHandlers..........................................................................................................................................14
objectFactory.........................................................................................................................................15
plugins....................................................................................................................................................16
environments.........................................................................................................................................17
transactionManager............................................................................................................................18
dataSource.........................................................................................................................................19
mappers.................................................................................................................................................21
SQLMapXMLFiles.....................................................................................................................................21
select......................................................................................................................................................22
insert,update,delete..............................................................................................................................24
sql...........................................................................................................................................................26
Parameters.............................................................................................................................................26
MyBatis3-UserGuide
29May20104
resultMap...............................................................................................................................................28
AdvancedResultMapping..................................................................................................................30
id,result.............................................................................................................................................32
SupportedJDBCTypes........................................................................................................................33
constructor.........................................................................................................................................33
association.........................................................................................................................................34
collection............................................................................................................................................37
discriminator......................................................................................................................................40
cache......................................................................................................................................................41
UsingaCustomCache.........................................................................................................................43
cache-ref................................................................................................................................................44
DynamicSQL..............................................................................................................................................44
if.............................................................................................................................................................44
choose,when,otherwise........................................................................................................................45
trim,where,set......................................................................................................................................45
foreach...................................................................................................................................................47
JavaAPI......................................................................................................................................................49
DirectoryStructure................................................................................................................................49
SqlSessions.............................................................................................................................................50
SqlSessionFactoryBuilder....................................................................................................................50
SqlSessionFactory...............................................................................................................................52
SqlSession...........................................................................................................................................54
SelectBuilder..............................................................................................................................................60
SqlBuilder...................................................................................................................................................63
MyBatis3-UserGuide
29May20105
WhatisMyBatis?
MyBatisisafirstclasspersistenceframeworkwithsupportforcustomSQL,storedproceduresand
advancedmappings.MyBatiseliminatesalmostalloftheJDBCcodeandmanualsettingofparameters
andretrievalofresults.MyBatiscanusesimpleXMLorAnnotationsforconfigurationandmap
primitives,MapinterfacesandJavaPOJOs(PlainOldJavaObjects)todatabaserecords.
GettingStarted
EveryMyBatisapplicationcentersaroundaninstanceofSqlSessionFactory.ASqlSessionFactory
instancecanbeacquiredbyusingtheSqlSessionFactoryBuilder.SqlSessionFactoryBuildercanbuilda
SqlSessionFactoryinstancefromanXMLconfigurationfile,offromacustompreparedinstanceofthe
Configurationclass.
BuildingSqlSessionFactoryfromXML
BuildingaSqlSessionFactoryinstancefromanXMLfileisverysimple.Itisrecommendedthatyouusea
classpathresourceforthisconfiguration,butyoucoulduseanyReaderinstance,includingonecreated
fromaliteralfilepathorafile:
//URL.MyBatisincludesautilityclass,calledResources,thatcontainsa
numberofmethodsthatmakeitsimplertoloadresourcesfromtheclasspathandotherlocations.
Stringresource