manytone-mybatis框架

9bfwbjaz  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(92)

我的帐户在db中没有余额列表,它只有一个字段,所以我们的想法是,当调用controller get(createaccount)时,它也会创建一个单独的表,我在db中这样做,所以现在我尝试获取与account相关的所有余额,并且余额仍然返回null。如果你能帮忙我会很感激的

@Select("Select accountId,customerId from account where accountId=#{accountId}")
    @Results(value = { @Result(property = "accountId", column = "accountId"), @Result(property = "customerId", column = "customerId"),
            @Result(property = "balances", javaType = List.class, column = "accountId", many = @Many(select = "getBalances"))

    })
    Optional<Account> getAccount(@Param("id") Long id);

    @Select("select currency,balanceId,accountId,amount from balance where accountId=#{accountId}")
    List<Balance> getBalances(Long accountId);

public class Account {

@Id
private Long accountId;

@Column("country")
private String country;

@Column("customerId")
private Long customerId;

List<Balance> balances;

这里有一点棘手,我是我的巴蒂斯新手,所以试图找到我的方式来做我需要的事情,它的工作,除了当我试图获得帐户,应该显示一切,包括余额
我基本上是首先检查帐户是否存在,如果不存在,我创建帐户,然后我从请求中获取货币列表,并建立余额列表,包括初始金额,还用生成的帐户id填充它。

public Account create(AccountCreateRequest request) {
    Optional<Long> customerId = accountMapper.checkIfCustomerExist(request.getCustomerId());

    if (customerId.isEmpty()) {
        Account account = Account.builder().customerId(request.getCustomerId()).country(request.getCountry()).build();
        accountMapper.saveAccount(account);
        List<Balance> balances = request.getCurrencies().stream().map(currency -> Balance.builder()
                .currency(currency)
                .amount(BigDecimal.ZERO)
                .build()).map(b -> b.setAccountId(account.getAccountId())).collect(Collectors.toList());
        balances.forEach(accountMapper::saveBalance);
        account.setBalances(balances);
        return account;
    } else {
        throw new RuntimeException("Account already exist");
    }
}

暂无答案!

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

相关问题