一:SpringMVC是什么
SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架。学他就是用来代替Struts2的,那么为什么不用Struts2而选择SpringMVC呢!那就必须说说它相比与struts2的部分优点:
(2)整合:大部分企业都会使用Spring,而SpringMVC就是Spring的一个子框架,当然方便些。
(3)实现机制:Struts2是基于filter过滤器的,而SpringMVC是基于servlet,以前认为filter是servlet的一种特殊,但是servelet明显比filter快。而且struts2是多例的,而SpringMVC则是单例的
(4)参数封装上:
Struts基于属性进行封装。
Springmvc基于方法封装。颗粒更细
二:SpringMVC的执行流程
理论层面
(1)用户发送请求至前端控制器DispatcherServlet;
(2) DispatcherServlet收到请求后,调用HandlerMapping处理器映射器,请求获取Handle;
(3)处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet;
(4)DispatcherServlet 调用 HandlerAdapter处理器适配器;
(5)HandlerAdapter 经过适配调用 具体处理器(Handler,也叫后端控制器);
(6)Handler执行完成返回ModelAndView;
(7)HandlerAdapter将Handler执行结果ModelAndView返回给DispatcherServlet;
(8)DispatcherServlet将ModelAndView传给ViewResolver视图解析器进行解析;
(9)ViewResolver解析后返回具体View;
(10)DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)
(11)DispatcherServlet响应用户。
实现层面
从1到14,是使用SpringMVC全部的代码执行过程,我们要做的就是
1)在Web.xml中配置前端控制器
2)在SpringMVC的配置文件(springMVC.xml)中配置处理器映射器
3)在SpringMVC的配置文件(Springmvc.xml)中配置处理器适配器(处理器映射器有三种,无论采取哪种都可以)
4)创建自定义Controller
5)配置自定义Controller的bean在Springmvc中
6)配置视图解析器
三:注解开发
常用注解
1:@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。
用于类上,则表示类中的所有响应请求的方法都是以该地址作为父路径。method=RequestMethod.GET。
2:@RequestBody:注解实现接收http请求的json数据,将json转换为java对象。
3:@ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。
4:@Controller或@RestController,其中@RestController相当于@RequestMapping+@Controller
5:@SessionAttributes:把ModelMap里面的数据放入Session里面。可以在类上面加上@SessionAttributes注解,里面包含的字符串就是要放入session里面的key
使用注解开发配置:
SpringMVC.xml添加扫描注解
<context:component-scan base-package="com.xqc"></context:component-scan>
配置注解的处理器映射器
<!-- 配置注解处理器映射器
功能:寻找执行类Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
配置注解的处理器适配器
<!-- 配置注解处理器适配器
功能:调用controller方法,执行controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
</bean>
视图解析器
<!-- 配置sprigmvc视图解析器:解析逻辑试图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
四:SpringMVC整合Spring ,Mybatis
1:创建工程
2:jar包准备,使用Maven则pom.xml准备
3:配置web.xml文件
3.1)加载SpringMVC配置文件
<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
13 <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认加载方式
默认加载必须规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.2)加载spring配置文件
1 <!-- 加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
4:配置Springmvc.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--注解开发,开启扫描-->
<context:component-scan base-package="cn.itcast"></context:component-scan> <!-- annotation-driven:默认创建了多个对象:RequestMappingHandlerMapping,RequestMappingHandlerAdapter
也就提供对json格式支持
-->
<mvc:annotation-driven/> <!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Springmvc.xml
5:配置beans.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--Spring注解开发,开启扫描-->
<context:component-scan base-package="com.xqc"></context:component-scan> <!-- 第一步:配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property> </bean> <!-- 第二步:创建sqlSessionFactory。生产sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 配置mybatis接口代理开发 * 接口类名和映射文件必须同名
* 接口类和映射文件必须在同一个目录下
* 映射文件namespace名字必须是接口的全类路径名
* 接口的方法名必须和映射Statement的id一致
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xqc.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- 第三步:事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置通知 ,管理事务的策略-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes> </tx:advice> <!-- 配置拦截service ,切面-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xqc.service.*.*(..))"/>
</aop:config> </beans>
beans.xml
6:配置 jdbc.properties 文件
jdbc.url = jdbc\:mysql\:///mybatismoder
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root
jdbc.password= 1234
jdbc.properties
7:配置Mybatis配置文件 sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> </configuration>
sqlMapConfig.xml
8:创建包结构,编写代码即可。