mybatis表Map

roejwanj  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(149)

我有表帐户,我有表余额->然后我有一个方法,假设检查天气帐户有一个余额,我试图添加钱,但我的余额总是返回空,如何从这里管理,我是新的tomybatis,不知道如何创建这种关系。

@Insert("INSERT INTO BALANCE (accountId,currency,fundsToAdd) values (#{accountId},#{currency},#{fundsToAdd})")
void addFunds(Long accountId,String currency, BigDecimal fundsToAdd);

我调用的方法

private Balance addFunds(Account account, String currency, BigDecimal amount) throws Exception {
    Account locatedAccount = accountMapper.getAccount(account.getId())
    .orElseThrow(() -> new Exception("No account with ID " + account.getCustomerId() + " found"));
    Balance foundBalance = findBalance(locatedAccount, currency).orElseThrow(() -> new RuntimeException("Could not locate balance"));

    BigDecimal fundsAfterAdding = foundBalance.getAmount().add(amount);
    accountMapper.addFunds(locatedAccount.getId(), currency, fundsAfterAdding);
    return accountMapper.getBalance(account.getId(), currency).orElseThrow(() -> new RuntimeException("Could not get balance after adding funds"));

}

抛出空指针的方法,因此帐户余额为空

private Optional<Balance> findBalance(Account account, String currency) {
    return account.getBalances().stream()
            .filter(balance -> balance.getCurrency().equalsIgnoreCase(currency))
            .findFirst();
}

table

CREATE TABLE IF NOT EXISTS Account
(
    id         INTEGER NOT NULL GENERATED always as identity,
    customerId INTEGER,
    country    VARCHAR(22)

);

CREATE TABLE IF NOT EXISTS Balance
(
    balanceId INTEGER NOT NULL GENERATED always as identity,
    accountId int,
    currency  VARCHAR(200),
    amount    BIGINT
);

模型

public class Account {

@Id
private Long id;

@Column("country")
private String country;

@Column("customerId")
private Long customerId;

// i need to somehow be able to refer to account balances 
List<Balance> balances;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题