Building an entire Java web application with MyBatis 3.docx
- 文档编号:8897675
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:46
- 大小:40.23KB
Building an entire Java web application with MyBatis 3.docx
《Building an entire Java web application with MyBatis 3.docx》由会员分享,可在线阅读,更多相关《Building an entire Java web application with MyBatis 3.docx(46页珍藏版)》请在冰豆网上搜索。
BuildinganentireJavawebapplicationwithMyBatis3
BuildinganentireJavawebapplicationwithMyBatis3,Tomcat,MySQLandEclipse(onWindows7)
ThisisamultiparttutorialwhichwillshowyouhowtobuildaServlet/JSPbasedwebapplicationfromscratch,usingMySQL,TomcatandMyBatis(thenewversionoftheiBatisframework)onawindowsplatformusingEclipse.
Youwillmakethemostofthistutorialif
∙Youhavebuiltatleastafewsmallwebapplicationsinthepast.
∙YouknowhowtosetupTomcat5.xorhigherversion
∙YouaresomewhatfamiliarwiththeEclipseIDE
∙YouarefamiliarwithSQL.
∙Youknowhowtoset-upMySQLonyourdevelopmentboxandintegrateitwithyourjavawebapplication.
Let'sgetstarted
ToseehowTomcat,MyBatisandMySQLworktogetherwe'regoingtocreateatinyjavawebapplicationinEclipse,calledModelingAgency.
Ifyoudon'twanttofollowthewholetutorial,youcanskipthroughtothedifferentsectionsbelowandpickouttheinformationyouneed,otherwisecontinuetoPart1-TheSetup
TableofContents
∙Part1:
TheSetup
∙Part2:
TheModelingAgencyApplication
∙Part3:
CreatingtheModelingAgencyprojectinEclipse
∙Part4:
CreatingaTestServlet
∙Part5:
SettingupLogging
∙Part6:
TheJavaPOJOmodel
∙Part7:
ConfiguringMyBatis
∙Part8:
TestingMyBatiswithjUnit
∙Part9:
CreatingtheDAOLayer
∙Part10:
InitializingMyBatisonApplicationStartup
∙Part11:
FinalizingtheInterface
AttheendofthistutorialyouwillhopefullybeabletocreateabasicJavaJSP/ServletbasedwebappwithMyBatis,MySQLandTomcatinanEclipseenvironment.
YoucandownloadafullversionoffinishedModelingAgencywebapphere.
Thistutorialtookmealongtimetowrite,soifyoufounditusefulinanyway,ifyoufounderrorswithitoryouthinksomethingismissing,pleaseletmeknowandIwilldomybesttoimproveit.
Part1:
TheSetup
InstallingEclipse
ChancesareyoualreadyhaveEclipseinstalledbynow.JustmakesureyouhavetheJavaEEversionofEclipseoratleasttheWebToolsProjectinstalled.
ForthistutorialIusedJavaEEEclipse(Helios)SR1
Ifyouareinstallingeclipseforthefirsttimeusethislink:
DownloadJavaEEEclipseHelios
InstallingTomcat6
Goto:
http:
//tomcat.apache.org/index.htmlanddownloadtheappropriateTomcatdistributionyouneed.
Tomcat5.5or6shouldbothworkwiththistutorial.
ForthistutorialIusedTomcat6.0.29.
Youcandownloadthisversionhere.
InstallingMySQL
It'sassumedatthispointthatyouhaveaworkingMySQLdatabaseinstancesetupalready.IdeallyMySQLversion5.1orabove.IfyoualreadyhavetheMySQLdriversetupwithyourTomcatserveryoucanskipthissection,ifnot,readon.
TouseMySQLfromyourJavawebapplicationyouwillfirstneedtodownloadtheMySQLJavaDriver,alsoknownastheJDBCDriverforMySQL(Connector/J)
1)Goto:
anddownloadJDBCDriverforMySQL(Connector/J)ziparchive.
2)Extractthedriver.jarfileintoyourTomcatinstallation:
CATALINA_HOME/common/libfolder(Tomcat5)orCATALINA_HOME/lib(Tomcat6)
ForthistutorialIusedthemysql-connector-java-5.1.13-bin.jarbutanyversionabovethatshouldalsowork.
Tip:
AvoidputtingyourMySQLdriverjarunderWEB-INF/libasitmaycauseissuesdowntheroadwhenyouwanttousemysqlwithotherwebapps.You'llprobablyforgetaboutitanditwillcauseclasspathconflicts.SothebestbetistoleaveitinthesharedlibrariesfolderofyourTomcatserver.Thatwayallwebappswillhaveaccesstoit.
Jarlibrariesforthistutorial
Tomakethingsquickerforyou,youcandownloadallthejarfilesyouwillneedforthistutorialhere.OncewesetuptheeclipseprojectinPart3:
CreatingtheModelingAgencyprojectinEclipse,youcanjustextractallthejarshereunderthe/WEB-INF/libfolder
Howeverifyouwanttodoitmanuallyfollowtherestofthistutorial.
InstallingMyBatis
Goto:
http:
//www.mybatis.org/java.html.
ClickonDownloadthePersistenceFrameworkanddownloadthezippedarchivemybatis-3.0.4-bundle.zip(directlink)
OncewehavesetuptheModelingAgencywebapplicationinEclipseinPart3:
CreatingtheModelingAgencyprojectinEclipse,youwillextractthemybatis-3.0.4.jartothe/WEB-INF/libfolderofyourapplication.Don'tworry,I'llremindyouaboutthatlater.
Tip:
Beforeyougoanyfuther,IwouldstronglysuggestyougothroughtheverywellwrittenMyBatisdocumentationbeforeyoucontinuewiththistutorial(Especiallyifyou'veneverusediBatisorMyBatis).Itwilltakeabout30minutestoskimthroughit,andwillgiveyouanexcellentfoundationofthebasics.
downloadthedocumentation
InstallingJSTL/EL
Onelastthing.InourprojectwearegoingtousetheJSTLlibrarytomakethingseasierabit.WithacombinationoftheJSPExpressionLanguageandtheJSTLlibraryyoushouldalmostneverneedtowrite'java'codeinsideyourjspfilesanymore.TolearnmoreaboutthisreadTheEvolutionofServletsandJSP.
TomakeuseofJava'sJSTLlibraryandtheJSPExpressionLanguageinourproject,youwillneedtodownloadjstl.jarandstandard.jarfromtheJakartaTaglibsProjectextractthemtothe/WEB-INF/libfolderofyourapplication.Don'tworry,I'llremindyouaboutthislateraswell..
Thesejarscanbefoundhere:
Goto:
http:
//jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgianddownloadthe1.1.2.zipbinary.
Fordirectlinkclickhere.
InstallingLogBack(thenewLog4j)
IfyouwanttobeabletologthingstotheconsoleforthisprojectyoushouldalsoinstallLogBack,thesuccessorofLog4J
Downloadlogback-0.9.28.zip.Youwillneedtoextractthefollowingfilestothe/WEB-INF/libfolderofthewebappwewillsetupinPart3:
CreatingtheModelingAgencyprojectinEclipse.
∙logback-core-0.9.28.jar
∙logback-classic-0.9.28.jar
Thereisalsoanadditionaldepndencywhichlogbackneeds,whichyoucandownloadfromhere:
slf4j-api-1.6.1.jar
Ifyoufeellikeeverythinghereissetupcorrectly,let'smoveonto
Part2:
TheModelingAgencyApplication
ToseehowTomcat,MyBatisandMySQLworktogether,we'regoingtocreateasmallJavawebapplicationinEclipsecalledModelingAgency.
TheModelingAgencywebappdoesthefollowing:
∙Itletsaphotographer(aclientobject)bookamodel(amodelobject)foraphotographyshootataparticularplaceandtime(abookingobject).
TheInterface
Ourinterfaceisgoingtolooksomethinglikethiswhenwe'redone:
Youcanthinkoftheapplicationasanappointmentorganizerforphotographers.I'msurethiswillmakemoresensewhenyouseetheactualtables.
TheTables
Theclienttable
Themodeltable
Thebookingtable
TheSQL
HereisthesqltocreatetheMySQLdatabaseforourModelingAgency,andpopulatewiththedataabove:
createdatabasemodeling_agency;
usemodeling_agency;
grantallonmodeling_agency.*to'test'@'localhost'identifiedby'test'#feelfreetorenamethistowhateveryouwant
CREATETABLEmodeling_agency.client
(
client_idINTEGERNOTNULLAUTO_INCREMENT,
client_nameVARCHAR(45)NOTNULLDEFAULT'',
PRIMARYKEY(client_id)
);
CREATETABLEmodeling_agency.model
(
model_idINTEGERNOTNULLAUTO_INCREMENT,
model_nameVARCHAR(45)NOTNULLDEFAULT'',
PRIMARYKEY(model_id)
);
CREATETABLEmodeling_agency.booking
(
booking_idINTEGERNOTNULLAUTO_INCREMENT,
booking_client_idINTEGER,
booking_model_idINTEGER,
booking_timeTIMESTAMPNOTNULLDEFAULTCURRENT_TIMESTAMP,
booking_locationVARCHAR(100)NOTNULLDEFAULT'##NOLOCATIONSELECTED##',
PRIMARYKEY(booking_id),
CONSTRAINT`FK_booking_client`FOREIGNKEY(booking_client_id)
REFERENCESmodeling_agency.client(client_id),
CONSTRAINT`FK_booking_model`FOREIGNKEY(booking_model_id)
REFERENCESmodeling_agency.model(model_id)
);
insertintomodeling_agency.client(client_name)values("Michelangelo");
insertintomodeling_agency.client(client_name)values("Rafaelo");
insertintomodeling_agency.client(client_name)values("Leonardo");
insertintomodeling_agency.client(client_name)values("Donatello");
insertintomodeling_agency.model(model_name)values("Monalisa");
insertintomodeling_agency.model(model_name)values("PlainJane");
insertintomodeling_agency.model(model_name)values("FunkySusan");
insertintomodeling_agency.model(model_name)values("Mary");
insertintobooking(booking_client_id,booking_model_id,booking_location)values((selectclient_idfromclientwhereclient_name='Michelangelo'),(selectmodel_idfrommodelwheremodel_name='Monalisa'),"SomewhereinItaly");
insertintobooking(booking_client_id,booking_model_id,booking_location)values((selectclient_idfromclientwhereclient_name='Leonardo'),(selectmodel_idfrommodelwheremodel_name='PlainJane'),"Neartheriver");
insertintobooking(booking_client_id,booking_model_id,booking_location)values((selectclient_idfromclientwhereclient_name='Donatello'),(selectmodel_idfrommodelwheremodel_name='Mary'),"Onthebeach");
Note:
KeepinmindthemysqltestuserwesetupintheSQLabove,asitwillbethesameyouwillusewhenyouconfigurehowTomcatwillworkwithyourdatabase
Nowwe'rereadytosetuptheModelingAgencyprojectasaDynamicWebprojectineclipse.
Part3:
CreatingtheModelingAgencyprojectinEclipse
Ifyou'vefollowedtheprevioustutorialsPart1andPart2youshouldnowbereadytocreateasimplejavawebapplicationcalledModelingAgencyinEclipse,inordertolearnhowMySQL,MyBatisandTomcatworktogether.
Creatinganewproject
1.OpenupEclipse.UndertheProjectExplorertabrightclickandselectNew-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Building an entire Java web application with MyBatis
链接地址:https://www.bdocx.com/doc/8897675.html