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即可。

更多整合请参照Git项目:https://git.oschina.net/svenaugustus/app-ss4m-less

Spring4+Spring MVC+MyBatis整合思路的更多相关文章

  1. ssm整合说明与模板-Spring Spring MVC Mybatis整合开发

    ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...

  2. spring, spring mvc, mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  3. Spring + Spring MVC + MyBatis 整合

    1.所需要Jar包 ? <!-- Spring3.0.1包 -->   org.springframework.web-3.0.1 系列   <!-- 公共包 -->   sl ...

  4. Spring+MVC+Mybatis整合

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 什么是秒杀业务 网站售卖某产品时,规定在某个日期开始售卖限量的产品, ...

  5. Spring+Spring MVC+MyBatis整合

    一.准备工作    1.1导入所需jar包 1.2数据库 CREATE TABLE `t_customer` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ...

  6. Spring MVC + MyBatis整合(IntelliJ IDEA环境下)

    一些重要的知识: mybais-spring.jar及其提供的API: SqlSessionFactoryBean: SqlSessionFactory是由SqlSessionFactoryBuild ...

  7. eclipse搭建maven project的spring4 spring mvc mybatis

    一,先确定已经安装好了Eclipse Java EE IDE for Web Developers我用的是如下版本 Version: Neon.3 Release (4.6.3)Build id: 2 ...

  8. Spring + Spring MVC + MyBatis框架整合

    ---恢复内容开始--- 一.Maven Web项目创建 如有需要,请参考:使用maven创建web项目 二.Spring + Spring MVC + MyBatis整合 1.Maven引入需要的J ...

  9. 【Spring 持久层】Spring 与 Mybatis 整合

    持久层整合总述 1.Spring 框架为什么要与持久层技术进行整合? JavaEE开发需要持久层进行数据库的访问操作 JDBC.Hibernate.MyBatis 进行持久开发过程存在大量的代码冗余 ...

随机推荐

  1. 【windows server 2008R2】windows server 2008R2自动重启

    客户反映2018.3.20早上8点多数据库重启. 我找了半天原因,看了一下告警日志没发现什么问题.后来我再跟他确认,他说他练上去的时候正在准备桌面.这感觉像是服务器重启导致数据库重启. 于是我远程上去 ...

  2. Linu之linux系统基础优化和基本命令

    Linux系统基础优化和基本命令 网络参数设定命令 ifconfig: 查询,设置网卡和ip等参数 ifup,ifdown: 脚本命令,更简单的方式 ip: 符合指令,直接修改上述功能 编辑网卡配置文 ...

  3. TPO-19 C1 Discussing A Point Raised In A Lecture

    TPO-19 C1 Discussing A Point Raised In A Lecture 第 1 段 1.Listen to a conversation between a student ...

  4. Python 夺大满贯!三大编程语言榜即将全部“失守”!

    有互联网创业者说: 2019年可能会是过去十年里最差的一年 但却是未来十年里最好的一年 真的是这样吗? “每月工资1w,如何赚到200w?” 同样一个问题,问不同的人会得到不同的答案. 有一类人,开始 ...

  5. Java non-javadoc

    Java注释 non-javadoc 表示该处没有自己的注释, @see javax.servlet.Servlet#init() 参考see后面的链接 /* * (non-javadoc) * @s ...

  6. XSS-DVWA

    1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...

  7. 方正 ignb路由器设置备份(自用笔记)

    192.168.15.96255.255.255.0192.168.15.1219.232.46.61219.141.136.10

  8. Linux下端口映射工具rinetd

    Linux下简单好用的工具rinetd,实现端口映射/转发/重定向官网地址http://www.boutell.com/rinetd 软件下载wget http://www.boutell.com/r ...

  9. decode 函数及其用法

    http://blog.csdn.net/oscar999/article/details/18399177

  10. 一个demo 理解 vuex

    相比接触vue的同学们已经看了官方文档了.这里我用一个简单的demo来阐述下vuex的知识点,虽然简单,但是容易理解.也加深自己的记忆. 用脚手架建立个项目vue init webpakc-simpl ...