zoukankan      html  css  js  c++  java
  • 数据库: 商城案例

    ProductDao

    • 需求1: 根据商品ID 获取商品名称 ,商品价格 以及商品所属分类的名称

      • 参数 pid, 返回值 product对象

    • 需求2: 根据分类ID 获取商品分类信息

      • 参数 cid , 返回值 category对象

    • 需求3: 查询指定分类ID 下的商品个数

      • 参数 cid , 返回值 int类型 商品个数

    • 需求4: 查询指定分类ID 下的所有商品信息

      • 参数分类ID ,返回值 List集合 集合中保存商品对象

    编写 ProductDao

    public class ProductDao {
    
        //1.根据商品ID 获取商品名称 ,商品价格 以及商品所属分类的名称
        public Product findProductById(String pid) throws SQLException {
    
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    
            String sql = "select * from product where pid = ?";
    
            Product product = qr.query(sql, new BeanHandler<Product>(Product.class), pid);
    
            //调用 findCategoryById()方法, 传递外键cid 获取商品对应 的分类信息
            Category category = findCategoryById(product.getCid());
    
            //将category保存到商品对象中
            product.setCategory(category);
    
            return product;
        }
    
        //2.根据分类ID 获取商品分类信息
        public Category findCategoryById(String cid) throws SQLException {
    
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    
            String sql = "select * from category where cid = ?";
    
            Category category = qr.query(sql, new BeanHandler<Category>(Category.class),cid);
    
            return category;
        }
    
        //3.查询指定分类ID 下的商品个数
        public int getCount(String cid) throws SQLException {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    
            String sql = "select count(*) from product where cid = ?";
    
            //获取的单列数据 ,使用ScalarHandler 封装
            Long count = (Long)qr.query(sql,new ScalarHandler<>(),cid);
    
            //将Lang类型转换为 int 类型,并返回
            return count.intValue();
        }
    
    
        //4.查询指定分类下的所有商品信息
        public List<Product> findProductByCid(String cid) throws SQLException {
            
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            
            String sql = "select * from product where cid = ?";
    
            //查询结果是一个List集合, 使用BeanListHandler 封装结果集
            List<Product> list = qr.query(sql, new BeanListHandler<Product>(Product.class), cid);
            
            return list;
        }
    }

    测试 ProductDao

    public class TestProductDao {
    
        ProductDao productDao = new ProductDao();
    
        //1.测试 根据商品ID 获取商品名称 ,商品价格 以及商品所属分类的名称
        @Test
        public void testFindProductById() throws SQLException {
    
            Product product = productDao.findProductById("1");
    
            System.out.println("商品名称: "+product.getPname()+ ", 商品价格: " + product.getPrice() +
                    ", 商品所属分类: "+ product.getCategory().getCname());
        }
    
    
        //2.测试 查询指定分类ID下的商品数
        @Test
        public void testGetCount() throws SQLException {
            //查询 cid为3的分类下有多少个商品
            int count = productDao.getCount("3");
            System.out.println("分类ID为3的分类下商品个数: " + count);
        }
    
        //3.测试 查询指定分类下的所有商品信息
        @Test
        public void testFindProductByCid() throws SQLException {
    
            //查询cid为 2的分类下 所有的商品信息
            List<Product> list = productDao.findProductByCid("2");
    
            for (Product product : list) {
                System.out.println(product);
            }
    
        }
    }
  • 相关阅读:
    Kafka 生产者 自定义分区策略
    同步互斥
    poj 1562 Oil Deposits(dfs)
    poj 2386 Lake Counting(dfs)
    poj 1915 KnightMoves(bfs)
    poj 1664 放苹果(dfs)
    poj 1543 Perfect Cubes (暴搜)
    poj 1166 The Clocks (暴搜)
    poj 3126 Prime Path(bfs)
    处理机调度
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/15070813.html
Copyright © 2011-2022 走看看