From eca02f24bd842aa7c0cd7d4e0fb51f82899d1e07 Mon Sep 17 00:00:00 2001 From: macro Date: Sat, 13 Apr 2019 16:22:41 +0800 Subject: [PATCH] =?UTF-8?q?demo=E9=80=9A=E7=94=A8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/macro/mall/config/Swagger2Config.java | 2 +- .../mall/demo/controller/DemoController.java | 41 +++++++------ .../com/macro/mall/demo/dto/CommonPage.java | 60 +++++++++++++++++++ .../com/macro/mall/demo/dto/CommonResult.java | 59 ++++++------------ 4 files changed, 103 insertions(+), 59 deletions(-) create mode 100644 mall-demo/src/main/java/com/macro/mall/demo/dto/CommonPage.java diff --git a/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java b/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java index 3e0041b..a3da046 100644 --- a/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java +++ b/mall-admin/src/main/java/com/macro/mall/config/Swagger2Config.java @@ -69,7 +69,7 @@ public class Swagger2Config { .build(); } - List defaultAuth() { + private List defaultAuth() { List result = new ArrayList<>(); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; diff --git a/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java b/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java index c538802..cd858f8 100644 --- a/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java +++ b/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java @@ -1,8 +1,10 @@ package com.macro.mall.demo.controller; +import com.macro.mall.demo.dto.CommonPage; import com.macro.mall.demo.dto.CommonResult; import com.macro.mall.demo.dto.PmsBrandDto; import com.macro.mall.demo.service.DemoService; +import com.macro.mall.model.PmsBrand; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; @@ -14,6 +16,8 @@ import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + /** * 测试controller */ @@ -28,24 +32,24 @@ public class DemoController { @ApiOperation(value = "获取全部品牌列表") @RequestMapping(value = "/brand/listAll", method = RequestMethod.GET) @ResponseBody - public Object getBrandList() { - return new CommonResult().success(demoService.listAllBrand()); + public CommonResult> getBrandList() { + return CommonResult.success(demoService.listAllBrand()); } @ApiOperation(value = "添加品牌") @RequestMapping(value = "/brand/create", method = RequestMethod.POST) @ResponseBody - public Object createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) { + public CommonResult createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) { if (result.hasErrors()) { - return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); + return CommonResult.validateFailed(result.getFieldError().getDefaultMessage()); } CommonResult commonResult; int count = demoService.createBrand(pmsBrand); if (count == 1) { - commonResult = new CommonResult().success(pmsBrand); + commonResult = CommonResult.success(pmsBrand); LOGGER.debug("createBrand success:{}", pmsBrand); } else { - commonResult = new CommonResult().failed(); + commonResult = CommonResult.failed("操作失败"); LOGGER.debug("createBrand failed:{}", pmsBrand); } return commonResult; @@ -54,17 +58,17 @@ public class DemoController { @ApiOperation(value = "更新品牌") @RequestMapping(value = "/brand/update/{id}", method = RequestMethod.POST) @ResponseBody - public Object updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto,BindingResult result) { + public CommonResult updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto,BindingResult result) { if(result.hasErrors()){ - return new CommonResult().validateFailed(result.getFieldError().getDefaultMessage()); + return CommonResult.validateFailed(result.getFieldError().getDefaultMessage()); } CommonResult commonResult; int count = demoService.updateBrand(id, pmsBrandDto); if (count == 1) { - commonResult = new CommonResult().success(pmsBrandDto); + commonResult = CommonResult.success(pmsBrandDto); LOGGER.debug("updateBrand success:{}", pmsBrandDto); } else { - commonResult = new CommonResult().failed(); + commonResult = CommonResult.failed("操作失败"); LOGGER.debug("updateBrand failed:{}", pmsBrandDto); } return commonResult; @@ -73,29 +77,30 @@ public class DemoController { @ApiOperation(value = "删除品牌") @RequestMapping(value = "/brand/delete/{id}", method = RequestMethod.GET) @ResponseBody - public Object deleteBrand(@PathVariable("id") Long id) { + public CommonResult deleteBrand(@PathVariable("id") Long id) { int count = demoService.deleteBrand(id); if (count == 1) { LOGGER.debug("deleteBrand success :id={}", id); - return new CommonResult().success(null); + return CommonResult.success(null); } else { LOGGER.debug("deleteBrand failed :id={}", id); - return new CommonResult().failed(); + return CommonResult.failed("操作失败"); } } @ApiOperation(value = "分页获取品牌列表") @RequestMapping(value = "/brand/list", method = RequestMethod.GET) @ResponseBody - public Object listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, - @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) { - return new CommonResult().pageSuccess(demoService.listBrand(pageNum, pageSize)); + public CommonResult> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, + @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) { + List brandList = demoService.listBrand(pageNum, pageSize); + return CommonResult.success(CommonPage.restPage(brandList)); } @ApiOperation(value = "根据编号查询品牌信息") @RequestMapping(value = "/brand/{id}", method = RequestMethod.GET) @ResponseBody - public Object brand(@PathVariable("id") Long id) { - return new CommonResult().success(demoService.getBrand(id)); + public CommonResult brand(@PathVariable("id") Long id) { + return CommonResult.success(demoService.getBrand(id)); } } diff --git a/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonPage.java b/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonPage.java new file mode 100644 index 0000000..134c750 --- /dev/null +++ b/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonPage.java @@ -0,0 +1,60 @@ +package com.macro.mall.demo.dto; + +import com.github.pagehelper.PageInfo; + +import java.util.List; + +/** + * 分页数据封装类 + */ +public class CommonPage { + private Integer pageNum; + private Integer pageSize; + private Long totalPage; + private List list; + + /** + * 将PageHelper分页后的list转为分页信息 + */ + public static CommonPage restPage(List list) { + CommonPage result = new CommonPage<>(); + PageInfo pageInfo = new PageInfo<>(list); + result.setTotalPage(pageInfo.getTotal() / pageInfo.getPageSize()); + result.setPageNum(pageInfo.getPageNum()); + result.setPageSize(pageInfo.getPageSize()); + result.setList(pageInfo.getList()); + return result; + } + + public Integer getPageNum() { + return pageNum; + } + + public void setPageNum(Integer pageNum) { + this.pageNum = pageNum; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Long getTotalPage() { + return totalPage; + } + + public void setTotalPage(Long totalPage) { + this.totalPage = totalPage; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } +} diff --git a/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonResult.java b/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonResult.java index 9198352..af841db 100644 --- a/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonResult.java +++ b/mall-demo/src/main/java/com/macro/mall/demo/dto/CommonResult.java @@ -1,58 +1,36 @@ package com.macro.mall.demo.dto; -import com.github.pagehelper.PageInfo; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * 通用返回对象 */ -public class CommonResult { +public class CommonResult { public static final int SUCCESS = 0; public static final int FAILED = 1; public static final int VALIDATE_FAILED = 2; private int code; private String message; - private Object data; + private T data; /** * 普通成功返回 * * @param data 获取的数据 */ - public CommonResult success(Object data) { - this.code = SUCCESS; - this.message = "操作成功"; - this.data = data; - return this; - } - - /** - * 返回分页成功数据 - */ - public CommonResult pageSuccess(List data) { - PageInfo pageInfo = new PageInfo(data); - long totalPage = pageInfo.getTotal() / pageInfo.getPageSize(); - Map result = new HashMap<>(); - result.put("pageSize", pageInfo.getPageSize()); - result.put("totalPage", totalPage); - result.put("pageNum", pageInfo.getPageNum()); - result.put("list", pageInfo.getList()); - this.code = SUCCESS; - this.message = "操作成功"; - this.data = result; - return this; + public static CommonResult success(T data) { + CommonResult result = new CommonResult(); + result.setCode(SUCCESS); + result.setData(data); + return result; } /** * 普通失败提示信息 */ - public CommonResult failed() { - this.code = FAILED; - this.message = "操作失败"; - return this; + public static CommonResult failed(String message) { + CommonResult result = new CommonResult(); + result.setCode(FAILED); + result.setMessage(message); + return result; } /** @@ -60,10 +38,11 @@ public class CommonResult { * * @param message 错误信息 */ - public CommonResult validateFailed(String message) { - this.code = VALIDATE_FAILED; - this.message = message; - return this; + public static CommonResult validateFailed(String message) { + CommonResult result = new CommonResult(); + result.setCode(VALIDATE_FAILED); + result.setMessage(message); + return result; } public int getCode() { @@ -82,11 +61,11 @@ public class CommonResult { this.message = message; } - public Object getData() { + public T getData() { return data; } - public void setData(Object data) { + public void setData(T data) { this.data = data; } }