一、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:
#如果使用高版本驱动 则添加cj
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: 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{
@Autowired
private UserMapper userMapper;
@Override
public 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{
@Autowired
private UserMapper userMapper;
//MP 可以根据对象中不为null的属性拼接where条件
@Override
public 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=xxx
3.请求的结构冗余. 不合适多个参数的写法.
请求优化: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{
@Autowired
private UserMapper userMapper;
//规则:字段与属性的逻辑运算符为 '=' 号时,使用实体(user)对象封装
@Override
public 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{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserByIds(Integer[] ids) {
List<Integer> idList = Arrays.asList(ids);
return userMapper.selectBatchIds(idList);
}
}