springMVC学习笔记(二):controller参数

一、mybatis分页插件(PageHelper插件)

文档地址:查看文档

1、 数据库分页

数据库分页:物理分页和内存分页
1)内存分页:先将所有数据加载内存中,然后从内存中查找分页的数据。
2)物理分页:在数据检索数据的时候,只检索分页数据,将数据返回给客户端。
MyBatis提供内存分页:RowBounds参数。
Mybatis物理分页:
1、自己写sql语句 sql limit ?,?
2、MyBatis插件 MyBatis PageHelper

2、PageHelper的使用步骤

第1步:导人jar

pagehelper-5.1.9.jar
jsqlparser-2.1.jar

第2步:spring集成(spring-datasource.xml)

       <!--分页插件  -->
       <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
                <property name="properties">
                    <value>
                        helperDialect=mysql
                        reasonable=true
                        supportMethodsArguments=true
                        params=count=countSql
                        autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
        </array>
    </property>

第3步:在service层进行调用分页的方法

		PageHelper.startPage(2, 3);
		List<Users> list = usersMapper.selectByExample(null);
		System.out.println(list);

注意:
1)PageHelper.startPage(2, 3); 查询第2页 每页显示3条。
2)只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select)方法会被分页。

二、jsp中关于Date数据类型的显示

方式:jsp的jstl的format库 (c库)

第1步:<%@taglib uri=“http://java.sun.com/jstl/fmt_rt” prefix=“fmt”%>

第2步:<fmt:formatDate value="${user.birthday}" pattern=“yyyy-MM-dd HH:mm:ss”/>
将controller传递过来的Date类型数据,格式成指定格式的字符串日期。

				**注**:HH是24小时制      hh是12小时制

三、spring mvc 处理器@controller的参数的绑定

1、基本类型参数绑定 : 自动绑定

要求:处理器方法的参数必须与url的参数一致。

2 、pojo对象类型参数绑定: 自动绑定

要求:表达的元素的name必须与pojo的属性名一致。

3、控制的方法默认参数:request ,response,model,session。
4、集合

1)数组

注意:批量删除不要逐个删,应该通过batch的删除语句
delete from users where id in (33,57)

2) list集合

3) Map集合

5、 Controller参数其他东西

1)、@RequestParam

@RequestParam(name=“pageNum”,defaultValue=“1”)
作用1) 可以设置默认值
作用2) 参数映射 前端传过来的参数名和后端controller接口的参数名不一致

2)、restFul风格–参数传递

http://localhost:8080/应用名/user/delete?id=33 非restFul风格传参
http://localhost:8080/应用名/user/delete/33 restFul风格传参
注意:需要@PathVariable 进行参数绑定

补充

  1. http请求的响应码:
    404 : 找打不资源
    500 : 系统代码错误
    200 : 请求成功
    400 : Bad Request 请求无法接受–一般参数绑定失败【重点】
  2. spring mvc处理日期参数绑定的问题【重点】:
    方式一:@DateTimeFormat(pattern=“yyyy-MM-dd”) 实现
    前提:spring-web.xml开启mvc注解配置
<mvc:annotation-driven></mvc:annotation-driven>替换
                    
<!-- 1.映射器:HandlerMapping  url==>handler -->

<bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

<!-- 2.适配器: HandlerAdatper   调用 Handler==>Controller-->

<bean  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

mvc:annotation-driven支持@DateTimeFomat和@NumberFormat

注意:mvc:annotation-driven配置自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter

方法二:@InitBinder只会对当前Controller的String的日期转Date的日期进行自动转换

/**
    * 注册属性编辑器(字符串转换为日期)
   */
   @InitBinder
   public void initBinder(WebDataBinder binder) throws Exception {
    
    
       binder.registerCustomEditor(Date.class, new CustomDateEditor(
               new SimpleDateFormat("yyyy-MM-dd"),true));
   }

在这里插入图片描述

	 特别注意:必须在web.xml中配置spring的编码过滤器,否则乱码。

方法三:spring mvc的扩展插件-HandlerMethodArgumentResolver(了解)

表单----->controller的方法edit(Users users)
HandlerMethodArgumentResovle用于处理客户端将请求参数传递给Controller方法
进行参数的自动绑定。
详情请点

上一章:springMVC学习笔记(一):springmvc开发流程

猜你喜欢

转载自blog.csdn.net/weixin_46822085/article/details/108984250