主角即Spring、SpringMVC、MyBatis,即所謂的SSM框架,你們應該也都有所瞭解,概念性的東西就不寫了,有萬能的百度。以前沒有記錄SSM整合的過程,此次剛恰好基於本身的一個小項目從新搭建了一次,並且比項目搭建的要更好一些。之前解決問題的過程和方法並無及時記錄,之後在本身的小項目中遇到我再整理分享一下。我的認爲使用框架並非很難,關鍵要理解其思想,這對於咱們提升編程水平頗有幫助。 css
工做環境:JDK 1.七、Mysql 5.六、Myeclipse 十、Tomcat 七、Mavenhtml
框架版本:Spring 4.2.6.RELEASE、SpringMVC 4.2.6.RELEASE、MyBatis 3.2.8前端
項目目錄結構:java
一、類文件和資源文件分開存放,類文件分爲六層,其中common存放公共和框架部分,controller存放項目控制層,service存放項目業務邏輯層,model存放項目實體類,mapper存放數據層接口,test存放測試相關類。webpack
注:若涉及到多業務模塊的狀況,分層能夠在各層內部進行劃分,固然對於大模塊建議採用Maven多模塊項目方式搭建。程序員
二、前端在webapp下分出了common和project,common存放公共部分,project內部用於存放多個子模塊,common以及各子模塊內部分別新建images,js,css和view四個文件夾,用於歸類不一樣資源。web
構建好總體結構後,接下來應該把目光看下web.xml文件,不管是何種JavaEE框架,入口老是在web.xml,認準這邊就沒錯了。spring
web.xml 內容不少,SSM整合相關的關鍵部分就是加載Spring配置文件spring-config.xml,以及將SpringMVC核心調度器DispatcherServlet註冊爲servlet(配置文件爲mvc-config.xml),其餘部分有註釋,選擇性觀看便可。sql
web.xml數據庫
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- ================站臺名稱,站臺描述,大小圖標================ --> <display-name>SpringMVC</display-name> <description>柴可夫斯基模板</description> <icon> <small-icon>/common/images/favicon.ico</small-icon> <large-icon>/common/images/favicon.ico</large-icon> </icon> <!-- 支持分佈式 --> <!-- <distributable/> --> <!-- 應用路徑,若是不設置,缺省爲"webapp.root",當tomcat加載多個項目時,會發生名稱衝突 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>spring4.root</param-value> </context-param> <!-- ================log4j配置開始================ --> <!-- log4j2不須要在這邊作額外的配置 --> <!-- ================log4j配置結束================ --> <!-- ================Spring配置開始================ --> <!-- 設置Spring容器加載全部的配置文件的路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-config.xml</param-value> <!-- <param-value>classpath:/spring-*.xml</param-value> --> </context-param> <!-- 配置Spring監聽器,能夠在容器啓動時,加載contextConfigLocation的context-param節點的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ================Spring配置結束================ --> <!-- 配置監聽器,定義在服務器啓動和關閉時,須要執行的方法 --> <listener> <listener-class>com.demo.common.startup.InitListener</listener-class> </listener> <!-- 防止Struts和Quartz等內存溢出監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 監聽HTTP請求事件,爲Bean的request,session,globalsession等做用域提供支持 --> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- Spring 字符編碼配置 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 自定義登陸過濾器,攔截JSP頁面,不容許直接訪問 --> <filter> <filter-name>loginFilter</filter-name> <filter-class>com.demo.common.filter.LoginFilterSpring</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>contentType</param-name> <param-value>text/html;charset=UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!--包裝request過濾器--> <filter> <filter-name>wrapRequestFilter</filter-name> <filter-class>com.demo.common.filter.WrapParameterFilter</filter-class> </filter> <filter-mapping> <filter-name>wrapRequestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 激活Tomcat的defaultServlet來處理靜態文件(效率較高) --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> <url-pattern>*.gif</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.css</url-pattern> <url-pattern>*.ico</url-pattern> <url-pattern>*.eot</url-pattern> <url-pattern>*.svg</url-pattern> <url-pattern>*.ttf</url-pattern> <url-pattern>*.woff</url-pattern> <url-pattern>*.mp3</url-pattern> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- ================配置SpringMVC核心調度器================ --> <!-- 不指定具體文件的話,默認爲"/WEB-INF/<servlet name>-servlet.xml" --> <!-- load-on-startup表明啓動順序,設置爲大於等於0表示容器在應用啓動時就加載並初始化這個servlet --> <!-- 推薦攔截/,風格優雅 --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 隨服務器啓動的servlet --> <servlet> <servlet-name>initServlet</servlet-name> <servlet-class>com.demo.common.startup.InitServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <!-- 阿里巴巴數據源配置啓用Web監控統計功能 --> <!-- 經過 http://ip:port/druid/ 地址訪問便可 --> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <param-name>allow</param-name> <param-value>127.0.0.1</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!-- CXF服務發佈配置 --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> <!-- 設置session過時時間爲60分鐘 --> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- 指定錯誤404和500的處理頁面 --> <error-page> <error-code>404</error-code> <location>/common/view/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/common/view/500.jsp</location> </error-page> <!-- 歡迎頁面 --> <welcome-file-list> <welcome-file>common/view/index.jsp</welcome-file> </welcome-file-list> </web-app>
這塊涉及內容也不少,從何提及呢,先上一下文件內容好了:
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 加載配置屬性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:constant.properties"/> <!-- 開啓異步任務(同時開啓定時器註解掃描) --> <task:annotation-driven /> <!-- 使用@AspectJ風格的切面聲明 --> <!-- <aop:aspectj-autoproxy/> --> <!-- 使用Annotation自動註冊Bean --> <!-- 在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解 --> <context:component-scan base-package="com.demo"><!-- base-package 若是多個,用「,」分隔 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 引入Mybatis配置 --> <import resource="spring-mybatis.xml"/> <!-- 引入Socket配置 --> <import resource="spring-socket.xml"/> <!-- 引入MongoDB配置 --> <import resource="test/mongo-config.xml"/> <!-- 引入定時器任務配置 --> <!-- <import resource="classpath*:com/demo/config/spring-job.xml"/> --> <!-- 引入hibernate4配置 --> <!-- <import resource="spring-hibernate.xml"/> --> <!-- 引入緩存配置 --> <!-- <import resource="spring-cache.xml"/> --> <!-- 引入CXF配置 --> <import resource="test/spring-cxf.xml"/> <!-- 引入Redis配置(無需如此配置,直接使用RedisUtil便可) --> <!-- <import resource="test/spring-jedis.xml"/> --> </beans>
上面內容不少,可是並不全是SSM框架的,須要關注的點只有下面幾個:
一、context:component-scan 包掃描
這個註解不用多說了,要注意的就是主容器中不掃描@Controller註解,由於@Controller將會在SpringMVC掃描。
二、import 標籤和多文件配置
在團隊開發時候,每一個人都常去改動Spring配置文件,不科學,使用這個技巧方便,每一個都有各自的配置文件了。項目較大,有較多的bean時,能夠將其分散到子文件中。雖然Spring還有自動掃描的功能,但我感受也不怎麼好,須要去掃描,影響性能;並且各個Bean分散在不一樣包中,很差配置。
多文件配置一般有兩種作法:
2.1 在 web.xml配置中的contextConfigLocation節點配置多個值。
具體代碼以下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/context1.xml, /WEB-INF/classes/context2.xml, /WEB-INF/classes/context3.xml </param-value> </context-param>
其中分隔符能夠是","也能夠是" "等,也能夠用通配符application-*,這樣配置的要求是,你的Spring配置文件必須是applicationContext-*****.xml這樣的形式存在,*號表明通配符,具體就不說了。
2.2 在一個application.xml中配置多個import標籤引入其餘文件。
我的喜歡這種方式,清晰明瞭,總得有一個主入口吧(估計受了webpack和SeaJS的影響)。
從這邊也不難看到Spring框架在其中扮演的角色:管理容器,有效地組織你的中間層對象(無節操得集成其餘框架)。
三、引入mybatis配置:spring-mybatis.xml
這個文件就是用來完成spring和mybatis的整合的。這裏面也沒多少行配置,主要的就是自動掃描,自動注入,配置數據庫。註釋也很詳細,你們看看就明白了。
spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 1. 數據源配置 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties" /> <!-- Druid方式配置數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="10" /> <property name="minIdle" value="20" /> <property name="maxActive" value="100" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="6000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 驗證是否回收 --> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 開啓Druid的監控統計功能 --> <property name="filters" value="stat" /> </bean> <!-- 2. 建立SqlSession的工廠 --> <!-- dataSource:引用數據源,統一加載配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" ></property> <!-- 自動配置別名-做用相似mybatis-config.xml的別名 --> <property name="typeAliasesPackage" value="com.demo.model" /> <!-- 設置別名的類加上父類限定 --> <property name="typeAliasesSuperType" value="com.demo.common.base.BaseEntity"/> <!-- 當mybatis的xml文件和mapper接口不在相同包下時,須要用mapperLocations屬性指定xml文件的路徑 --> <!-- *是個通配符,表明全部的文件,**表明全部目錄下 --> <property name="mapperLocations" value="classpath*:mappings/**/*.xml"/> <!-- 指定mybatis核心配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 3. 自動掃描加載Sql映射文件/接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- sqlSessionFactoryBeanName:表明延遲加載--> <!-- 這個配置的前提條件是:映射接口類文件(.java)和映射XML文件(.xml)須要放在相同的包下(com.demo.mapper)--> <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- basePackage:指定sql映射文件/接口所在的包(自動掃描)--> <property name="basePackage" value="com.demo.mapper"></property> <!-- 掃描basePackage下全部以@MyBatisDao註解的接口 --> <property name="annotationClass" value="com.demo.common.persistence.annotation.MyBatisDao"/> </bean> <!-- 4. 事務管理 --> <!-- dataSource:引用上面定義的數據源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5. 使用聲明式事務 --> <!-- transaction-manager:引用上面定義的事務管理器 --> <!-- 配置 Annotation 驅動,掃描@Transactional註解的類定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 定義JdbcTemplate的Bean --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"></bean> </beans>
這塊配置裏面的註釋也很詳細,在此就不說了,主要是自動掃描控制器,視圖模式,註解的啓動這三個。
mvc-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 打開使用註解自動檢測功能自動註冊Bean,只掃描@Controller --> <context:component-scan base-package="com.demo" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 文件上傳表單的視圖解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="2097152"></property><!--限制文件上傳2M內 --> <property name="maxInMemorySize" value="40960"></property> </bean> <!-- 默認的註解映射的支持 - 增長String類型中文解析 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 使用自定義Spring攔截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.demo.common.core.SpringMVCInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 視圖配置 --> <!-- 對轉向頁面的路徑解析,指定輸出視圖的先後綴,controller返回的視圖直接加上此先後綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/project/" p:suffix=".jsp" /> </beans>
這部份內容也能夠參考上一篇博文:《經久不衰的Spring框架:SpringMVC 統括》
到此,已經完成了SSM三大框架的整合了,接下來測試一下,若是成功了,那麼恭喜你,若是失敗了,繼續調試吧,做爲程序員就是不停的與BUG作鬥爭!
測試的話,無非就是新建對應的view、controller、service、mapper ,測試一下SpringMVC和Mybatis的功能,很簡單,就不羅嗦了。
部署的話,就幹到tomcat裏面去,啓動,訪問localhost便可,也不贅述了。
至此,SSM三大框架的整合就完成了,在此基礎上可再添加其餘功能。
因爲這只是SSM整合的文章,因此上面不少文件內容沒有面面俱到,好比日誌處理、socket、ws、bean定製等等,後續以爲有用得會繼續放上來,請繼續關注山人行博客。