Spring、Spring MVC、MyBatis 整合文件配置詳解
- 2019 年 10 月 4 日
- 筆記
大家好,我是你們的導師,我每天都會在這裡給大家分享一些乾貨內容(當然了,周末也要允許老師休息一下哈)。上次老師跟大家分享了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基本相似。
配置內容如下:
<!-- 配置前端控制器 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- ContextconfigLocation配置springmvc載入的配置文件 適配器、處理映射器等 --> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <!-- 1、.action訪問以.action結尾的 由DispatcherServlet進行解析 2、/,所有訪問都由DispatcherServlet進行解析 --> <url-pattern>/</url-pattern> </servlet-mapping>
這裡需要注意,springmvc.xml是spring配置文件,將在後面討論。在<servlet-mapping>中url如果是.action,前端控制器就只會攔截以.action結尾的請求,並不會理會靜態的文件。對靜態頁面的控制就要通過其他的手段。以/作為url的話就會攔截所有的請求,包括靜態頁面的請求。這樣的話就可以攔截任何想要處理的請求,但是有一個問題。如果攔截了所有的請求,如果不在攔截器中做出相應的處理那麼所有靜態的js、css以及頁面中用到的圖片就會訪問不到造成頁面無法正常顯示。但這可以通過靜態資源的配置來解決這個問題。後面會提到。
配置spring容器:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param>
其中applicationContext-*.xml包含3個配置文件,是springIoC容器的具體配置。後面會提到。
配置一個監聽器:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
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"> <display-name></display-name> <!-- 404錯誤攔截 --> <error-page> <error-code>404</error-code> <location>/error404.jsp</location> </error-page> <!-- 500錯誤攔截 --> <error-page> <error-code>500</error-code> <location>/error500.jsp</location> </error-page> <!-- 載入spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置前端控制器 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- ContextconfigLocation配置springmvc載入的配置文件 適配器、處理映射器等 --> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <!-- 1、.action訪問以.action結尾的 由DispatcherServlet進行解析 2、/,所有訪問都由DispatcherServlet進行解析 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 解決post亂碼問題的過濾器 --> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list> </web-app>
看到配置文件中多了兩塊內容,一個是error page是用來友好的處理錯誤的,可以使用錯誤程式碼來區別並跳轉到相應的處理頁面。這段配置程式碼最好放到最前面,在前端控制器攔截之前處理。
還有一塊內容是一個解決post亂碼問題的過濾器,攔截post請求並編碼為utf8。
2 springmvc.xml的配置
視圖解析器的配置:
<!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用前綴和後綴 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
在Controller中設置視圖名的時候會自動加上前綴和後綴。
Controller的配置
自動掃描方式,掃描包下面所有的Controller,可以使用註解來指定訪問路徑。
<!-- 使用組件掃描的方式可以一次掃描多個Controller --> <context:component-scan base-package="com.wxisme.ssm.controller">
也可以使用單個的配置方式,需要指定Controller的全限定名。
<bean name="/queryUser.action" class="com.wxisme.ssm.controller.Controller1"/>
配置註解的處理器適配器和處理器映射器:
<!-- 註解的處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 註解的處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
也可以使用下面的簡化配置:
<!-- 配置註解的處理器映射器和處理器適配器 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
配置攔截器,可以直接定義攔截所有請求,也可以自定義攔截路徑。
<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="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中配置。
<!-- 訪問靜態資源文件 --> <!-- <mvc:default-servlet-handler/> 需要在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"> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用前綴和後綴 --> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 使用組件掃描的方式可以一次掃描多個Controller --> <context:component-scan base-package="com.wxisme.ssm.controller"> </context:component-scan> <!-- 配置註解的處理器映射器和處理器適配器 --> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 自定義參數類型綁定 --> <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:default-servlet-handler/> 需要在web.xml中配置--> <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> <!-- 定義全局異常處理器 --> <!-- 只有一個全局異常處理器起作用 --> <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean> </beans>
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配置文件和類名需要一致。
<!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑,如果需要掃描多個包中間用半形逗號隔開 --> <property name="basePackage" value="com.wxisme.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
整個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"/> <!-- 配置數據源 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> <!-- 配置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> <!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑,如果需要掃描多個包中間用半形逗號隔開 --> <property name="basePackage" value="com.wxisme.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
業務邏輯控制,applicationContext-service.xml的配置:
這個文件里暫時只需要定義service的實現類即可。
<!-- 定義service --> <bean id="userService" class="com.wxisme.ssm.service.impl.UserServiceImpl"/>
事務控制,applicationContext-transaction.xml的配置
配置數據源,使用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 --> <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配置參考官方文檔。
別名的定義:
<typeAliases> <!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--> <package name="com.wxisme.ssm.po"/> </typeAliases>
mapper映射文件的配置:
<mappers> <!-- 載入映射文件 --> <!-- 這裡也可以使用class來載入映射文件,前提是:使用mapper代理的方法,遵循規範, 並且兩個文件必須同名且在同一目錄 <mapper class="com.wxisme.mybatis0100.mapper.UserMapper"/> 基於class載入,可以進行批量載入 --> <!-- 通過掃描包的方式來進行批量載入映射文件 --> <package name="com.wxisme.ssm.mapper"/> </mappers>
整個文件的配置應該是這樣的:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 將資料庫連接數據抽取到屬性文件中方便測試 --> <!-- <properties resource="/WEB-INF/classes/jdbc.properties"></properties> --> <!-- 別名的定義 --> <typeAliases> <!-- 批量定義別名 ,指定包名,自動掃描包中的類,別名即為類名,首字母大小寫無所謂--> <package name="com.wxisme.ssm.po"/> </typeAliases> <!-- 資料庫連接用資料庫連接池 --> <mappers> <!-- 通過掃描包的方式來進行批量載入映射文件 --> <package name="com.wxisme.ssm.mapper"/> </mappers> </configuration>
具體mapper文件的配置,在使用mapper代理的方法時,命名空間需要是對應的Mapper類。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.wxisme.ssm.mapper.AlbumMapper" > </mapper>
以上只是對SSM框架簡單使用時的配置文件,如果需要深入使用或者需要理解其內部機理需要參考官方文檔和其源程式碼。
END