1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Spring4+SpringMVC+MyBatis整合思路

      本文主要簡單講解框架整合的思路。
      1、Spring框架的搭建

      創(chuàng)新互聯(lián)建站主營原陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā)公司,原陽h5微信平臺小程序開發(fā)搭建,原陽網(wǎng)站營銷推廣歡迎原陽等地區(qū)企業(yè)咨詢

      這個很簡單,只需要web容器中注冊org.springframework.web.context.ContextLoaderListener,并指定spring加載配置文件,那么spring容器搭建完成。(當然org.springframework的核心jar包需要引入)

      當然為了更加易用支持J2EE應(yīng)用,一般我們還會加上如下:

      Spring監(jiān)聽HTTP請求事件:org.springframework.web.context.request.RequestContextListener



      contextConfigLocation
      classpath:webconfig/service-all.xml


      org.springframework.web.context.ContextLoaderListener








      org.springframework.web.context.request.RequestContextListener


      org.springframework.web.util.IntrospectorCleanupListener


      encodingFilter
      org.springframework.web.filter.CharacterEncodingFilter

      encoding
      UTF-8


      forceEncoding
      false



      encodingFilter
      /


      2、Spring MVC的搭建

      首先我們知道Spring MVC的核心是org.springframework.web.servlet.DispatcherServlet,所以web容器中少不了它的注冊。(當然org.springframework的web、mvc包及其依賴jar包需要引入)



      Spring-MVC
      org.springframework.web.servlet.DispatcherServlet

      contextConfigLocation
      classpath:spring/spring-mvc.xml

      1


      Spring-MVC
      .do

      同時為了更好使用MVC,spring-mvc.xml需要配置以下:

      1)(可選)多部分請求解析器(MultipartResolver)配置,與上傳文件有關(guān) 需要類庫commons-io、commons-fileupload





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

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

      4)(必選)處理器映射器(HandlerMapping)配置,可以配置多個,一般采用RequestMappingHandlerMapping或者自定義

      這里我們自定義了一個處理器映射器,繼承重寫RequestMappingHandlerMapping,支持@RequestMapping無需任何path參數(shù)自動裝載類名或方法作為url路徑匹配。

      class="io.flysium.framework.web.servlet.mvc.method.annotation.CustomHandlerMapping">

      CustomHandlerMapping實現(xiàn):@Override
      br/>@Override
      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
      • 默認不需要添加任何參數(shù)(如:/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處理方法參數(shù)。一般擴展RequestMappingHandlerAdapter,或者自定義。如果我們需要json請求的處理,這里必須擴展。同時我們需要注意的是日期格式的轉(zhuǎn)換。

      另外Spring 4.2新特性,加之注解會自動注入@ControllerAdvice,可以定義RequestBodyAdvice、ResponseBodyAdvice,可以更方便地在參數(shù)處理方面著手自定義。

      class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">








      class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">



      class="org.springframework.format.support.FormattingConversionServiceFactoryBean">






      class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">


      static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />




      yyyy-MM-dd HH:mm:ss




      class="io.flysium.framework.http.converter.json.CustomJackson2HttpMessageConverter">



      text/html;charset=UTF-8
      application/json;charset=UTF-8


      6)(可選)處理器異常解析器(HandlerExceptionResolver)配置,可以配置多個,配置Controller異常拋出后,我們是怎么樣處理的,一般需要日志或做反饋的可以自定義。

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

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

      class="org.springframework.web.servlet.view.InternalResourceViewResolver">


      value="org.springframework.web.servlet.view.JstlView" />

      最后給Controller加入組件掃描吧,這樣減少xml配置,直接在Java代碼中加入注解即可。



      expression="org.springframework.stereotype.Controller" />
      expression="org.springframework.web.bind.annotation.RestController" />
      expression="org.springframework.web.bind.annotation.ControllerAdvice" />

      3、Mybatis整合

      整合mybatis到Spring框架,我們需要mybatis的jar包,及mybatis-spring整合jar包。然后在Spring容器中注冊配置org.mybatis.spring.SqlSessionFactoryBean(需要數(shù)據(jù)源,及指定Mybatis配置文件)及org.mybatis.spring.SqlSessionTemplate即可。


      名稱欄目:Spring4+SpringMVC+MyBatis整合思路
      鏈接地址:http://ef60e0e.cn/article/ppijdj.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        兴化市| 望都县| 淮北市| 新密市| 宿松县| 福泉市| 大埔区| 华蓥市| 恭城| 荔波县| 平泉县| 大冶市| 佳木斯市| 改则县| 哈尔滨市| 云南省| 丹寨县| 肇庆市| 正定县| 榆社县| 金阳县| 五大连池市| 波密县| 尼玛县| 嘉兴市| 阿克陶县| 宜黄县| 金溪县| 格尔木市| 南昌县| 宜都市| 全椒县| 阜阳市| 武川县| 湘乡市| 双流县| 阿尔山市| 长岛县| 婺源县| 曲周县| 招远市|