封装类代码如下:
public class ResponseResult<T> {
/**
* 状态码
*/
private String status;
/**
* 提示
*/
private String msg;
/**
* 数据
*/
private T data;
}
为了节省篇幅,get和set、Constructor就不放上去了,由于我们不知道返回的到底是具体哪一个对象还是list或者是String等等,在这里我们使用泛型(不懂的同学可以去访问了解一下,https://blog.csdn.net/qq_28761767/article/details/80938968),而在实际使用过程中,我们只要将数据封装到ResponseResult<T>中返回给前端就可以了,接下来我们看看实际使用,代码如下:
//取出总数据行数
int totalCount = goodsSpuMapper.selectCount();
ResponseResult<List<GoodsDetile>> responseResult;
//判断是否有数据再进行查询
if (totalCount > 0) {
PageUtils pageUtils = new PageUtils(totalCount, pageSize, totalPages);
List<GoodsDetile> list = goodsSpuMapper.selectAll(pageUtils.getOffset(), pageUtils.getPageSize());
responseResult = new ResponseResult<List<GoodsDetile>>("success", "成功!", list);
if (pageUtils.getMessage().equals("error")) {
responseResult.setStatus("error");
responseResult.setMsg("已经到最后一页!");
}
}else {
responseResult = new ResponseResult<List<GoodsDetile>>("error", "未查到数据!", null);
}
return responseResult;
在这里最终返回的数据为
{
"status": "success",
"msg": "成功!",
"data": [
{
"spuId": 1,
"spuNo": "100",
"goodsName": "路由器",
"lowPrice": 298,
"categoryId": 1,
"brandId": 1,
"createTime": "2019-03-21T03:41:02.000+0000",
"updateTime": "2019-04-03T07:15:14.000+0000"
},
{
"spuId": 2,
"spuNo": "111",
"goodsName": "笔记本电脑",
"lowPrice": 5555,
"categoryId": 2,
"brandId": 2,
"createTime": "2019-04-03T06:09:38.000+0000",
"updateTime": "2019-04-04T06:09:42.000+0000"
}
]
}
如有错误,请及时指出,感谢大家。