搭建springMVC mybatis 应用--1
搭建springMVC mybatis 应用--1
1、工作中搭建了一遍springMVC mybatis的一个应用,原本想象的很简单,但真实际做时才发现总会有各种
各样的问题。IT这个行业是最注重实践的,还是要多动手做。
纸上得来终觉浅 ,绝知此事要躬行。
2、程序的序即顺序,做程序很多时候就是要理清这"序"。
很多时候我们一些事做的很乱就是没理清楚顺序,理清顺序,遵循顺序事情才能变得简单。
3、搭建一个 spring MVC+ mybatis的应用,你先想想应当按照怎样的顺序来做?
一般来说无非两种顺序:从前向后、从后向前。
从前向后:先搭建web层 能做到接收http请求 返回响应 ,再搭建DAO层
从后向前:先搭建DAO层 能完成基本的增删改查,再搭建web层 能接收http请求。
就先选择从前向后吧
4、下一步该做什么了呢?
我要先确定使用的 spring的版本 查了下spring官网当前的release版本已到4.2.5
那就使用最新的4.2.5吧。
看官网的起步说明现在已经推荐使用spring boot 来配置spring应用,这个先不管他。
我打算还是使用传统的配置方式。
5、依赖管理使用maven , spring官网的例子就是maven的,我也使用maven。
6、在eclipse新建maven工程
7、先已经有个工程了,怎么把spring mvc 弄进来?
先想一下 spring MVC运行流程:
客户端发送http请求到 web服务器,web服务器把http请求发送封装为java对象,
传递给 spring 的DispatcherServlet,然后进入spring mvc的处理流程。
所以DispatcherServlet 是spring mvc的起点,首先要配置 spring的这个DispatchServlet。
怎么配置 这个 Servlet ?
这个和配置我们自己写的Servlet没多大区别,spring官网文档给出了例子:
<web-app> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example/*</url-pattern> </servlet-mapping> </web-app>
注意一点:
load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
1表示容器启动时就实例化这个Servlet,配成0或不配置表示这个Servlet被请求时才实例化。
8、web应用下怎么加载spring的配置文件。
对于一个非web应用 ,我们如下的方式加载spring的配置文件, 初始化ApplicationContext:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/**/*.xml");
ctx.start();
那么 web应用下怎么加载这些配置文件?
若web.xml中配置的DispatcherServlet 如 7 中那样配置,你必须配置文件
/WEB-INF/example-servlet.xml ------配置spring mvc 的 bean 的文件 。
当然我们一般不会这么干,我们还有另外一个选择,可以使用 servlet init parameter 来指定:
看下面的实例配置:
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
contextConfigLocation 指定了spring配置文件位置。
若是多个xml文件可以:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
9、DispatcherServlet 收到http请后要转发给Controller进行处理。
使用注解@Controller即可把类配置为Controller.
spring 会扫描@Controller标记的类,并注册为WebApplicationContext的bean
要在spring配置文件配置 Controller的扫描基础包路径:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="org.springframework.samples.petclinic.web"/> <!-- ... --> </beans>
10、配置<mvc:annotation-driven/>
DispatcherServlet 收到http请后要转发给Controller,这个过程还要做一系列的处理:
怎么确定把这个http请求交个哪个 Controller处理?
请求参数的如何绑定?
消息转换器怎么配置?是json还是xml
等等。
这些东西在spring mvc 的较早版本都是要配置的。
现在基本都是通过注解处理:
@NumberFormat 、@RequestMapping、@ExceptionHandler、@RequestBody
只是在配置文件中开启注解驱动即可:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> </beans>
官方文档的说明:
The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter,
and an ExceptionHandlerExceptionResolver (among others) in support of processing requests
with annotated controller methods using annotations such as @RequestMapping,
@ExceptionHandler, and others.
意思:
<mvc:annotation-driven/>注册了RequestMappingHandlerMapping、
RequestMappingHandlerAdapter、ExceptionHandlerExceptionResolver 的bean
来支持处理使用类似 @RequestMapping, @ExceptionHandler注解标记的 controller 中的方法。