一、SpringMVC参数传递(三种方式)

项目结构:

pojo:

package com.jt.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;import java.io.Serializable;@Data
@Accessors(chain = true)
//1.将对象与表进行关联
//规则1:如果表明与对象名一致,则名称可以省略
//规则2:如果字段名与属性名一致,则注解可以省略
@TableName("demo_user")
//序列化接口的作用:保证对象网络传输的有效性!!!
public class User implements Serializable {//2.主键 --- 主键自增[AUTO]、非空、UUID[ASSIGN_UUID](生成唯一编号)@TableId(type = IdType.AUTO) //主键的意思private Integer id;//3.标识属性与字段的映射
//    @TableField("name")private String name;private Integer age;private String sex;
}

UserMapper:

package com.jt.mapper;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
//@Mapper //将该接口交给Spring容器管理,Spring创建对象/*** 1.规则1:继承BaseMapper时,必须添加泛型对象* 2.规则2:自己的方法不要与接口方法重名*/
public interface UserMapper extends BaseMapper<User> {}

UserMapperImpl:

package com.jt.service;import com.jt.pojo.User;import java.util.List;//统一代码规范
public interface UserService {User findUserById(Integer id);List<User> findUserByNS(User user);List<User> findUserByNA(User user);List<User> getUserByIds(Integer[] ids);
}

application.yml:

# 端口配置
Server:port: 8090#配置数据源
spring:datasource:#如果使用高版本驱动 则添加cjdriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=trueusername: rootpassword: root#Spring整合MP
mybatis-plus:#定义别名包type-aliases-package: com.jt.pojo#导入映射文件mapper-locations: classpath:/mappers/*.xml#开启驼峰映射configuration:map-underscore-to-camel-case: true#为com.jt.mapper包下的SQL执行打印日志
logging:level:com.jt.mapper: debug

启动类RunApp.java:

package com.jt;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.jt.mapper")
public class RunApp {public static void main(String[] args) {SpringApplication.run(RunApp.class, args);}
}

- 1.简单的参数接受

- - 1.需求分析

根据id查询用户信息
URL:http://localhost:8090/findUserById?id=1

- - 2.编辑UserController

package com.jt.controller;import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController // 获取请求+响应json数据
//@RestController = @Controller + @ResponseBody
//@Controller // 和上面的@RestController注解基本无差别,唯一少了一个@ResponseBody注解
//@ResponseBody // 将服务端数据转换给JSON串返回
public class UserController {@Autowired//编码规则:面向接口编程 解耦private UserService userService;/*** URL地址:http://localhost:8090/findUserById?id=1* 请求类型:Get/DELETE      /POST/PUT* 参数:id=1(由用户在浏览器中传入的值)* 返回值结果:浏览器中返回User对象的json串* 不同类型的请求注解:*      @PostMapping("")*      @PutMapping("")*      @GetMapping("")*      @DeleteMapping("")** 参数说明:*      1.参数的名称必须与URL中的名称一致*      2.SpringMVC可以根据用户的需求,自动的实现类型的转化*          底层实现:SpringMVC所有的参数默认都是String类型*                   根据用户参数的类型,自动实现转化*/// @RequestMapping(value = "findUserById",method = RequestMethod.GET)@GetMapping("findUserById")// 只允许接收get类型public User findUserById(Integer id){return userService.findUserById(id);}
}

- - 3.编辑UserServiceImpl

package com.jt.service;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;@Overridepublic User findUserById(Integer id) {return userMapper.selectById(id);}
}

- - 4.页面效果展现

- 2.对象参数接受

- - 1.需求

案例: 根据name=“王昭君” sex=女 查询用户数据?
URL: http://localhost:8090/findUserByNS?name=王昭君&sex=女

- - 2.编辑UserController

package com.jt.controller;import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController // 获取请求+响应json数据
public class UserController {@Autowired//编码规则:面向接口编程 解耦private UserService userService;/*** 规则:SpringMVC 可以利用对象的方式接收* 底层实现:参数name="xxx"  拼接set形成setName,之后检查对象中是否有对应的setName(),如果匹配到该方法,则为对象赋值* 注意事项:参数名最好以属性名一致** URL地址:http://localhost:8090/findUserNS?name=王昭君&sex=女* 参数:name=xxx&sex=xxx* 返回值:List<User>*/@GetMapping("findUserNS")// 只允许接收get类型public List<User> findUserByNS(User user){return userService.findUserByNS(user);}
}

- - 3.编辑UserServiceImpl

package com.jt.service;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;//MP 可以根据对象中不为null的属性拼接where条件@Overridepublic List<User> findUserByNS(User user) {QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);return userMapper.selectList(queryWrapper);}
}

- - 4.页面展示

- 3.RESTFul参数接受

- - 1.RESTFul

- - - 1.RestFul介绍
REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。
定义: RESTFul 是一种请求的规则(语法/定义)
- - - 2.RESTFul说明
Get请求: http://localhost:8090/findUserByNS?name=王昭君&sex=女
信息: 1.查询请求2.参数直观 name=xxx3.请求的结构冗余. 不合适多个参数的写法.请求优化:http://localhost:8090/user/王昭君/女优势:1. 用户不能了解请求的意图 规定:请求方法名称不能出现 “动词”,只能写名词.2. 参数保密, 只有后端服务器清楚参数的意义.3. 请求字节传输量少 简洁.
注意事项:1. URL地址中参数与参数之间使用 /分隔.2. 请求的参数的位置一旦固定,不可轻易修改.3. 用户发请求时,就应该按照restFul的结构执行.4. restFul请求一般以get请求为主. put/delete/post

- - 2.案例

- - - 1.需求
查询 name="貂蝉" age>10岁 的用户
URL:http://localhost:8090/user/貂蝉/10
- - - 2.编辑UserController
package com.jt.controller;import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import javax.naming.Name;
import java.util.List;@RestController // 获取请求+响应json数据
public class UserController {@Autowired//编码规则:面向接口编程 解耦private UserService userService;/*** 后端服务器接收规则:*      1.参数与参数之间使用 / 分割*      2.参数位置一旦确定,一般不变*      3.接收的参数使用 {形参变量}*      4.使用特定的注解@PathVariable 接收*      5.如果参数有多个建议使用对象接受  参数必须与属性一致,SpringMVC自动封装* 注意:如果名称不统一,则需要转换,具体如下:*                              @PathVariable("name")  String username* URL:http://localhost:8090/user/貂蝉/10* 参数:name/age* 返回值:List<User>*/@GetMapping("user/{name}/{age}")public List<User> findUserByNA(User user){    return userService.findUserByNA(user);}/*说明一:@GetMapping("user/{name}/{age}")public List<User> findUserByNA(@PathVariable("name") String name,@PathVariable Integer age){System.out.println(name);System.out.println(age);return null;}*/
}
- - - 3.编辑UserServiceImpl
package com.jt.service;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;//规则:字段与属性的逻辑运算符为 '=' 号时,使用实体(user)对象封装@Overridepublic List<User> findUserByNA(User user) {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name", user.getName()).gt("age", user.getAge());return userMapper.selectList(queryWrapper);}
}
- - - 4.页面展示

- 4.同名提交问题

- - 1.需求

用户查询 id=1,3,4,5 的数据 如果有同名的参数一般采用 ',' 号分割
URL:http://localhost:8090/getUserByIds?ids=1,3,4,5

- - 2.编辑UserController

package com.jt.controller;import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import javax.naming.Name;
import java.util.List;@RestController // 获取请求+响应json数据
public class UserController {@Autowired//编码规则:面向接口编程 解耦private UserService userService;/*** 规则:如果参数使用 ',' 号分隔,则SpringMVC可以自动的转化为数组。* 查询多个用户* URL:http://localhost:8090/getUserByIds?ids=1,3,4,5* 参数:ids = 1,3,4,5* 返回值:List<User>*/@GetMapping("/getUserByIds")public List<User> getUserByIds(Integer[] ids){return userService.getUserByIds(ids);}
}

- - 3.编辑UserServiceImpl

package com.jt.service;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Arrays;
import java.util.List;@Service
public class UserServiceImpl implements UserService{@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> getUserByIds(Integer[] ids) {List<Integer> idList = Arrays.asList(ids);return userMapper.selectBatchIds(idList);}
}

- - 4.页面效果展现

SpringMVC的参数传递相关推荐

  1. Required Long parameter is not present,SpringMVC的参数传递问题

    不变得ajax请求: $.ajax({type: "POST",async: false,url: _ctx + "/loadList",contentType ...

  2. springMVC 相对于 Structs 的优势

    智者说,没有经过自己的思考和估量,就不能接受别人的东西.资料只能是一个参考,至于是否正确,还得自己去分辨 SpringMVC相对于Structs的几个优势: 1.springMVC安全性更高,stru ...

  3. SpringMVC 传递相同名称的参数的最佳方法

    华为云4核8G,高性能云服务器,免费试用 >>>    SpringMVC 多个对象的相同字段参数传递解决方案,在SpringMVC中,有时需要传递多个对象(除了Model和web元 ...

  4. spring框架_IOC_DI_AOP_MVC

    本文注解总结:注解收录集 Spring框架 一.Spring介绍 二.Spring-`IOC` 2.1 IOC调用原理图 2.2 Spring-IOC 配置文件方式 2.2.1 准备Dog类 2.2. ...

  5. springMVC参数传递(三)

    2019独角兽企业重金招聘Python工程师标准>>> 第一步:web.xml文件是一个工程必不可少的配置文件. <!-- lang: xml --> <?xml ...

  6. 使用SpringMVC参数传递时,解决get请求时中文乱码的问题

    问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码. 解决办法: 一, 我们需要把request.getParameter("参数名" ...

  7. 用SpringMVC参数传递时,解决get请求时中文乱码的问题

    问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码 解决办法: 方法一 把request.getParameter("参数名")获取到 ...

  8. SpringMvc参数传递

    SpringMvc两种映射规则 1.spring mvc 默认参数映射规则:名字一致  自动映射 2.spring mvc自定义映射规则:可以通过注解来实现=>  @RequestParam 传 ...

  9. mvc ajax get请求,springMVC 中 ajax get 请求和 post 请求的坑以及参数传递

    1, ajax 请求 无论为 post ,或者 get ,url中带有?形式的参数,后台都能以String类型变量接收,变量名称和参数名称必须一致 前台ajax: $.ajax( "prod ...

最新文章

  1. Python标准模块--asyncio
  2. snipaste截图软件滚动截图_截图工具的逆袭,Snipaste 还可以这么玩
  3. linux shell express,Linux下使Shell 命令脱离终端在后台运行
  4. 登陆SQL Server 2008时提示评估期已过的解决办法
  5. html和ascll有什么关联,什么是HTML ASCII(HTML ASCII)?
  6. 孩子哭的时候大人应该怎么办?
  7. Delphi之TStrings和TStringLists类【转】
  8. 60度斜坡怎么计算_坡度计算公式图解
  9. Intel8251可编程串行扩展RS232串口
  10. Java如何使用IP代理
  11. Yahoo!团队实践分享:网站性能优化的35条黄金守则
  12. leopard 全部搞定状态截图
  13. Python生成标签云/词云
  14. 对对碰(网页版单机小游戏)
  15. 沈博研:你一定要看的黄金投资优势分析!
  16. mysql配置文件生效测试
  17. 虹科方案|SCADA软件 -VTScada在饮用水工厂的使用成本
  18. 防WiFi万能钥匙蹭网,隐藏ssid就可以了
  19. iframe中由一个页面跳转到另一个页面
  20. Joost大行其道 P2P网络电视颠覆传统电视工业?

热门文章

  1. JPEG2000压缩DICOM文件的解压(三)
  2. Oxygen XML Web组织参与内容创建
  3. CVE-2014-6271“破壳”漏洞
  4. 曾经写的俄罗斯方块源码 2021-06-13
  5. Stata进行矩阵运算,求逆矩阵、特征根、特征向量
  6. 【QGIS入门实战精品教程】10.2:QGIS中DEM三维显示方法
  7. Spring Security认证_Remember Me
  8. HTML5纯css实现爱心动画效果DW、vscode来自程序员的浪漫表白
  9. WorkFlow .Net 流程会签
  10. Vue时间戳(年/月/日/时:分:秒and 刚刚/一分钟前···)