Spring+Springmvc+MyBatis整合

來源:互聯網
上載者:User

Spring+Springmvc+MyBatis整合

寫在整合之前
現在剛進入公司,公司採用的是SSM架構,使用Maven搭建工程,之前自己學的是SSH,所以入門相對還可以,覺得不是太難,在進入公司這一周多的時間,自己覺得有以下幾點很重要:

在公司裡,架構他只是要求你瞭解,熟悉公司的架構,真正要用到的還是java基礎的公司,因為最重要的是業務。 架構入門也不是太難,只是想深入瞭解就有點麻煩,很多原理和理論性的問題很難理解,得有一個宏觀的概念。 在以後的時間內一定要深入瞭解主流的架構一、基本概念

spirng
springmvc
myBatis
三大架構的基本概念就不解釋了,因為剛開始都肯定瞭解,寫了也沒有太大的意義存在,但是得知道他們各自負責什麼功能。

二、SSM整合

整合的思路就從項目的整體結構、設定檔和實作類別入手,因為只是搭建一個SSM環境,業務功能唯寫了一個,還請見諒。

項目的整體結構


Maven項目採用了約定的形式:

src/main/java 存放java檔案 src/main/resources 存放設定檔 src/test/java 存放測試類別 src/test/resources 存放測試類別的資源檔 pom.xml Maven的設定檔

這個小項目採用的是spring4.2.3+MyBatis3.2.6版本,應該算是比較新的,Maven項目剛開始做的話我覺得pom.xml檔案配置比較麻煩,不知道具體使用哪個依賴包,導致Maven工程老是不成功,這也是初學者的一個麻煩點,雖然不是痛點,咋這裡,我給出pom.xml的所有配置。

    4.0.0    ssm4    ssm4    0.0.1-SNAPSHOT    war    ssm4            UTF-8        4.2.3.RELEASE        4.11        3.2.6        5.1.37        3.0-alpha-1        2.2        1.2        10.2.0.4.0        1.1.39        2.0        2.2        1.1.3        1.3.2        3.3.1        1.2.1        1.2.17        1.7.6        1.6.1        1.0.17        1.2.3        1.7.4        0.8.0.RELEASE        1.0.4        0.9.5.1        6.8.8        2.3.20        1.7        5.3.7        4.2        3.1                      maven-compiler-plugin        2.3.2                  1.7          1.7                            maven-war-plugin        2.2                  3.1          false                                                    junit            junit            ${junit.version}            test                                    org.springframework            spring-core            ${spring.version}                            org.springframework            spring-beans            ${spring.version}                            org.springframework            spring-context            ${spring.version}                            org.springframework            spring-jdbc            ${spring.version}                            org.springframework            spring-web            ${spring.version}                            org.springframework            spring-webmvc            ${spring.version}                            org.springframework            spring-aop            ${spring.version}                            org.springframework            spring-tx            ${spring.version}                            org.springframework            spring-orm            ${spring.version}                            org.springframework            spring-context-support            ${spring.version}                            org.springframework            spring-test            ${spring.version}                            org.springframework            spring-jms            ${spring.version}                                    org.mybatis            mybatis            ${mybatis.version}                            org.mybatis            mybatis-spring            1.2.2                            org.mybatis.caches            mybatis-ehcache            1.0.2                                    mysql            mysql-connector-java            ${mysql.version}                                            commons-logging            commons-logging            ${commons-logging.version}                            log4j            log4j            ${log4j.version}                            org.slf4j            slf4j-log4j12            ${slf4j.version}                            org.slf4j            slf4j-api            ${slf4j.version}                                    javax.servlet.jsp            jsp-api            ${jsp-api.version}                            javax.servlet            servlet-api            ${servlet-api.version}                            javax.servlet            jstl            ${jstl.version}                                    org.springframework            spring-test            ${spring.version}                            junit            junit            ${junit.version}                            org.testng            testng            ${testng.version}                                    org.apache.commons            commons-io            ${commons-io.version}                            org.apache.commons            commons-lang3            ${commons-lang3.version}                            org.apache.commons            commons-collections4            4.0                            org.kuali.commons            commons-beanutils            1.8.3-kuali-4                                    org.aspectj            aspectjweaver            ${aspectjweaver.version}                            dom4j            dom4j            ${dom4j.version}                            com.mchange            c3p0            ${c3p0.version}            
Spring和MyBatis進行整合

spring和MyBatis整合的設定檔

                                                                                                                                                                

MyBatis的對應檔

            select * from person    

MyBatis的設定檔
因為和spring進行了整合,MyBatis的設定檔基本上不用寫其他資訊。

到這裡,spring和MyBatis就算是整合完成。

Spring和springmvc整合

springmvc是包含在spring中的,基本上是不需要單獨在配置什麼。

                                

Controller層:

@Controllerpublic class PersonController {    @Resource    PersonService personService;    @RequestMapping("person/listAll.action")    public String listAll(Model model){        List personList = personService.listAll();        model.addAttribute("personList", personList);        return "/person/jPersonList";    }}
web.xml檔案的配置:
        contextConfigLocation        classpath*:spring/spring-mybatis.xml                org.springframework.web.context.ContextLoaderListener                springmvc        org.springframework.web.servlet.DispatcherServlet                    contextConfigLocation            classpath*:springmvc/springmvc-servlet.xml                        springmvc        *.action                index.jsp    
運行效果:

總結

這是一個很小很小的項目,但是包含了SSM需要的所有配置,不管是大項目還是小項目,總的來說基本用的都是這些設定檔,在可以搭建起來SSM環境之後才可以進一步深入瞭解其原理,不然只談理論,沒有技術支撐,也沒有什麼卵用。
設定檔包括幾個:

web.xml spring-mybatis.xml springmvc.xml mybatis.xml

在spring3之後註解慢慢開始代替設定檔的一部分功能,也簡化了設定檔的配置難度,學的時間不長,這個教程只適合初學者學習,如有紕漏,請指導。

項目源碼:連結:http://pan.baidu.com/s/1bnoMA4Z 密碼:43ob

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.