Spring4+SpringMVC+MyBatis整合思路

本文主要简单讲解框架整合的思路。

1、Spring框架的搭建

这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件,那么spring容器搭建完成。(当然org.springframework的核心jar包需要引入)

当然为了更加易用支持J2EE应用,一般我们还会加上如下:

Spring监听HTTP请求事件:org.springframework.web.context.request.RequestContextListener

<!-- 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"/> -->

  <!-- 使用: -->

  <!-- 1、注解获取:@Autowired HttpServletRequest request; -->

  <!-- 2、java代码:HttpServletRequest request = 

  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); -->

  <!-- 3、直接在参数中传递: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配置文件结束 -->

2、Spring MVC的搭建

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

<!-- 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需要配置以下:

1)(可选)多部分请求解析器(MultipartResolver)配置,与上传文件有关 需要类库commons-io、commons-fileupload

<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>

3、Mybatis整合

整合mybatis到Spring框架,我们需要mybatis的jar包,及mybatis-spring整合jar包。然后在Spring容器中注册配置org.mybatis.spring.SqlSessionFactoryBean(需要数据源,及指定Mybatis配置文件)及org.mybatis.spring.SqlSessionTemplate即可。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容