前端与后端之间参数的传递与接收和@RequestBody,@Requestparam,@Param三个注解的使用
阅读原文时间:2023年07月08日阅读:1

参数在前台通过对象的形式传递到后台,在后台,可以用@RequestBody注解通过Map或JSONObject接收(太麻烦,既要从Map中取值,取完值后又要封装到Map),也可以用@RequestParam注解通过具体的属性接收。在dao层可以通过Map将参数传递到mapper.xml,也可以用@Param注解将具体的属性值传递到Mapper.xml。

前端代码:

前端传递给后台一个对象。

data() {
return {
listQuery: {
page: 1,
limit: 20,
dataCode: null,
dataName: null,
// enterpriseId: null,
enterpriseType: null
},
},

getList() {
this.listLoading = true
dataSet.getList(this.listQuery).then(response => {
if (response.success) {
this.list = response.data
this.total = response.total
} else {
this.$message.error("数据集合信息获取失败")
}
}).catch(error => {
console.log(error)
this.$message.error("发生内部错误,请联系管理员")
}).finally(() => {
this.listLoading = false
})
},

getList(query) {
return request({
url: '/dataSet/list',
method: 'post',
params: query
})
},

后台代码:

controller用@RequestParam注解接收单个属性

@RestController
@RequestMapping("api/dataSet")
@Validated
public class DataSetController {
private static Logger logger = LoggerFactory.getLogger(DataSetController.class);

@Autowired  
private DataSetService dataSetService;  
@PostMapping("/list")  
public Result getDataSetList(  
        @RequestParam(required = false) String dataCode,  
        @RequestParam(required = false) String dataName,  
        @RequestParam(name = "page", defaultValue = "1") int pageIndex,  
        @RequestParam(name = "limit", defaultValue = "10") int length  
) {  
    return dataSetService.getDataSetList(dataCode, dataName, pageIndex, length);  
}  

}

也可以用Map或JSONObject接收前端传递过来的整个对象

@RestController
@RequestMapping("api/dataSet")
@Validated
public class DataSetController {
private static Logger logger = LoggerFactory.getLogger(DataSetController.class);

@Autowired  
private DataSetService dataSetService;  
@PostMapping("/list")  
public Result getDataSetList(@RequestBody Map map ) {  

      String dataCode = (String) map.get("dataCode");
      String dataName = (String) map.get("dataName");
      Integer pageIndex = (Integer) map.get("pageIndex");
      Integer length = (Integer) map.get("length");

      return dataSetService.getDataSetList(dataCode, dataName, pageIndex, length); } }

@RestController
@RequestMapping("api/dataSet")
@Validated
public class DataSetController {
private static Logger logger = LoggerFactory.getLogger(DataSetController.class);

@Autowired  
private DataSetService dataSetService;  
@PostMapping("/list")  
public Result getDataSetList(@RequestBody JSONObject map ) {  

      String dataCode = map.getString("dataCode");
      String dataName = map.getString("dataName");
      Integer pageIndex = map.getIntValue("pageIndex");
      Integer length = map.getIntValue("length");
      return dataSetService.getDataSetList(dataCode, dataName, pageIndex, length); } }

service中可以将参数封装到Map中再传递到dao层,即使是controller层用Map接收,service层中也要从Map中取值。

public Result getDataSetList(String dataCode, String dataName, int pageIndex, int length) {
Map map = new HashMap();
map.put("dataCode", dataCode);
map.put("dataName", dataName);
PageHelper.startPage(pageIndex, length);
List dataSetList = dataSetMapper.selectDataSetList(map);
PageInfo pageInfo = new PageInfo(dataSetList);
return Result.operating("数据集合查询成功", true, Result.SUCCESS, pageInfo.getList(), (int) pageInfo.getTotal(), pageInfo.getPages());
}

service中也可以直接将参数分别传递到dao层

public Result getDataSetList(String dataCode, String dataName, int pageIndex, int length) {
PageHelper.startPage(pageIndex, length);
List dataSetList = dataSetMapper.selectDataSetList(dataCode,dataName);
PageInfo pageInfo = new PageInfo(dataSetList);
return Result.operating("数据集合查询成功", true, Result.SUCCESS, pageInfo.getList(), (int) pageInfo.getTotal(), pageInfo.getPages());
}

如果service中将参数封装到Map中再传递到dao层,则dao层用Map

public interface DataSetMapper extends Mapper {
List selectDataSetList(Map map);
}

如果service中直接将参数分别传递到dao层,则用@Param注解接收

public interface DataSetMapper extends Mapper {
List selectDataSetList(@Param("dataCode") String dataCode,@Param("dataName") String dataName); }

mapper.xml:

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章