mybatis和spring進行整合,來編寫一個商品查詢的工程。
一.整合dao
1.sqlmapconfig.xml
mybatis自己的配置檔案---sqlmapconfig.xml:
2.applicationcontext-dao.xml
spring的配置檔案
配置:
資料源
sqlsessionfactory
mapper掃描器
applicationcontext-dao.xml内容:
3.逆向工程生成po類及mapper(單表增删改查)
如圖.逆向工程生成的檔案
将生成的檔案拷貝至工程中。
4.手動定義商品查詢mapper
針對綜合查詢mapper,一般情況會有關聯查詢,建議自定義mapper
5.自定義mapper的映射配置檔案itemsmappercustom.xml
sql語句:
select * from items where items.name like '%洗衣機%'
itemsmappercustom.xml:
6.自定義mapper的映射接口檔案itemsmappercustom.java
二.整合service
讓spring管理service接口。
1.定義service接口
2.接口的實作itemsserviceimpl:
3.在spring容器配置service(applicationcontext-service.xml)
建立applicationcontext-service.xml,檔案中配置service。
4.事務控制(applicationcontext-transaction.xml)
在applicationcontext-transaction.xml中使用spring聲明式事務控制方法。
5.整合springmvc
springmvc.xml
建立springmvc.xml檔案,配置處理器映射器、擴充卡、視圖解析器。
6.配置前端控制器
web.xml:
7.編寫controller(就是handler)
8.編寫jsp如圖-2.b.jsp的目錄.png
9.加載spring容器
将mapper、service、controller加載到spring容器中。
圖application配置檔案
建議使用通配符加載上邊的配置檔案。
在web.xml中,添加spring容器監聽器,加載spring容器。
凡是符合/web-inf/classes/spring/applicationcontext-*.xml規則的配置檔案就自動加載了。
測試:
現将工程部署到tomcat中,啟動tomcat,我們來測試
http://localhost:8080/springmvc_mybatis1507/queryitems.action
測試結果如圖
實際的資料庫表中的情況如圖資料庫情況
說明測試成功!
整個的整合和環境配置工作就到此結束了,接下來就是開發了。
在此基礎上接下來的開發請看以後釋出的總結文章。
轉載請注明出處:http://blog.csdn.net/acmman/article/details/46997813