• 数据库: 商城案例


    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);
            }
    
        }
    }
  • 相关阅读:
    slot 的简单使用(一)匿名插槽
    修改Tooltip 文字提示 的背景色 箭头颜色
    解决vue/cli3.0 语法验证规则 ESLint: Expected indentation of 2 spaces but found 4. (indent)
    洛谷P2014 选课(树形DP+分组背包)
    洛谷P4316 绿豆蛙的归宿(概率DP/期望DP+拓扑排序)
    Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)
    Atcoder ABC144 Gluttony(贪心+二分)
    洛谷P1352 没有上司的舞会(树形DP+记忆化)
    HDU2476 String painter(区间DP)
    POJ1651 Multiplication Puzzle(区间DP+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/15070813.html
Copyright © 2020-2023  润新知