Spring4+SpringMVC+MyBatis整合思路

本文主要簡單講解框架整合的思路。html

一、Spring框架的搭建java

這個很簡單,只須要web容器中註冊org.springframework.web.context.ContextLoaderListener,並指定spring加載配置文件,那麼spring容器搭建完成。(固然org.springframework的核心jar包須要引入)git

固然爲了更加易用支持J2EE應用,通常咱們還會加上以下:web

Spring監聽HTTP請求事件:org.springframework.web.context.request.RequestContextListenerredis

<!-- spring配置文件開始 -->
 <context-param>
   <param-name>contextConfigLocation</param-name><!-- spring配置文件,請根據須要選取 -->
   <param-value>classpath*:webconfig/service-all.xml</param-value>
 </context-param>
 <listener><!-- Spring負責監聽web容器啓動和關閉的事件 --><!-- Spring ApplicationContext載入 -->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener><!-- Spring監聽HTTP請求事件 -->
   <!-- 使spring支持request與session的scope,如: -->
   <!-- <bean id="loginAction" class="com.foo.LoginAction" scope="request"/> -->
   <!-- 使用: -->
   <!-- 一、註解獲取:@Autowired HttpServletRequest request; -->
   <!-- 二、java代碼:HttpServletRequest request = 
   ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); -->

   <!-- 三、直接在參數中傳遞:public String sayHi(HttpServletRequest request) -->
   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
 <listener><!-- Spring 刷新Introspector防止內存泄露 -->
   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 </listener>
 <filter>
   <filter-name>encodingFilter</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>false</param-value>
   </init-param>
 </filter>
 <filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- spring配置文件結束 -->


二、Spring MVC的搭建spring

首先咱們知道Spring MVC的核心是org.springframework.web.servlet.DispatcherServlet,因此web容器中少不了它的註冊。(固然org.springframework的web、mvc包及其依賴jar包須要引入)express

<!-- spring mvc配置開始 -->
 <servlet>
   <servlet-name>Spring-MVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:spring/spring-mvc.xml</param-value><!-- spring mvc配置文件 -->
   </init-param>
   <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>Spring-MVC</servlet-name>
   <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <!-- spring mvc配置結束 -->


同時爲了更好使用MVC,spring-mvc.xml須要配置如下:json

1)(可選)多部分請求解析器(MultipartResolver)配置,與上傳文件有關 須要類庫commons-io、commons-fileuploadspring-mvc

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"></property><!-- 默認編碼-->
   <property name="maxUploadSize" value="104857600"></property><!-- 文件大小最大值-->
   <property name="maxInMemorySize" value="40960"></property><!-- 內存中的最大值-->
 </bean>


2)(可選)本地化(LocaleResolver)配置
微信

3)(可選)主題解析器(ThemeResolver)配置 

4)(必選)處理器映射器(HandlerMapping)配置,能夠配置多個,通常採用RequestMappingHandlerMapping或者自定義

這裏咱們自定義了一個處理器映射器,繼承重寫RequestMappingHandlerMapping,支持@RequestMapping無需任何path參數自動裝載類名或方法做爲url路徑匹配。

<bean id="handlerMapping" 
   class="io.flysium.framework.web.servlet.mvc.method.annotation.CustomHandlerMapping">
   <property name="order" value="-1" />
 </bean>


CustomHandlerMapping實現:

@Override
 protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) {
   RequestMappingInfo info = createRequestMappingInfoDefault(method);
   if (info != null) {
     RequestMappingInfo typeInfo = createRequestMappingInfoDefault(handlerType);
     if (typeInfo != null)
       info = typeInfo.combine(info);
   }
   return info;
 }

 private RequestMappingInfo createRequestMappingInfoDefault(AnnotatedElement element) {
   RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element,
       RequestMapping.class);
   RequestCondition condition = (element instanceof Class)
       ? getCustomTypeCondition((Class) element)
       : getCustomMethodCondition((Method) element);
   /**
   * 以類名和方法名映射請求,參照@RequestMapping
   * 默認不須要添加任何參數(如:/className/methodName.do)
   */

   String defaultName = (element instanceof Class)
       ? ((Class) element).getSimpleName()
       : ((Method) element).getName();
   return requestMapping == null
       ? null
       : createRequestMappingInfo(requestMapping, condition, defaultName);
 }

 protected RequestMappingInfo createRequestMappingInfo(RequestMapping annotation,
     RequestCondition<?> customCondition, String defaultName) {
   String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
   if (patterns != null && (patterns.length == 0)) {
     patterns = new String[]{defaultName};
   }
   return new RequestMappingInfo(
       new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(),
           this.useSuffixPatternMatch, this.useTrailingSlashMatch,
           this.fileExtensions),
       new RequestMethodsRequestCondition(annotation.method()),
       new ParamsRequestCondition(annotation.params()),
       new HeadersRequestCondition(annotation.headers()),
       new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
       new ProducesRequestCondition(annotation.produces(), annotation.headers(),
           this.contentNegotiationManager),
       customCondition);
 }


5)(必選)處理器適配器(HandlerAdapter)配置,能夠配置多個,主要是配置messageConverters,其主要做用是映射前臺傳參與handler處理方法參數。通常擴展RequestMappingHandlerAdapter,或者自定義。若是咱們須要json請求的處理,這裏必須擴展。同時咱們須要注意的是日期格式的轉換。

另外Spring 4.2新特性,加之註解會自動注入@ControllerAdvice,能夠定義RequestBodyAdvice、ResponseBodyAdvice,能夠更方便地在參數處理方面着手自定義。

<bean id="handlerAdapter"
 class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

 <property name="order" value="-1" />
 <property name="messageConverters">
   <list>
   <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" 
      /> -->

     <ref bean="mappingJacksonHttpMessageConverter" />
   </list>
 </property>
 <property name="webBindingInitializer">
   <bean
     class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

     <property name="conversionService">
       <!-- 針對普通請求(非application/json) 前臺的日期字符串與後臺的Java Date對象轉化,
       此狀況,應使用spring 
         mvc自己的內置日期處理 -->

       <!-- 能夠在VO屬性上加註解:@DateTimeFormat 須要類庫joda-time -->
   <bean
   class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

   </bean>
     </property>
   </bean>
 </property>
</bean>
<!-- json請求(application/json)返回值Date轉String,全局配置 -->
<bean name="jacksonObjectMapper"
 class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">

 <property name="featuresToDisable">
   <array>
   <util:constant
static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />

    </array>
 </property>
 <!-- 若是想自定義,能夠在VO屬性上加註解:@JsonFormat(shape = JsonFormat.Shape.STRING, pattern 
    = Consts.DATE_PATTERN.DATE_PATTERN_OBLIQUE,timezone = "GMT+8") -->

 <property name="simpleDateFormat">
   <value>yyyy-MM-dd HH:mm:ss</value>
 </property>
</bean>
<!--避免IE執行Ajax時,返回JSON出現下載文件 -->
<!-- 自定義 -->
<bean id="mappingJacksonHttpMessageConverter"
 class="io.flysium.framework.http.converter.json.CustomJackson2HttpMessageConverter">

 <property name="objectMapper" ref="jacksonObjectMapper" />
 <property name="supportedMediaTypes">
   <list>
     <value>text/html;charset=UTF-8</value>
     <value>application/json;charset=UTF-8</value>
   </list>
 </property>
</bean>


6)(可選)處理器異常解析器(HandlerExceptionResolver)配置,能夠配置多個,配置Controller異常拋出後,咱們是怎麼樣處理的,通常須要日誌或作反饋的能夠自定義。

7)(可選)請求到視圖名翻譯器(RequestToViewNameTranslator)配置,RequestToViewNameTranslator能夠在處理器返回的View爲空時使用它根據Request得到viewName。

8)(可選)視圖解析器(ViewResolver)配置,能夠配置多個,定義跳轉的文件的先後綴 ,視圖模式配置,主要針對@Controller返回ModelAndView的視圖路徑解析,動給後面控制器的方法return的字符串 加上前綴和後綴,變成一個 可用的url地址 。

<bean id="viewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/" />
   <property name="suffix" value=".jsp" />
   <property name="viewClass"
     value="org.springframework.web.servlet.view.JstlView" />
 </bean>


最後給Controller加入組件掃描吧,這樣減小xml配置,直接在Java代碼中加入註解便可。

<!-- 自動掃描類包,將標誌Spring註解的類自動轉化爲Bean,同時完成Bean的注入 -->
 <!-- 掃描控制器 -->
 <context:component-scan base-package="io.flysium" use-default-filters="false">
   <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Controller" />

   <context:include-filter type="annotation" 
     expression="org.springframework.web.bind.annotation.RestController" />

   <context:include-filter type="annotation" 
     expression="org.springframework.web.bind.annotation.ControllerAdvice" />

 </context:component-scan>


三、Mybatis整合

整合mybatis到Spring框架,咱們須要mybatis的jar包,及mybatis-spring整合jar包。而後在Spring容器中註冊配置org.mybatis.spring.SqlSessionFactoryBean(須要數據源,及指定Mybatis配置文件)org.mybatis.spring.SqlSessionTemplate便可。


更多整合請參照Git項目:https://git.oschina.net/svenaugustus/app-ss4m-less

目前除了ssm,另外整合redis(支持切換單節點配置、主從哨兵配置,集羣配置)、spring session方案。

其中包括spring MVC的簡單demo,用於學習交流。

我有一個微信公衆號,常常會分享一些Java技術相關的乾貨;若是你喜歡個人分享,能夠用微信搜索「Java團長」或者「javatuanzhang」關注。

相關文章
相關標籤/搜索