3:Dao层和Service层

Dao层

Dao接口

public interface GoodsDao {
    /**
     * 查询所有的商品
     * @return
     * @throws SQLException
     */
    List<Goods> selectAllGoods() throws SQLException;

    /**
     * 根据id查询商品的详细信息
     * @param id
     * @return
     * @throws SQLException
     */
    Goods seelctGoodsById(Integer id) throws SQLException;
}

Dao接口实现类

public class GoodsDaoImpl implements GoodsDao {

    @Override
    public List<Goods> selectAllGoods() throws SQLException {
        List res = new ArrayList();
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_goods";
        final PreparedStatement ps = conn.prepareStatement(sql);
        final ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            final int id = rs.getInt("id");
            final String name = rs.getString("name");
            final double price1 = rs.getDouble("price1");
            final double price2 = rs.getDouble("price2");
            final int amount = rs.getInt("amount");
            final Goods goods = new Goods(id, name, price1, price2, amount);
            res.add(goods);
        }
        JdbcUtil.release(rs, ps, conn);

        return res;
    }

    @Override
    public Goods seelctGoodsById(Integer id) throws SQLException {
        Goods goods = null;
        Connection conn = JdbcUtil.getConnection();
        String sql = "select * from tb_goods where id = ?";
        final PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        final ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            final String name = rs.getString("name");
            final double price1 = rs.getDouble("price1");
            final double price2 = rs.getDouble("price2");
            final int amount = rs.getInt("amount");
            goods = new Goods(id, name, price1, price2, amount);
        }
        JdbcUtil.release(rs, ps, conn);
        return goods;
    }
}

测试代码:

class GoodsDaoImplTest {

    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Test
    void selectAllGoods() throws SQLException {
        final List<Goods> goodsList = goodsDao.selectAllGoods();
        goodsList.forEach(System.out::println);
    }

    @Test
    void seelctGoodsById() throws SQLException {
        final Goods goods = goodsDao.seelctGoodsById(1);
        System.out.println(goods);
    }
}

Service层

Service层接口

public interface GoodsService {
    /**
     * 查询出所有的商品
     * @return
     * @throws SQLException
     */
    List<Goods> listAllGoods() throws SQLException;

    /**
     * 查寻指定id的商品的详细信息
     * @param id
     * @return
     * @throws SQLException
     */
    Goods getGoodsById(Integer id) throws SQLException;
}

Service层实现类

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<Goods> listAllGoods() throws SQLException {
        final List<Goods> goodsList = goodsDao.selectAllGoods();
        return goodsList;
    }

    @Override
    public Goods getGoodsById(Integer id) throws SQLException {
        final Goods goods = goodsDao.seelctGoodsById(id);
        return goods;
    }
}

测试代码

class GoodsServiceImplTest {

    private GoodsService goodsService = new GoodsServiceImpl();

    @Test
    void listAllGoods() throws SQLException {
        final List<Goods> goodsList = goodsService.listAllGoods();
        goodsList.forEach(System.out::println);
    }
    @Test
    void getGoodsById() throws SQLException {
        final Goods goods = goodsService.getGoodsById(1);
        System.out.println(goods);
    }
}
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值