Spring、Spring MVC、MyBatis 整合文件配置詳解

大家好,我是你們的導師,我每天都會在這裏給大家分享一些乾貨內容(當然了,週末也要允許老師休息一下哈)。上次老師跟大家分享了MySQL詳細知識點的知識,今天跟大家分享下SSM框架整合文件配置的知識。

使用SSM框架做了幾個小項目了,感覺還不錯是時候總結一下了。先總結一下SSM整合的文件配置。其實具體的用法最好還是看官方文檔。

Spring:http://spring.io/docs

MyBatis:http://mybatis.github.io/mybatis-3/

基本的組織結構和用法就不說了,前面的博客和官方文檔上都非常的全面。jar包可以使用Maven來組織管理。來看配置文件。

1 web.xml的配置

web.xml應該是整個項目最重要的配置文件了,不過servlet3.0中已經支持註解配置方式了。在servlet3.0以前每個servlet必須要在web.xml中配置servlet及其映射關係。但是在spring框架中就不用了,因爲Spring中是依賴注入(Dependency Injection)的也叫控制反轉(Inversion of Control)。但是也要配置一個重要的servlet,就是前端控制器(DispatcherServlet)。配置方式與普通的servlet基本相似。

配置內容如下:

spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation WEB-INF/classes/spring/springmvc.xml spring / 這裏需要注意,springmvc.xml是spring配置文件,將在後面討論。在中url如果是.action,前端控制器就只會攔截以.action結尾的請求,並不會理會靜態的文件。對靜態頁面的控制就要通過其他的手段。以/作爲url的話就會攔截所有的請求,包括靜態頁面的請求。這樣的話就可以攔截任何想要處理的請求,但是有一個問題。如果攔截了所有的請求,如果不在攔截器中做出相應的處理那麼所有靜態的js、css以及頁面中用到的圖片就會訪問不到造成頁面無法正常顯示。但這可以通過靜態資源的配置來解決這個問題。後面會提到。

配置spring容器:

contextConfigLocation WEB-INF/classes/spring/applicationContext-*.xml 其中applicationContext-*.xml包含3個配置文件,是springIoC容器的具體配置。後面會提到。

配置一個監聽器:

org.springframework.web.context.ContextLoaderListener web.xml的完整配置是這樣的: <?xml version="1.0" encoding="UTF-8"?>

<web-app version=“3.0”
xmlns=“http://java.sun.com/xml/ns/javaee”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

404 /error404.jsp 500 /error500.jsp contextConfigLocation WEB-INF/classes/spring/applicationContext-*.xml org.springframework.web.context.ContextLoaderListener spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation WEB-INF/classes/spring/springmvc.xml spring

看到配置文件中多了兩塊內容,一個是error page是用來友好的處理錯誤的,可以使用錯誤代碼來區別並跳轉到相應的處理頁面。這段配置代碼最好放到最前面,在前端控制器攔截之前處理。

還有一塊內容是一個解決post亂碼問題的過濾器,攔截post請求並編碼爲utf8。

2 springmvc.xml的配置

視圖解析器的配置:

在Controller中設置視圖名的時候會自動加上前綴和後綴。

Controller的配置

自動掃描方式,掃描包下面所有的Controller,可以使用註解來指定訪問路徑。

<context:component-scan base-package=“com.wxisme.ssm.controller”>
也可以使用單個的配置方式,需要指定Controller的全限定名。

配置註解的處理器適配器和處理器映射器: 也可以使用下面的簡化配置:

<mvc:annotation-driven conversion-service=“conversionService”></mvc:annotation-driven>
配置攔截器,可以直接定義攔截所有請求,也可以自定義攔截路徑。

mvc:interceptors

</mvc:interceptors>
配置全局異常處理器

<!-- 定義全局異常處理器 -->
 <!-- 只有一個全局異常處理器起作用 -->
 <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>
配置文件上傳數據解析器,在上傳文件時需要配置。

<!--配置上傳文件數據解析器 -->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize">
 <value>9242880</value>
 </property>
 </bean>

還可以配置一些自定義的參數類型,以日期類型綁定爲例。

<!-- 自定義參數類型綁定 -->
 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
 <property name="converters">
 <list>
 <!-- 日期類型綁定 -->
 <bean class="com.wxisme.ssm.controller.converter.DateConverter"/>
 </list>
 </property>
 </bean>

上面提到過如果在配置前端控制器時攔截了所有的請求,不做特殊處理就會導致部分靜態資源無法使用。如果是這種情況就可以使用下面的配置來訪問靜態資源文件。

<mvc:resources mapping="/images/" location="/images/" />
<mvc:resources mapping="/css/
" location="/css/" />
<mvc:resources mapping="/js/" location="/js/" />
<mvc:resources mapping="/imgdata/
" location="/imgdata/" />
也可以使用默認,但是需要在web.xml中配置。

完全可以不攔截所有路徑,大可避免這個問題的發生。完整的配置大概是這樣的,需要注意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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package=“com.wxisme.ssm.controller”>
</context:component-scan>

<mvc:annotation-driven conversion-service=“conversionService”></mvc:annotation-driven>

 <mvc:resources mapping="/images/**" location="/images/" />
 <mvc:resources mapping="/css/**" location="/css/" /> 
 <mvc:resources mapping="/js/**" location="/js/" />
 <mvc:resources mapping="/imgdata/**" location="/imgdata/" />





    <!-- 定義攔截器 -->
     <mvc:interceptors>
     <!-- 直接定義攔截所有請求 -->

     <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>

     <!-- <mvc:interceptor>

 攔截所有路徑的請求 包括子路徑
 <mvc:mapping path="/**"/>
 <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>
 </mvc:interceptor> -->
 </mvc:interceptors>
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize">
 <value>9242880</value>
 </property>
 </bean>

applicationContext-*.xml的配置

applicationContext-*.xml包括三個配置文件,分別對應數據層控制、業務邏輯service控制和事務的控制。

數據訪問層的控制,applicationContext-dao.xml的配置:

配置加載數據連接資源文件的配置,把數據庫連接數據抽取到一個properties資源文件中方便管理。

配置爲:

<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>
其中jdbc.properties文件的內容如下:配置內容如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=root
jdbc.password=1234

配置數據庫連接池,這裏使用的是dbcp,別忘了添加jar包!

<!-- 配置數據源 dbcp數據庫連接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="${jdbc.driver}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
</bean>

3 Spring和MyBatis整合配置,jar包由MyBatis提供

配置sqlSessionFactory

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <!-- 數據庫連接池 -->
 <property name="dataSource" ref="dataSource"/>
 <!-- 加載Mybatis全局配置文件 -->
 <property name="configLocation" value="/WEB-INF/classes/mybatis/SqlMapConfig.xml"/>
</bean>

SqlMapConfig.xml文件是MyBatis的配置文件,後面會提到。

配置Mapper掃描器,掃描mapper包下的所有mapper文件和類,要求mapper配置文件和類名需要一致。

整個applicationContext-dao.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>

業務邏輯控制,applicationContext-service.xml的配置:

這個文件裏暫時只需要定義service的實現類即可。

事務控制,applicationContext-transaction.xml的配置

配置數據源,使用JDBC控制類。

配置通知,事務控制。
<!-- 通知 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <!-- 傳播行爲 -->
 <tx:method name="save*" propagation="REQUIRED"/>
 <tx:method name="insert*" propagation="REQUIRED"/>
 <tx:method name="update*" propagation="REQUIRED"/>
 <tx:method name="delete*" propagation="REQUIRED"/>
 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
 
 </tx:attributes>
 </tx:advice>

配置AOP切面

<!-- 配置aop -->
 <aop:config>
 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/>
 </aop:config>

整個事務控制的配置是這樣的:

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">
 
<!-- 事務控制 對MyBatis操作數據庫 spring使用JDBC事務控制類 -->
 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <!-- 配置數據源 -->
 <property name="dataSource" ref="dataSource"/>
</bean>
 
 <!-- 通知 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <!-- 傳播行爲 -->
 <tx:method name="save*" propagation="REQUIRED"/>
 <tx:method name="insert*" propagation="REQUIRED"/>
 <tx:method name="update*" propagation="REQUIRED"/>
 <tx:method name="delete*" propagation="REQUIRED"/>
 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
 
 </tx:attributes>
 </tx:advice>
 
 <!-- 配置aop -->
 <aop:config>
 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wxisme.ssm.service.impl.*.*(..))"/>
 </aop:config>
 
</beans>

4 MyBatis的配置

SqlMapConfig.xml的配置 全局setting配置這裏省略,數據庫連接池在spring整合文件中已經配置,具體setting配置參考官方文檔。

別名的定義:

mapper映射文件的配置: 整個文件的配置應該是這樣的: <?xml version="1.0" encoding="UTF-8"?> 具體mapper文件的配置,在使用mapper代理的方法時,命名空間需要是對應的Mapper類。 <?xml version="1.0" encoding="UTF-8" ?> 以上只是對SSM框架簡單使用時的配置文件,如果需要深入使用或者需要理解其內部機理需要參考官方文檔和其源代碼。

原文:https://mp.weixin.qq.com/s/A3rWXwp4ZTM1UD00oJIx3Q作者:java研發軍團來源:微信公衆號
免費分享java技術資料,需要的朋友可以關注後私信免費獲取

Spring、Spring MVC、MyBatis 整合文件配置詳解
Spring、Spring MVC、MyBatis 整合文件配置詳解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章