关于【Java】批量与单个的统一

先贴一些代码。

@RestController
public class ShopCartRestController implements ShopCartService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShopCartRestController.class);
    @Autowired
    private ShopCartManager     shopCartManager;

    @Override
    @RequestMapping(value = "/batchDelete", method = { RequestMethod.POST })
    public XResponse<Boolean> batchDelete(@RequestBody CommonShopCartVO commonShopCartVO) {
        try{
            int raw = shopCartManager.batchDelete(commonShopCartVO);
            if(raw  > 1){
                return new XResponse<Boolean>(XResponse.SUCCESS,"批量删除成功",true);
            }else if(raw  == 1){
                return new XResponse<Boolean>(XResponse.SUCCESS,"删除成功",true);
            }else {
                return new XResponse<Boolean>(XResponse.ERRCODE,"删除失败",false);
            }
        } catch (BizException e) {
//            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return new GanjieResponse<Boolean>(Constant.ERRCODE,e.getMessage(),false);
        }
    }
}

commonShopCartVO中gsgBizId 以 xxx,xxx,xxx/xxx 形式传过来,根据操作的长的判断是否是批量
单个的话最后得到的times等于1,多值大于1。所以可以使XResponse返回给客户端正确的提示。

    @Transactional(rollbackFor = {BizException.class, Exception.class})//->回滚注解
    public int batchDelete(CommonShopCartVO commonShopCartVO) throws BizException{
        String []codeList = commonShopCartVO.getGsgBizId().split(",");
        int times = 0;
        for(int a = 0 ; a < codeList.length ; a++){
            if(StringUtils.isNotBlank(codeList[a])){
                CommonShopCartVO param =  new CommonShopCartVO();
                param.setUserBizId(commonShopCartVO.getUserBizId());
                param.setDeleted(true);
                param.setGsgBizId(codeList[a]);
//                LOGGER.warn(JSON.toJSONString(param));
                int tiaoshu = shopCartMapper.batchDelete(param);//->删除的条数
                times += tiaoshu;
            }
        }
        return times;
    }

注:XResponse类

public class XResponse<T> implements Serializable {
    private static final long serialVersionUID = 2757354524710232976L;
    public static final String SUCCESS = "200";
    public static final String ERRCODE = "500";
    private String code;                //返回code,类似于200/500
    private String message;             //返回文字表述
    private T      data;                //返回内容:返回值
    private Object ext;                 //扩展信息

    public XResponse() {

    }
    public XResponse(String errCode, String errorMessage) {
        this.code = errCode;
        this.message = errorMessage;
    }
    //定义一个返回,做法是为了让客户端知道
    public XResponse(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    //封装
    ···
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,183评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,170评论 6 342
  • 复盘对我来说是个新名词,所以问了几遍,度娘了下,原来是股票用语,这个时代不接触股票的会不会被OUT. 很少坚持一件...
    S凉阅读 1,355评论 1 2
  • 刚一出樟宜机场,我就被这个小岛的美给迷惑住了。这是我第一次到热带国家,这里到处都绿意盎然,整齐的街道边种满了橡胶树...
    湄公河的日落阅读 1,427评论 0 0
  • 十年前,我是抱着小西买了一盆文竹。也是喜欢竹的性格,养得郁郁葱葱。每年五月初,文竹总会发出新的嫩芽,一天长一节...
    黛眉女侠阅读 3,342评论 0 1

友情链接更多精彩内容