3、springmvc和mybatis整合

1.1 需求

使用springmvcmybatis完成商品列表查询。

1.2整合思路

springmvc+mybaits的系统架构:

第一步:整合dao

mybatisspring整合,通过spring管理mapper接口。

使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

第二步:整合service

通过spring管理 service接口。

使用配置方式将service接口配置在spring配置文件中。

实现事务控制。

第三步:整合springmvc

由于springmvcspring的模块,不需要整合。

1.2mybatisspring进行整合。

1.2.1 sqlMapConfig.xml

mybatis自己的配置文件。

1.2.2 applicationContext-dao.xml

配置:

数据源

SqlSessionFactory

mapper扫描器

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

 

1.2.4 手动定义商品查询mapper 

1.2.4.1 ItemsMapperCustom.xml

sql语句:

SELECT * FROM items  WHERE items.name LIKE '%笔记本%'

1.2.4.2 ItemsMapperCustom.java

1.2.5 整合service

spring管理service接口。

1.2.5.1 定义service接口

1.2.5.2 spring容器配置service(applicationContext-service.xml)

创建applicationContext-service.xml,文件中配置service

1.2.5.3 事务控制(applicationContext-transaction.xml)

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

1.2.6 整合springmvc

1.2.6.1 springmvc.xml

创建springmvc.xml文件,配置处理器映射器、适配器、视图解析器。

<!-- 可以扫描controller、service、...
    这里让扫描controller,指定controller的包
     -->
    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>
    
        
    <!--注解映射器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
    <!--注解适配器 -->
    <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
    
    <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
    mvc:annotation-driven默认加载很多的参数绑定方法,
    比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    实际开发时使用mvc:annotation-driven
     -->
    <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>

1.2.6.2 编写Controller(就是Handler)

1.2.6.3 编写jsp

1.2.7 加载spring容器

mapperservicecontroller加载到spring容器中。

建议使用通配符加载上边的配置文件。

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

商品修改功能开发

2.1 需求

操作流程:

1、进入商品查询列表页面

2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)

要修改的商品从数据库查询,根据商品id(主键)查询商品信息

3、在商品修改页面,修改商品信息,修改后,点击提交

2.2 开发mapper

mapper

根据id查询商品信息

根据id更新Items表的数据

不用开发了,使用逆向工程生成的代码。

2.3 开发service

接口功能:

根据id查询商品信息

修改商品信息

2.4 开发controller

方法:

商品信息修改页面显示

商品信息修改提交

2.5 @RequestMapping

1)url映射

定义controller方法对应的url,进行处理器映射使用。

2)窄化请求映射

 

3)限制http请求方法

出于安全性考虑,对http的链接进行方法限制。

如果限制请求为post方法,进行get请求,报错

2.6 controller方法的返回值

   返回ModelAndView需要方法结束时,定义ModelAndView,将modelview分别进行设置。

 返回string如果controller方法返回string

1、表示返回逻辑视图名。

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

2redirect重定向

商品修改提交后,重定向到商品查询列表。

redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行requestrequest无法共享)

 

3forward页面转发

通过forward进行页面转发,浏览器地址栏url不变,request可以共享。

controller方法形参上可以定义requestresponse,使用requestresponse指定响应结果:

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串");

参数绑定

3.1 spring参数绑定过程

从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上。

springmvc中,接收页面提交的数据是通过方法形参来接收。而不是在controller类定义成员变更接收!!!!

 

3.2默认支持的类型

直接在controller方法形参上定义下边类型的对象,就可以使用这些对象。在参数绑定过程中,如果遇到下边类型直接进行绑定。

3.2.1 HttpServletRequest

通过request对象获取请求信息

3.2.2 HttpServletResponse

通过response处理响应信息

3.2.3 HttpSession

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

3.2.4 Model/ModelMap

model是一个接口,modelMap是一个接口实现 。 

作用:将model数据填充到request域。

3.3 简单类型

通过@RequestParam对简单类型的参数进行绑定。

如果不使用@RequestParam,要求request传入参数名称和controller方法的形参名称一致,方可绑定成功。

如果使用@RequestParam,不用限制request传入参数名称和controller方法的形参名称一致。

通过required属性指定参数是否必须要传入,如果设置为true,没有传入参数,报下边错误:

3.4 pojo绑定

页面中inputnamecontrollerpojo形参中的属性名称一致,将页面中数据绑定到pojo

页面定义:

controllerpojo形参的定义:

 

3.5 post乱码

web.xml添加post乱码filter

web.xml中加入:

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

以上可以解决post请求乱码问题。

对于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-1tomcat默认编码,需要将tomcat编码后的内容按utf-8编码

原文地址:https://www.cnblogs.com/wyl9527/p/6444159.html