Java开发常用笔记,持续更新中~

BigDecimal循环累加问题
   BigDecimal total = new BigDecimal(0);//初始值需要为0
   for(AccountType listResult:list){
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("userId", user.getId());
    map.put("accountType",String.valueOf(listResult.getId()));
    UserAccount userAccount = userAccountMapper.findUserAccountByUserAndType(map);
    if(!ComUtil.isEmpty(userAccount)){
        userBalance userBalance = userBalanceMapper.findByBalance(userAccount.getId());
        if(!ComUtil.isEmpty(userBalance)){
            total = total.add(userBalance.getAmount());//累加操作
        }
    }
   }
SpringBoot与axios表单提交问题
  • 问题描述: axios 表单提交,springBoot 无法接受到数据,但使用swagger测试,可以。
  • 原因:
    1.axios的表单提交 ,content-type 默认为 application/json;charset=UTF-8
    2.提交数据会附加在payload(以JSON形式)。
    3.@ModelAttribute 可以接受表单的数据,但是content-type的类型需要为application/x-www-form。@RequestBody 可以接受表单数据,但是content-type类型需要为application/json。@RequestParam 从URL中接受请求参数。(不用于表单提交)。
    @PostMapping("/addressBook/insertAddressBook")
    @ApiOperation(value = "添加地址簿信息")
    public ResponseModel insertAddressBook(@RequestBody AddressBookQuery addressBookQuery){
        return null;
    }
BigDecimal比较大小
    @GetMapping("/withdrawMoney/drawing")
    @ApiOperation(value = "提币")
    public ResponseModel drawing(String number, String coinType){
        val map = new HashMap<String, Object>();
        //获取当前金额
        BigDecimal money = new BigDecimal(number);
        CoinType coinTypeResult = coinTypeService.getCoinType(coinType);
        int difference = money.compareTo(coinTypeResult.getOutMin());
        if(!ComUtil.isEmpty(coinTypeResult)){
            //difference=(-1(小于);0(等于);1(大于))
            if(difference==-1){
                map.put("result", "金额不能小于最小提币金额");
                map.put("status", false);
                return ResponseHelper.buildBadResponseModel(map);
            }else{
                //执行提币操作
            }
        }
        return null;
    }
java大小写转换函数
  • 在实际接口开发过程中,如果接口文档不明确的情况下,类型传参前段开发人员可能因为大小写原因后台解析错误,所以统一大写或小写
    @GetMapping("/userRechargeCoin/getBtcAddress")
    @ApiOperation(value = "获取比特币地址")
    public ResponseModel getBtcAddress(String accountType) throws IOException {
        //accountType.toUpperCase()转大写
        //accountType.toLowerCase()
        val map = bitcoinAddressService.getBtcAddress(accountType.toUpperCase());
        return ResponseHelper.buildResponseModel(map);
    }
累加清空
  • 在实际开发中,做累加操作的值放入list列表,为保证数据的有效性每次循环累加后需要吧,累加值清空,重新累加下一个对象的累加值,这个坑算是自己不严谨导致的某些问题,这边做一下记录。
for(UserBalance userBalanceT2 : userBalanceList){
    if(!ComUtil.isEmpty(userBalanceT2)){
        UserBalance userBalanceRs = new UserBalance();
        userBalanceRs.setAmountT2(new BigDecimal(0));
        userBalanceRs.setId(userBalanceT2.getId());
        userBalanceService.updateUserBalance(userBalanceRs);
        balanceChangeList = balanceChangeService.findBalanceChangeByBalanceId(userBalanceT2.getId());
        if(!ComUtil.isEmpty(balanceChangeList)){
            for(BalanceChange balanceChange : balanceChangeList){
                if(balanceChange.getTypeId() == desposit && balanceChange.getStatus() == 3){
                    number = balanceChange.getAmount();
                }else if(balanceChange.getTypeId() == extract && balanceChange.getStatus() == 3){
                    number = balanceChange.getAmount().negate();
                }else if(balanceChange.getTypeId() == exchangeIn){
                    number = balanceChange.getAmount();
                }else if(balanceChange.getTypeId() == exchangeOut){
                    number = balanceChange.getAmount().negate();
                }
                total = total.add(number);
            }
            int i = total.compareTo(new BigDecimal(0));
            if(i<0 || i==0){
                total = new BigDecimal(0);
            }
            AmountTotal amountTotal = new AmountTotal();
            amountTotal.setBalanceId(userBalanceT2.getId());
            amountTotal.setTotal(total);
            amountTotalList.add(amountTotal);
            total = new BigDecimal(0); //主要是这两句
            number = new BigDecimal(0); //主要是这两句
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值