这里主要有@PathVariable和@RequestParam注解两种传参方式
本文的单值传参是根据查询功能写的,多值传参是根据更新功能写的,这里的更新功能中含有复选框的级联操作,需要的小伙伴可以去这里(复选框的级联操作
)看看喔~~
如果本文对您有帮助的话请点个小心心,谢谢撒!!!
1.@PathVariable注解传参:接收请求路径中占位符的值
单值传递:
前端:
function queryById(hosrId) {
$.ajax({
type: "GET",
url: "/register/"+hosrId, //RESTful风格的API定义
data: "",
success: function (vo) {
······
}
});
}
后端:
Service层:
@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
public Hosregister queryByHosId(int hosrId){
Hosregister hosregister = registerMapper.selectByPrimaryKey(hosrId);
hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getdId()));
return hosregister;
}
Controller层:
@RequestMapping(value = "{id}",method = RequestMethod.GET)
public ResultVO<Hosregister> queryByHosId(@PathVariable("id") Integer hosrId){
Hosregister hosregister = registerService.queryByHosId(hosrId);
return new ResultVO<>(hosregister);
}
多值传递:
前端:
$(function (){
//根据地址获取teamId,如果null则执行增加,否则执行更新
let url = document.location.toString();//获取URL
let hosrId=url.GetValue("hosrId");
queryById(hosrId);
queryKeShi();
//提交按钮的单击事件--实现真正的更新
$("#btnUpdate").click(function () {
let myselect1=document.getElementById("dep");
let index1=myselect1.selectedIndex ;
let depId=myselect1.options[index1].value;
let myselect2=document.getElementById("doc");
let index2=myselect2.selectedIndex ;
let dId=myselect2.options[index2].value;
$.ajax({
type: "POST",//这里必须写POST
url: "/register/load/"+hosrId+"/"+depId+"/"+dId,
data: $("#myForm").serialize()+"&_method=PUT",
dataType:"json",
success: function(vo){
if(vo.code==200) {
window.location.href = "/pages/registration/index.html";
}else{
alert("更新失败!"+vo.msg);
}
}
});
});
});
后端:
Service层:
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
public int updateRegister(Hosregister hosregister){
return registerMapper.updateByPrimaryKeySelective(hosregister);
}
Controller层:
@RequestMapping(value = "load/{hosrId}/{depId}/{dId}",method = RequestMethod.PUT)
public ResultVO<Hosregister> updateRegister(@PathVariable("hosrId") Integer hosrId,
@PathVariable("depId") Integer depId,
@PathVariable("dId") Integer dId,
Hosregister hosregister){
hosregister.setHosrId(hosrId);
hosregister.setdId(dId);
hosregister.setDepId(depId);
int i = registerService.updateRegister(hosregister);
if (i == 1){
return new ResultVO<Hosregister>();
}
return new ResultVO<>(500,"更新失败,服务器内部异常,请稍后再试!");
}
2.@RequestParam注解传参:将请求参数区数据映射到功能处理方法的参数上(注意:请求路径的等号不能丢!!!)
单值传递:
前端:
function queryById(hosrId) {
$.ajax({
type: "GET",
url: "/register"+"?hosrId="+hosrId,
data: "",
success: function (vo) {
......
}
});
}
后端:
Service层:
@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
public Hosregister queryByHosId(int hosrId){
Hosregister hosregister = registerMapper.selectByPrimaryKey(hosrId);
hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getdId()));
return hosregister;
}
Controller层:
@RequestMapping(value = "",method = RequestMethod.GET)
public ResultVO<Hosregister> queryByHosId(@RequestParam("hosrId") Integer hosrId){
Hosregister hosregister = registerService.queryByHosId(hosrId);
return new ResultVO<>(hosregister);
}
多值传递:
前端:
$(function (){
//根据地址获取teamId,如果null则执行增加,否则执行更新
let url = document.location.toString();//获取URL
let hosrId=url.GetValue("hosrId");
queryById(hosrId);
queryKeShi();
//提交按钮的单击事件--实现真正的更新
$("#btnUpdate").click(function () {
let myselect1=document.getElementById("dep");
let index1=myselect1.selectedIndex ;
let depId=myselect1.options[index1].value;
let myselect2=document.getElementById("doc");
let index2=myselect2.selectedIndex ;
let dId=myselect2.options[index2].value;
$.ajax({
type: "POST",//这里必须写POST
url: "/register/load"+"?hosrId="+hosrId+"&depId="+depId+"&dId="+dId,
data: $("#myForm").serialize()+"&_method=PUT",
dataType:"json",
success: function(vo){
if(vo.code==200) {
window.location.href = "/pages/registration/index.html";
}else{
alert("更新失败!"+vo.msg);
}
}
});
});
});
后端:
Service层:
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
public int updateRegister(Hosregister hosregister){
return registerMapper.updateByPrimaryKeySelective(hosregister);
}
Controller层:
@RequestMapping(value = "load",method = RequestMethod.PUT)
public ResultVO<Hosregister> updateRegister(@RequestParam("hosrId") Integer hosrId,
@RequestParam("depId") Integer depId,
@RequestParam("dId") Integer dId,
Hosregister hosregister){
hosregister.setHosrId(hosrId);
hosregister.setdId(dId);
hosregister.setDepId(depId);
int i = registerService.updateRegister(hosregister);
if (i == 1){
return new ResultVO<Hosregister>();
}
return new ResultVO<>(500,"更新失败,服务器内部异常,请稍后再试!");
}