(七)剩余DAO代码

AccountDAO.java

package com.aff.bookstore.dao;
import com.aff.bookstore.domain.Account;
public interface AccountDAO {

    /**
     * 根据 accountId 获取对应的 Account 对象
     * @param accountId
     * @return
     */
    public abstract Account get(Integer accountId);

    /**
     * 根据传入的 accountId, amount 更新指定账户的余额: 扣除 amount 指定的钱数
     * @param accountId
     * @param amount
     */
    public abstract void updateBalance(Integer accountId, float amount);

}

 

AccountDAOImpl.java

package com.aff.bookstore.dao.impl;

import com.aff.bookstore.dao.AccountDAO;
import com.aff.bookstore.domain.Account;

public class AccountDAOImpl extends BaseDAO<Account> implements AccountDAO {

    @Override
    public Account get(Integer accountId) {
        String sql = "select accountId,balance from account where  accountId = ?";
        return query(sql, accountId);

    }

    @Override
    public void updateBalance(Integer accountId, float amount) {
        String sql = "update account  set balance = balance - ? where  accountId = ?";
        update(sql, amount, accountId);
    }

}

 

TradeDAO.java

package com.aff.bookstore.dao;
import java.util.Set;
import com.aff.bookstore.domain.Trade;
public interface TradeDAO { /** * 向数据表中插入 Trade 对象 * @param trade */ public abstract void insert(Trade trade); /** * 根据 userId 获取和其关联的 Trade 的集合 * @param userId * @return */ public abstract Set<Trade> getTradesWithUserId(Integer userId); }

 

TradeDAOImpl.java

package com.aff.bookstore.dao.impl;
import java.util.HashSet;
import java.util.Set;
import com.aff.bookstore.dao.TradeDAO;
import com.aff.bookstore.domain.Trade;

public class TradeDAOImpl extends BaseDAO<Trade> implements TradeDAO {

    @Override
    public void insert(Trade trade) {
        String sql = "insert into trade(userid,tradetime)  values(?,?)";
        update(sql, trade.getUserId(),trade.getTradeTime());
    }

    @Override
    public Set<Trade> getTradesWithUserId(Integer userId) {
        String sql = "select  tradeId,userId, tradeTime from trade where userId = ?";
        return new HashSet<>(queryForList(sql, userId));
    }

}

 

TradeItemDAO.java

package com.aff.bookstore.dao;
import java.util.Collection;
import java.util.Set;
import com.aff.bookstore.domain.TradeItem;

public interface TradeItemDAO {

    /**
     * 批量保存 TradeItem 对象
     * @param items
     */
    public abstract void batchSave(Collection<TradeItem> items);

    /**
     * 根据 tradeId 获取和其关联的 TradeItem 的集合
     * @param tradeId
     * @return
     */
    public abstract Set<TradeItem> getTradeItemsWithTradeId(Integer tradeId);

}

 

TradeItemDAOImpl.java

package com.aff.bookstore.dao.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.aff.bookstore.dao.TradeItemDAO;
import com.aff.bookstore.domain.TradeItem;

public class TradeItemDAOImpl extends BaseDAO<TradeItem> implements TradeItemDAO {

    @Override
    public void batchSave(Collection<TradeItem> items) {
        String sql = "insert into tradeitem(bookid, quantity,tradeid) values(?,?,?)";
        Object[][] params = new Object[items.size()][3];

        List<TradeItem> tradeItems = new ArrayList<TradeItem>(items);
        for (int i = 0; i < tradeItems.size(); i++) {
            params[i][0] = tradeItems.get(i).getBookId();
            params[i][1] = tradeItems.get(i).getQuantity();
            params[i][2] = tradeItems.get(i).getTradeId();

        }
        batch(sql, params);
    }

    @Override
    public Set<TradeItem> getTradeItemsWithTradeId(Integer tradeId) {
        String sql = "select  itemId tradeItemId, bookId,quantity,tradeId from  tradeitem where tradeid = ?";
        return new HashSet<>(queryForList(sql, tradeId));
    }

}

 

posted @ 2020-05-20 13:00  林淼零  阅读(204)  评论(0编辑  收藏  举报