付費下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、SSM 框架一一詳細整合教程 (Spring+SpringMVC+MyBatis) SSM框架詳細整合教程(Spring+SpringMVC+MyBatis) 1、基本概念 1.1、 Spring Spring是一個開源框架,Spring是于2003年興起的一個輕量級的Java開發(fā)框架,由RodJohnson在其著作ExpertOne-On-OneJ2EEDevelopmentandDesign中闡述的部分理念和原型衍生而來。它是為了解決企業(yè)應(yīng)用開發(fā)的復雜性而創(chuàng)建的。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限于服務(wù)器端的開發(fā)。 從簡
2、單性、 可測試性和松耦合的角度而言, 任彳Java應(yīng)用都可以從Spring中受益。簡單來說,Spring是一個輕量級的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架。 1.2、 SpringMVC SpringMVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在 SpringWebFlow里面。SpringMVC分離了控制器、模型對象、分派 器以及處理程序?qū)ο蟮慕巧?,這種分離讓它們更容易進行定制。 1.3、 MyBatis MyBatis本 是apache的 一 個 開 源 項 目iBatis,2010年 這 個 項 目 由apachesoftwarefoundation遷移至了g
3、ooglecode,并且改名為MyBatis。MyBatis是一個基于Java的 持 久 層 框 架 。iBATIS提 供 的 持 久 層 框 架 包 括SQLMaps和DataAccessObjectsDAO)MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及結(jié)果集的檢索。 MyBatis使用簡單的XML或注解用于配置和原始映射,將接口和Java的POJOs(PlainOldJavaObjects普通的Java對象)映射成數(shù)據(jù)庫中的記錄。 2、開發(fā)環(huán)境搭建如果需要,參看之前的博文: http:/ 3、MavenWeb項目創(chuàng)建 如果需要,參看之前的博文: http:/ 建,參看上面
4、的博文。這次整合我分了2個配置文件,分別是 spring-mybatis.xml,包含spring和mybatis的配置文件,還有個是spring-mvc的配置文件,此外有2個資源文件:pertis和 10perties。完整目錄結(jié)構(gòu)如下(最后附上源碼下載地址,不建議直接使用源碼,因為此教程已經(jīng)有了全部代碼): 使用框架都是較新的版本: Spring4.0.2RELEASE SpringMVC4.0.2RELEASE MyBatis3.2.6 Maven引入需要的JAR包 為了方便后面說的時候不需要引入JAR包,我這里直接給出所有需要的JAR包,這都是基本的JAR
5、包,每個包的是干什么的都有注釋,就不再多說了。 pom.xml 4.0.2.RELEASE 3.2.6 1.7.7 1.2.17 junit junit 4.11test org.springframework spring-core $spring.versionorg.springframeworkspring-web$spring.version org.springframeworkspring-oxm$spring.version org.springframeworkspring-tx$spring.version org.springframeworkspring-jdbc $s
6、pring.versionorg.springframeworkspring-webmvc$spring.version org.springframeworkspring-aop$spring.versionorg.springframework spring-context-support $spring.version org.springframework spring-test $spring.versionorg.mybatis mybatis $mybatis.versionorg.mybatis mybatis-spring 1.2.2javax javaee-api 7.0m
7、ysql mysql-connector-java 5.1.30commons-dbcp commons-dbcp 1.2.2jstl jstl 1.2 log4jlog4j $log4j.versioncom.alibaba fastjson 1.1.41org.slf4j slf4j-api $slf4j.version org.slf4j slf4j-log4j12 $slf4j.versionorg.codehaus.jackson jackson-mapper-asl 1.9.13 commons-fileuploadcommons-fileupload1.3.1 commons-i
8、o commons-io 2.4 commons-codec commons-codec 1.9 Spring與MyBatis的整合 所有需要的JAR包都引入以后,首先進行Spring與MyBatis的整合, 然后再進行JUnit測試,先看一個項目結(jié)構(gòu)圖: 、 建立JDBC屬性文件 perties(文件編碼修改為utf-8) driver=com.mysql.jdbc.Driver url=jdbc:mysql:11:8080/db_zsl username=demao password=demao 定義初始連接數(shù) initialSize=0 定義最大連接
9、數(shù) maxActive=20 定義最大空閑 maxIdle=20 定義最小空閑 minIdle=1 定義最長等待時間 maxWait=60000 、 建立spring-mybatis.xml配置文件 這個文件就是用來完成spring和mybatis的整合的。 這里面也沒多少行配置,主要的就是自動掃描,自動注入,配置數(shù)據(jù)庫。注釋也很詳細,大家看看就明白了。 spring-mybatis.xml xmlns:xsi=http:/2001/XMLSchema-instancexmlns:p=/schema/p xmlns:
10、context=/schema/context xmlns:mvc=/schema/mvc xsi:schemaLocation=/schema/beans /schema/beans/spring-beans-3.1.xsd/schema/context http:/www.springframework.
11、org/schema/context/spring-context-31xsd/schema/mvc /schema/mvc/spring-mvc40.xsd class=org.springframework.beans.factory.config.PropertyPlaceholder Configurer destroy-method=closeclass=org.springframework.jdbc.datasource.DataSourceTransaction
12、Manager 、 Log4j的配置 為了方便調(diào)試,一般都會使用日志來輸出信息,Log4j是Apache 的一個開放源代碼項目,通過使用Log4j,我們可以控制日志信息輸 送的目的地是控制臺、文件、GUI組件,甚至是套接口服務(wù)器、NT的事件記錄器、UNIXSyslog守護進程等;我們也可以控制每一條日志的輸出格式;通過定義每一條日志信息的級別,我們能夠更加細致地控制日志的生成過程。 Log4j的配置很簡單,而且也是通用的,下面給出一個基本的配置,換到其他項目中也無需做多大的調(diào)整,如果想做調(diào)整或者想了解 Log4j的各種配置,參看我轉(zhuǎn)載的一篇博文,很詳細: http:/ 下面給出配置文件目錄:
13、10perties 定義LOG輸出級別 10g4j.rootLogger=INFO,Console,File 定義日志輸出目的地為控制臺 10g4j.appender.Console=org.apache.log4j.ConsoleAppender 10g4j.appender.Console.Target=System.out 可以靈活地指定日志輸出格式,下面一行是指定具體的格式 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 10g4j.appender.Console.layout.Conversio
14、nPattern=%c-%m%n 文件大小到達指定尺寸的時候產(chǎn)生一個新的文件 log4j.appender.File=org.apache.log4j.RollingFileAppender 指定輸出目錄 10g4j.appender.File.File=logs/ssm.log 定義文件最大大小 10g4j.appender.File.MaxFileSize=10MB 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級別日志 10g4j.appender.File.Threshold=ALL log4j.appender.File.layout=org.apache.log4j.Pat
15、ternLayout 10g4j.appender.File.layout.ConversionPattern=%p%dyyyy-MM-dd HH:mm:ss%c%m%n 4.2.4、JUnit測試 經(jīng)過以上步驟(到4.2.2,1og4j不配也沒影響),我們已經(jīng)完成了 Spring和mybatis的整合,這樣我們就可以編寫一段測試代碼來試試是否成功了。 ?攙 茲? X P 創(chuàng)建測試用表 既然我們需要測試,那么我們就需要建立在數(shù)據(jù)庫中建立一個測試 表,這個表建的很簡單,SQL語句為: DROPTABLEIFEXISTSuser_t; CREATETABLEuser_t( idint(11)NOT
16、NULLAUTO_INCREMENT, user_namevarchar(40)NOTNULL, passwordvarchar(255)NOTNULL, ageint(4)NOTNULL, PRIMARYKEY(id) )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT CHARSET=utf8; /*Dataforthetableuser_t*/insertintouser_t(id,user_name,password,age)values(1,測試,sfasgfaf,24); 424.2、利用MyBatisGenerator自動創(chuàng)建代碼 參考博文:http:/
17、這個可根據(jù)表自動創(chuàng)建實體類、MyBatis映射文件以及DAO接口,當然,我習慣將生成的接口名改為IUserDao,而不是直接用它生成的UserMapper。如果不想麻煩就可以不改。完成后將文件復制到工程中。如圖: 4.2.43建立Service接口和實現(xiàn)類 目錄結(jié)構(gòu): F面給出具體的內(nèi)容: IUserService.jave .hnust.service; .hnust.pojo.User; publicinterfacelUserService publicUsergetUserById(intuserId); UserServiceImpl.java .hnust.service.impl
18、; importjavax.annotation.Resource; importorg.springframework.stereotype.Service; .hnust.dao.IUserDao; .hnust.pojo.User; .hnust.service.IUserService; Service(userService) publicclassUserServiceImplimplementsIUserService Resource privatelUserDaouserDao; Override publicUsergetUserById(intuserld) /TODOA
19、uto-generatedmethodstub returnthis.userDao.selectByPrimaryKey(userId); 4244建立測試類 測試類在src/test/java中建立,下面測試類中注釋掉的部分是不使 用Spring時,一般情況下的一種測試方法;如果使用了Spring那么就可以使用注解的方式來引入配置文件和類,然后再將service接口 對象注入,就可以進行測試了。 如果測試成功,表示Spring和Mybatis已經(jīng)整合成功了。輸出信息使用的是Log4j打印到控制臺。 packageorg.zsl.testmybatis; importjavax.annota
20、tion.Resource; importorg.apache.log4j.Logger; importorg.junit.Before; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; importorg.springframework.test.context.ContextC
21、onfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; importcom.alibaba.fastjson.JSON; .hnust.pojo.User; .hnust.service.IUserService; RunWith(SpringJUnit4ClassRunner.class)表示繼承了 SpringJUnit4ClassRunner類 ContextConfiguration(locations=classpath:spring-mybatis.xml)publi
22、cclassTestMyBatis privatestaticLoggerlogger=Logger.getLogger(TestMyBatis.class); /privateApplicationContextac=null; Resource privateIUserServiceuserService=null; /Before /publicvoidbefore() /ac=new ClassPathXmlApplicationContext(applicationContext.xml); /userService=(IUserService)ac.getBean(userServ
23、ice); / Test publicvoidtest1() Useruser=userService.getUserById(1); /System.out.println(user.getUserName(); 10(值:+user.getUserName(); (JSON.toJSONString(user); 測試結(jié)果: 至此,完成Spring和mybatis這兩大框架的整合,下面在繼續(xù)進行 SpringMVC的整合 整合SpringMVC 上面已經(jīng)完成了2大框架的整合,SpringMVC的配置文件單獨放,然后在web.xml中配置整合。 酉己置
24、spring-mvc.xml 配置里面的注釋也很詳細, 在此就不說了, 主要是自動掃描控制器, 視圖模式,注解的啟動這三個。 xmlns:xsi=http:/2001/XMLSchema-instance xmlns:p=/schema/p xmlns:context=/schema/context xmlns:mvc=/schema/mvc xsi:schemaLocation=http:/www.s
25、/schema/beanshttp:/www.springframework.Org/schema/beans/spring-beans-3.1.xsd /schema/context /schema/context/spring-context-3.1.xsd /schema/mvc /schema/mvc/spring-mvc
26、-4Oxsd class=org.springframework.http.converter.json.MappingJacksonHttp MessageConverter text/html;charset=UTF-8 class=org.springframework.web.servlet.mvc.annotation.Annotation MethodHandlerAdapterclass=mons.CommonsMultipart Resolver 配置web.xml文件 這里面對spring-mybatis.xml的引入以及配置的spring-mvc的 Servlet就是為了完
27、成SSM整合,之前2框架整合不需要在此處進行任何配置。配置一樣有詳細注釋,不多解釋了。 web.xml xmlns=http:/ xsi:schemaLocation=http:/ http:/ version=3.0 ArchetypeCreatedWebApplication contextConfigLocation classpath:spring-mybatis.xmlencodingFilter org.springframework.web.filter.CharacterEncodingFiltertrue encoding UTF-8 encodingFilter /*org.springframework.web.context.ContextLoaderListenerorg.springframework.web.util.IntrospectorCleanupListener SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 true SpringMVC/index.jsp 測試 至此已經(jīng)完成了S
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)員工培訓與技能提升計劃制度
- 企業(yè)內(nèi)部保密責任追究制度
- 2026福建省面向西南財經(jīng)選調(diào)生選拔工作備考題庫附答案
- 2026紅河州公安局邊境管理支隊公開招聘邊境管控專職輔警(15人)參考題庫附答案
- 2026貴州博通橡塑制品有限公司招聘6人備考題庫附答案
- 2026遼寧鞍山市鐵東區(qū)事業(yè)單位面向應(yīng)屆畢業(yè)生招聘高層次急需緊缺人才16人參考題庫附答案
- 2026重慶飛駛特人力資源管理有限公司外派至招商局檢測車輛技術(shù)研究院有限公司招聘參考題庫附答案
- 2026陜西西安長安大學工程設(shè)計研究院有限公司招聘參考題庫附答案
- 226湖南郴州市宜章縣婦幼保健院招募見習生2人參考題庫附答案
- 四川藏區(qū)高速公路集團有限責任公司2026年校園招聘考試備考題庫附答案
- 房產(chǎn)代持委托協(xié)議書
- GB/T 45451.1-2025包裝塑料桶第1部分:公稱容量為113.6 L至220 L的可拆蓋(開口)桶
- 湖南省長沙市芙蓉區(qū)2024-2025學年六年級上學期語文期末試卷
- 配電箱巡檢表
- GB/T 18238.3-2024網(wǎng)絡(luò)安全技術(shù)雜湊函數(shù)第3部分:專門設(shè)計的雜湊函數(shù)
- 2025屆山西省陽泉市陽泉中學高二生物第一學期期末質(zhì)量檢測試題含解析
- 新人教版一年級數(shù)學下冊全冊教案(表格式)
- 無人機駕駛航空器飛行管理暫行條例(草案)知識考試題庫(85題)
- DB3502-Z 5026-2017代建工作規(guī)程
- 2023年廣東交通職業(yè)技術(shù)學院招聘考試真題
- 廣東省大灣區(qū)2023-2024學年高一上學期期末生物試題【含答案解析】
評論
0/150
提交評論