SpringMVC架构与Mybatis整合

来源:互联网 发布:针对接口编程 编辑:程序博客网 时间:2024/06/03 12:01

1、SpringMVC架构

1.1 Spring web mvc 介绍

spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:
这里写图片描述

1.2 Web MVC

mvc设计模式在b/s系统下应用:
这里写图片描述

1、 用户发起request请求至控制器(Controller)
控制接收用户请求的数据,委托给模型进行处理
2、 控制器通过模型(Model)处理数据并得到处理结果
模型通常是指业务逻辑
3、 模型处理结果返回给控制器
4、 控制器将模型数据在视图(View)中展示
web中模型无法将数据直接在视图上显示,需要通过控制器完成。如果在C/S应用中模型是可以将数据在视图中展示的。
5、 控制器将视图response响应给用户
通过视图展示给用户要的数据或处理结果。

1.3 Spring web mvc 架构

这里写图片描述

架构流程 :

1、 用户发送请求至前端控制器DispatcherServlet
2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
4、 DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
5、 执行处理器(Controller,也叫后端控制器)。
6、 Controller执行完成返回ModelAndView
7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、 ViewReslover解析后返回具体View
10、 DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
11、 DispatcherServlet响应用户

组件说明:

以下组件通常使用框架提供实现:
DispatcherServlet:前端控制器
用户请求到达前端控制器,它就相当于mvc模式中的c,dispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。

HandlerMapping:处理器映射器
HandlerMapping负责根据用户请求找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。

Handler:处理器
Handler 是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。
由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler。

HandlAdapter:处理器适配器
通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。

View Resolver:视图解析器
View Resolver负责将处理结果生成View视图,View Resolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。 springmvc框架提供了很多的View视图类型,包括:jstlView、freemarkerView、pdfView等。
一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。

2、SpringMVC入门

2.1 需求

实现商品查询列表功能。

2.2 新建web工程

新建动态web工程,在设置时可将 Default output folder 设置为 WebRoot\WEB-INF\classes ,Content directory 设置为WebRoot , 可与myeclipse兼容

Java环境:jdk1.7.0_72

导入spring3.2.0和必要的jar包:
这里写图片描述

2.3 前端控制器配置

在WEB-INF\web.xml中配置前端控制器:

  <!-- springmvc前端控制器 -->  <!-- 实际就是springmvc包提供的一个servlet类 -->  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- contextConfigLocation配置springmvc加载的配置文件(用来配置处理器映射器、适配器等)         如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)     -->     <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:springmvc.xml</param-value>     </init-param>     <!-- 表示servlet随服务启动 -->     <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <!--          第一种:*.action,访问以.action 结尾时由此servlet进行解析         第二种:/,所有访问的地址都由此servlet进行解析,如果是静态文件(jpg,js,css)需要配置不让DispatcherServlet进行解析,使用此种方法可以实现RESTful风格的url         第三种:/*,此设置方法错误,因为请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功     -->    <url-pattern>*.action</url-pattern>  </servlet-mapping>

2.4 配置处理器适配器

在classpath下的springmvc.xml文件配置如下:

<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 ">    <!-- 处理器适配器 -->    <bean    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /></beans>

SimpleControllerHandlerAdapter:即简单控制器处理适配器,使用实现了org.springframework.web.servlet.mvc.Controller 接口的Handler作为
Springmvc的后端控制器。

2.5 Handler开发

需要实现 controller接口,才能由org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter适配器执行。
ModelAndView:包含了模型数据及逻辑视图名

public class ItemsController1 implements Controller {    @Override    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {        //调用service查找数据库,查询商品列表,这里使用静态数据模拟        List<Items> itemsList=new ArrayList<Items>();        Items items_1 = new Items();        items_1.setName("联想笔记本");        items_1.setPrice(6000f);        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");        Items items_2 = new Items();        items_2.setName("苹果手机");        items_2.setPrice(5000f);        items_2.setDetail("iphone6苹果手机!");        itemsList.add(items_1);        itemsList.add(items_2);        //创建ModelAndView对象,用于返回        ModelAndView modelAndView=new ModelAndView();        //相当于request的setAttribut,在jsp页面中通过itemsList取数据        modelAndView.addObject("itemsList",itemsList);        //指定视图        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");        return modelAndView;    }}

2.6 编写视图jsp

使用jstl标签取数据
/WEB-INF/jsp/items/itemsList.jsp

<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">查询条件:<table width="100%" border=1><tr><td><input type="submit" value="查询"/></td></tr></table>商品列表:<table width="100%" border=1><tr>    <td>商品名称</td>    <td>商品价格</td>    <td>生产日期</td>    <td>商品描述</td>    <td>操作</td></tr><c:forEach items="${itemsList }" var="item"><tr>    <td>${item.name }</td>    <td>${item.price }</td>    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>    <td>${item.detail }</td>    <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td></tr></c:forEach></table></form>

2.7 配置Handler(controller)

将编写Handler在spring容器加载。

springmvc.xml

    <!-- controller注册 -->    <!-- 如果请求的URL为"上下文/queryItems.action",将会映射到 ItemsController1控制器-->    <bean name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"   />

2.8 配置处理器映射器

BeanNameUrlHandlerMapping:表示将定义的Bean名字作为请求的url,需要将编写的controller在spring容器中进行配置,且指定bean的name为请求的url,且必须以.action结尾。

springmvc.xml

    <!-- 处理器映射器  -->    <!-- 根据bean的name进行查找Handler 将action的url配置在bean的name中 -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
  • 1

2.9 配置视图解析器

    <!-- 视图解析器 -->    <!--解析jsp,默认使用jstl标签,classpath下要有jstl包-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
  • 1

增加 prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为:
前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”

<property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/>
  • 1

2.10 DispatcherSerlvet.properties

DispathcerServlet作为springmvc的中央调度器存在,DispatcherServlet创建时会默认从DispatcherServlet.properties文件加载springmvc所用的各各组件,如果在springmvc.xml中配置了组件,则以springmvc.xml中配置的为准,DispatcherServlet的存在降低了springmvc各各组件之间的耦合度。

这里写图片描述



映射器:分解url找Handler(指定按bean的name,或id)
适配器:规定了按照什么方式去执行Handler,换句话来说就要求了采用不同的规则去编写Handler

2.11 非注解的处理器映射器

HandlerMapping 负责根据request请求找到对应的Handler处理器及Interceptor拦截器,将它们封装在HandlerExecutionChain 对象中给前端控制器返回。

2.11.1 BeanNameUrlHandlerMapping

BeanNameUrl处理器映射器,根据请求的url与spring容器中定义的bean的name进行匹配,从而从spring容器中找到bean实例。

<!--beanName Url映射器 --><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

2.11.2 SimpleUrlHandlerMapping

simpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增强版本,它可以将url和处理器bean的id进行统一映射配置。

<!—简单url映射 -->    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        <property name="mappings">            <props>                <prop key="/items1.action">controller的bean id</prop>                <prop key="/items2.action">controller的bean id</prop>            </props>        </property>    </bean>

多个映射器可以并存,前端控制器判断url能让哪些映射器映射,就让正确的映射器处理。

2.12 非注解的处理器适配器

2.12.1 SimpleControllerHandlerAdapter

简单控制器处理器适配器,要求编写的Handler实现org.springframework.web.servlet.mvc.Controller 接口

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

2.12.2 HttpRequestHandlerAdapter

http请求处理器适配器,要求编写的Handler实现org.springframework.web.HttpRequestHandler 接口

<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

Controller实现如下:

public class ItemsController2 implements HttpRequestHandler {    @Override    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        //调用service查找数据库,查询商品列表,这里使用静态数据模拟        List<Items> itemsList=new ArrayList<Items>();        Items items_1 = new Items();        items_1.setName("联想笔记本");        items_1.setPrice(6000f);        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");        Items items_2 = new Items();        items_2.setName("苹果手机");        items_2.setPrice(5000f);        items_2.setDetail("iphone6苹果手机!");        itemsList.add(items_1);        itemsList.add(items_2);        //设置模型数据        request.setAttribute("itemsList",itemsList);        //设置转发的视图        request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);    }}

从上边可以看出此适配器器的handleRequest方法没有返回ModelAndView,可通过response修改定义响应内容,比如返回json数据:

response.setCharacterEncoding(“utf-8”);
response.setContentType(“application/json;charset=utf-8”);
response.getWriter().write(“json串”);

2.13 注解映射器和适配器

2.13.1 映射器 RequestMappingHandlerMapping

注解式处理器映射器,对类中标记@RequestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。

从spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。

配置如下:

<!--注解映射器 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  • 1

注解描述:
@RequestMapping:定义请求url到处理器功能方法的映射

2.13.2 适配器 RequestMappingHandlerAdapter

注解式处理器适配器,对标记@ResquestMapping的方法进行适配。

从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。

配置如下:

<!--注解适配器 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

2.13.3 < mvc:annotation-driven>

springmvc使用< mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用 < mvc:annotation-driven>替代注解处理器和适配器的配置。

2.13.4 Handler开发

与非注解方式相比,可以在一个类中写多个方法

// 使用Controller标识它是一个控制器@Controllerpublic class ItemsController3 {    // 商品查询列表    //实现对queryItems方法和url进行映射,一个方法对应一个url    @RequestMapping("/queryItems")    public ModelAndView queryItems() throws Exception {        // 调用service查找数据库,查询商品列表,这里使用静态数据模拟        List<Items> itemsList = new ArrayList<Items>();        Items items_1 = new Items();        items_1.setName("联想笔记本");        items_1.setPrice(6000f);        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");        Items items_2 = new Items();        items_2.setName("苹果手机");        items_2.setPrice(5000f);        items_2.setDetail("iphone6苹果手机!");        itemsList.add(items_1);        itemsList.add(items_2);        // 创建ModelAndView对象,用于返回        ModelAndView modelAndView = new ModelAndView();        // 相当于request的setAttribut,在jsp页面中通过itemsList取数据        modelAndView.addObject("itemsList", itemsList);        // 指定视图        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");        return modelAndView;    }    //商品添加方法    //商品修改方法}
  • 1

2.13.5 在spring容器中注册Handler

2.13.5.1 单个配置方式
<bean class="cn.itcast.ssm.controller.ItemsController3" />
  • 1
2.13.5.2 组件扫描器

使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用

    <!-- 可以扫描controller、service、... ,要指定包-->    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>

注:使用spring3.2和jdk7时发现使用这种方式会报错:at org.springframework.asm.ClassReader.(Unknown Source)
把spring相关jar包都换成4.0后好了

2.14 springmvc处理流程源码分析

第一步:前端控制器接收请求
调用doDispatch
这里写图片描述

第二步:DispatcherServlet调用HandlerMapping(处理器映射器)根据url查找Handler
这里写图片描述

这里写图片描述

第三步:DispatcherServlet调用HandlerAdapter(处理器适配器)对HandlerMapping找到Handler进行包装、执行。HandlerAdapter执行Handler完成后,返回了一个ModleAndView(springmvc封装对象)

DispatcherServlet 找一个合适的适配器:
这里写图片描述

适配器执行Handler:
这里写图片描述

第四步:DispatcherServlet拿着ModelAndView调用ViewResolver(视图解析器)进行视图解析,解析完成后返回一个View(很多不同视图类型的View)
这里写图片描述

视图解析:
这里写图片描述

第五步:DispatcherServlet进行视图渲染,将Model中数据放到request域,在页面展示
渲染方法:
这里写图片描述
将model数据放在request域:
这里写图片描述

3、和myBatis整合

3.1 整合思路

springmvc+mybaits的系统架构:
这里写图片描述

第一步:整合dao层
mybatis和spring整合,通过spring管理mapper接口。
使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

第二步:整合service层
通过spring管理 service接口。
使用配置方式将service接口配置在spring配置文件中。
实现事务控制。

第三步:整合springmvc
由于springmvc是spring的模块,不需要整合。

3.2 需求

使用springmvc和mybatis完成商品列表查询。

3.3 准备环境

jar包:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、dbcp或c3p0第三方连接池、jstl包、log4j包。

工程结构:
这里写图片描述

log4j.properties

# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

db.properties

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=root

3.4 整合dao

3.4.1 配置sqlMapConfig.xml

mybatis自己的配置文件。
在classpath下创建mybatis/sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <!--全局的setting配置,根据需要添加  -->    <!-- 配置别名 -->    <typeAliases>        <!-- 批量扫描 -->        <package name="cn.itcast.ssm.po"/>    </typeAliases>    <!-- 配置mapper -->    <!-- 由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置 -->    <!-- 必须遵循:mapper.xml和mapper.java文件同名且在一个目录-->    <!--<mappers></mappers>--></configuration>

3.4.2 applicationContext-dao.xml

Spring 的配置文件。
配置数据源、事务管理,配置SqlSessionFactory、mapper扫描器。

<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:property-placeholder location="classpath:db.properties" />    <!-- 数据库连接池,使用dbcp -->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">        <property name="driverClassName" value="${jdbc.driver}" />        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <property name="maxActive" value="30" />        <property name="maxIdle" value="5" />    </bean>    <!-- sqlSessionFactory -->    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的SqlSessionFactoryBean -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 数据库连接池 -->        <property name="dataSource" ref="dataSource" />        <!-- 加载mybatis的全局配置文件 -->        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />    </bean>    <!-- mapper扫描器 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->        <property name="basePackage" value="cn.itcast.ssm.mapper"></property>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>    </bean></beans>

3.4.3 逆向工程生成po类及mapper(单表增删改查)

这里写图片描述

3.4.4 手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper。

自定义ItemsCustom类继承Items
自定义ItemsQueryVo类,里面封装ItemsCustom,用于设置查询条件

3.4.4.1 ItemsMapperCustom.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="cn.itcast.ssm.mapper.ItemsMapperCustom" >  <!-- 定义商品查询的sql片段 -->  <sql id="query_items_where">        <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->        <!-- 商品查询条件通过ItemsQueryVo包装对象中itemsCustom属性传递 -->        <if test="itemsCustom!=null and itemsCustom.name!=''">            and items.name like '%${itemsCustom.name} %'        </if>  </sql>  <!-- 商品列表查询 -->  <!-- parameterType传入包装对象(包装了查询条件) -->  <!-- resultType建议使用扩展对象 -->  <select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo" resultType="cn.itcast.ssm.po.ItemsCustom">    select items.* from items     <where>         <include refid="query_items_where"></include>    </where>  </select></mapper>
3.4.4.2 ItemsMapperCustom.java
public interface ItemsMapperCustom {    //商品查询列表    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}

3.5 整合Service

目标:
1、Service由spring管理
2、spring对Service进行事务控制。

3.5.1 定义service接口和实现类

这里写图片描述

public interface ItemsService {    //商品查询列表    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}
  • 2
//商品管理public class ItemsServiceImpl implements ItemsService{    @Autowired    private ItemsMapperCustom itemsMapperCustom;    @Override    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {        //通过ItemsMapperCustom 查询数据库        return itemsMapperCustom.findItemsList(itemsQueryVo);    }}
  • 1
  • 2

3.5.2 applicationContext-service.xml

在spring容器配置service

<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/sc hema/tx/spring-tx-3.2.xsd ">     <!-- 商品管理service -->    <bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"></bean></beans>

3.5.3 applicationContext-transaction.xml

applicationContext-transaction.xml中使用spring 声明式 事务控制方法。

<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 ">    <!-- 事务管理器 -->    <!-- 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 数据源 -->        <!-- dataSource在applicationContext-dao.xml中配置了 -->        <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="delete*" propagation="REQUIRED"/>            <tx:method name="insert*" propagation="REQUIRED"/>            <tx:method name="update*" propagation="REQUIRED"/>            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>        </tx:attributes>    </tx:advice>    <!-- aop -->    <aop:config>        <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>    </aop:config></beans>

3.6 整合springmvc

3.6.1 springmvc.xml

配置处理器映射器、适配器、视图解析器.

<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 ">    <!--对应注解的Handler进行配置  -->    <!--<bean class="cn.itcast.ssm.controller.ItemsController" />-->    <!-- 可以扫描controller、service、... 要指定包-->    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>    <!-- 注解映射器和注解适配器配置 -->    <mvc:annotation-driven></mvc:annotation-driven>    <!-- 视图解析器 -->    <!--解析jsp,默认使用jstl标签,classpath下要有jstl包-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 配置jsp路径的前缀 -->        <property name="prefix" value="/WEB-INF/jsp/"/>        <!-- 配置jsp路径的后缀 -->        <property name="suffix" value=".jsp"/>    </bean></beans>

3.6.2 web.xml

配置前端控制器

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>springmvc_mybatis</display-name>  <!-- 加载spring容器 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- springmvc前端控制器 -->  <!-- 实际就是springmvc包提供的一个servlet类 -->  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等)         如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)     -->     <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/springmvc.xml</param-value>     </init-param>     <!-- 表示servlet随服务启动 -->     <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <!--          第一种:*.action,访问以.action 结尾时由此servlet进行解析         第二种:/,所有访问的地址都由此servlet进行解析,如果是静态文件(jpg,js,css)需要配置不让DispatcherServlet进行解析,使用此种方法可以实现RESTful风格的url         第三种:/*,此设置方法错误,因为请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功     -->    <url-pattern>*.action</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

3.6.3 编写Controller(就是Handler)

// 使用Controller标识它是一个控制器@Controllerpublic class ItemsController {    @Autowired    private ItemsService itemsService;    // 商品查询列表    //实现对queryItems方法和url进行映射,一个方法对应一个url     @RequestMapping("/queryItems")    public ModelAndView queryItems() throws Exception {        // 调用service查找数据库,查询商品列表,这里使用静态数据模拟        List<ItemsCustom> itemsList = itemsService.findItemsList(null);        // 创建ModelAndView对象,用于返回        ModelAndView modelAndView = new ModelAndView();        // 相当于request的setAttribut,在jsp页面中通过itemsList取数据        modelAndView.addObject("itemsList", itemsList);        // 指定视图//      modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");        modelAndView.setViewName("items/itemsList");        return modelAndView;    }    //商品添加方法    //商品修改方法}

3.6.4 编写jsp

这里写图片描述

3.7 加载spring容器

将mapper、service、controller加载到spring容器中。

这里写图片描述
建议使用通配符加载上边的配置文件。

在web.xml中,添加spring容器监听器,加载spring容器。

  <!-- 加载spring容器 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.8 测试

at org.springframework.asm.ClassReader.(Unknown Source)
组件扫描器不起作用,把spring相关jar包都换成4.0后好了

http://localhost:8080/springmvc_mybatis/queryItems.action

4、商品修改功能开发

4.1 需求

操作流程:
1、进入商品查询列表页面
2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)
要修改的商品从数据库查询,根据商品id(主键)查询商品信息
3、在商品修改页面,修改商品信息,修改后,点击提交

4.2 开发mapper

mapper:
根据id查询商品信息
根据id更新Items表的数据
不用开发了,使用逆向工程生成的代码。

4.3 开发service

接口功能:
根据id查询商品信息
修改商品信息

    /**     * 根据id查询商品信息     * @param id 查询商品的id     * @return ItemsCustom     * @throws Exception     */    public ItemsCustom findItemsById(Integer id) throws Exception;    /**     * 修改商品信息     * @param id 要修改的商品的id     * @param itemsCustom 要修改的商品信息      * @throws Exception     */    public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;

接口实现:

    @Autowired    private ItemsMapper itemsMapper;    @Override    public ItemsCustom findItemsById(Integer id) throws Exception {        Items items=itemsMapper.selectByPrimaryKey(id);        //中间对从数据库里取出的数据进行加工        //返回ItemsCustom        ItemsCustom itemsCustom=new ItemsCustom();        //将items的属性值拷贝到itemsCustom中        BeanUtils.copyProperties(items, itemsCustom);        return itemsCustom;    }    @Override    public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {        //添加业务校验,通常在service接口对关键参数进行校验        //验证ID是否为空,如果为空抛出异常,此处用Integer就是为了验证是否为空方便        //更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括大文本类型字段        //updateByPrimaryKeyWithBLOBs要求必须传入ID        itemsCustom.setId(id);        itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);    }

4.4 开发controller

    //商品信息修改页面显示    @RequestMapping("/editItems")    public ModelAndView editItems() throws Exception{        //调用service根据商品id查询商品信息        ItemsCustom itemsCustom=itemsService.findItemsById(1);        //返回ModelAndView        ModelAndView modelAndView=new ModelAndView();        //将商品信息放在model        modelAndView.addObject("itemsCustom",itemsCustom);        //商品修改页面        modelAndView.setViewName("items/editItems");        return modelAndView;    }    //商品信息修改提交    @RequestMapping("/editItemsSubmit")    public ModelAndView editItemsSubmit() throws Exception{        //调用service更新商品信息,页面需要将商品信息传到此        //返回ModelAndView        ModelAndView modelAndView =new ModelAndView();        //返回一个成功页面        modelAndView.setViewName("success");        return modelAndView;    }
  • 1
  • 2

5、@RequestMapping

通过RequestMapping注解可以定义不同的处理器映射规则。

5.1 URL路径映射

@RequestMapping(value=”/item”)或@RequestMapping(“/item”)
value的值是数组,可以将多个url映射到同一个方法

5.2 窄化请求映射

在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。

如下:
@RequestMapping放在类名上边,设置请求前缀

@Controller
@RequestMapping(“/item”)

方法名上边设置请求映射url:
@RequestMapping放在方法名上边,如下:
@RequestMapping(“/queryItem “)

访问地址为:/item/queryItem

5.3 请求方法限定

 限定GET方法
@RequestMapping(method = RequestMethod.GET)

如果通过Post访问则报错:
HTTP Status 405 - Request method ‘POST’ not supported

 限定POST方法
@RequestMapping(method = RequestMethod.POST)

 GET和POST都可以
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})

5.4 controller方法的返回值

5.4.1 返回ModelAndView

需要方法结束时,定义ModelAndView,将model和view分别进行设置。

5.4.2 返回string

5.4.2.1返回逻辑视图名

真正视图(jsp路径)=前缀+逻辑视图名+后缀

    @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})    public String editItems(Model model) throws Exception{        //调用service根据商品id查询商品信息        ItemsCustom itemsCustom=itemsService.findItemsById(1);        //通过形参中的model将model数据传到页面        //相等于modelAndView.addObject方法        model.addAttribute("itemsCustom",itemsCustom);        return "items/editItems";    }
5.4.2.2 redirect重定向

Contrller方法返回结果重定向到一个url地址
redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。

//重定向到queryItem.action地址,request无法带过去return "redirect:queryItem.action";
5.4.2.3 forward页面转发

controller方法执行后继续执行另一个controller方法,如下商品修改提交后转向到商品修改页面,修改商品的id参数可以带到商品修改方法中。

//结果转发到editItem.action,request可以带过去return "forward:editItem.action";

forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。

5.4.3 返回void

在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
request.getRequestDispatcher(“页面路径”).forward(request, response);

2、也可以通过response页面重定向:
response.sendRedirect(“url”)

3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding(“utf-8”);
response.setContentType(“application/json;charset=utf-8”);
response.getWriter().write(“json串”);

6、参数绑定

6.1 spring参数绑定过程

注解适配器对RequestMapping标记的方法进行适配,对方法中的形参会进行参数绑定。
从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上。
这里写图片描述

6.2 默认支持的参数类型

处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。

6.2.1 HttpServletRequest

通过request对象获取请求信息

6.2.2 HttpServletResponse

通过response处理响应信息

6.2.3 HttpSession

通过session对象得到session中存放的对象

6.2.4 Model/ModelMap

ModelMap是Model接口的实现类,通过Model或ModelMap向页面传递数据,如下:

//调用service查询商品信息
Items item = itemService.findItemById(id);
model.addAttribute(“item”, item);

页面通过${item.XXXX}获取item对象的属性值。
使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

6.3 简单类型

当请求的参数名称处理器形参名称 一致 时会将请求参数与形参进行绑定。

6.3.1 整型

public String editItem(Model model,Integer id) throws Exception{

6.3.2 字符串

例子略

6.3.3 单精度/双精度

例子略

6.3.4 布尔型

处理器方法:
public String editItem(Model model,Integer id,Boolean status) throws Exception

请求url:
http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false

说明:对于布尔类型的参数,请求的参数值为true或false。

6.3.5 @RequestParam

使用@RequestParam常用于处理简单类型的绑定。

value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报;
TTP Status 400 - Required Integer parameter ‘XXXX’ is not present

defaultValue:默认值,表示如果请求中没有同名参数时的默认值

定义如下:

public String editItem(@RequestParam(value="item_id",required=true) String id) {}

形参名称为id,但是这里使用value=” item_id”限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。
注意:如果请求参数中没有item_id将跑出异常:
HTTP Status 500 - Required Integer parameter ‘item_id’ is not present

这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值

6.4 pojo类型

6.4.1 简单pojo

将pojo对象中的属性名于传递进来的属性名对应,如果传进来的参数名称对象中的属性名称 一致 则将参数值设置在pojo对象中

页面定义如下;

<input type="text" name="name"/><input type="text" name="price"/>

Contrller方法定义如下:

@RequestMapping("/editItemSubmit")    public String editItemSubmit(Items items)throws Exception{    System.out.println(items);

请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。

6.4.2 包装pojo

如果采用类似struts中 对象.属性 的方式命名,需要将pojo对象作为一个包装对象的属性,action中以该包装对象作为形参。
包装对象定义如下:

Public class QueryVo {private Items items;}

页面定义:

<input type="text" name="items.name" /><input type="text" name="items.price" />

Controller方法定义如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{   System.out.println(queryVo.getItems());

6.4.3 数组绑定

需求:商品批量删除,用户在页面选择多个商品,批量删除。
将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id。
页面定义:

<c:forEach items="${itemsList }" var="item"><tr>    <td><input type="checkbox" name="items_id" value="${item.id}"></td>    <td>${item.name }</td>    <td>${item.price }</td>    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>    <td>${item.detail }</td>    <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td></tr></c:forEach>

controller方法定义:

    //批量删除 商品信息    @RequestMapping("/deleteItems")    public String deleteItems(Integer[] items_id) throws Exception{        //调用service批量删除商品        return "success";    }

6.4.4 list绑定

需求:通常在需要批量提交数据时,将提交的数据绑定到list中,比如:成绩录入(录入多门课成绩,批量提交),本例子需求:批量商品修改,在页面输入多个商品信息,将多个商品信息提交到controller方法中。

controller方法定义:
1、进入批量商品修改页面(页面样式参考商品列表实现)
2、批量修改商品提交
使用List接收页面提交的批量数据,通过包装pojo接收,在包装pojo中定义list属性
这里写图片描述

这里写图片描述

这里写图片描述

6.4.5 map绑定

在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。
包装类中定义Map对象如下:

Public class QueryVo {private Map<String, Object> itemInfo = new HashMap<String, Object>();  //get/set方法..}

页面定义如下:

<tr><td>学生信息:</td><td>姓名:<inputtype="text" name="itemInfo['name']"/>年龄:<inputtype="text" name="itemInfo['price']"/>.. .. ..</td></tr>

Contrller方法定义如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{     System.out.println(queryVo.getStudentinfo());}

6.5 自定义参数绑定

对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。
将请求日期数据串传成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。

需要向处理器适配器中注入自定义的参数绑定组件。
自定义日期类型绑定:

public class CustomDateConverter implements Converter<String,Date>{    @Override    public Date convert(String source) {        // 实现将日期串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        try {            //根据指定类型的字符串形式,转换成Date类型            return simpleDateFormat.parse(source);        } catch (ParseException e) {            e.printStackTrace();        }        //如果参数绑定失败返回null        return null;    }}

在注解适配器配置:
springmvc.xml

    <!-- 注解映射器和注解适配器配置 -->    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>    <!-- 自定义参数绑定 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!-- 转换器 -->        <property name="converters">            <list>                <!-- 日期类型转换 -->                <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"></bean>            </list>        </property>    </bean>

配置方式2:

<!--注解适配器 -->    <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">         <property name="webBindingInitializer" ref="customBinder"></property>     </bean>    <!-- 自定义webBinder -->    <bean id="customBinder"        class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">        <property name="conversionService" ref="conversionService" />    </bean>    <!-- conversionService -->    <bean id="conversionService"        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <!-- 转换器 -->        <property name="converters">            <list>                <bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>            </list>        </property>    </bean>

7、中文乱码处理

Post时中文乱码:
在web.xml中加入:

    <!-- post乱码处理 -->    <filter>        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

对于get请求中文参数出现乱码解决方法有两个:
修改tomcat配置文件添加编码与工程编码一致,如下:

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

另外一种方法对参数进行重新编码:

String userName new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

7、与struct2不同

1、 springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过滤器。
2、 springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发,传递参数是通过类的属性,只能设计为多例。
3、 Struts采用值栈存储请求和响应的数据,通过OGNL存取数据, springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过request域传输到页面。Jsp视图解析器默认使用jstl。


本文转载自:http://blog.csdn.net/zxm1306192988/article/details/71123718