馬哥資深網(wǎng)絡安全工程師
?orElseGet
Optional<Wallet> findByAccountId(Integer accountId);//為null時,創(chuàng)建一下新的Wallet wallet = repository.findByAccountId(accountId).orElseGet(() -> {
? ? ? ? ? Wallet newWallet = new Wallet();
? ? ? ? ? Account account = new Account();
? ? ? ? ? account.setId(accountId);
? ? ? ? ? newWallet.setMoney(0D);
? ? ? ? ? newWallet.setAccount(account);
? ? ? ? ? repository.save(newWallet);
? ? ? ? ? return newWallet;
? ? ? });
標簽: