关于增删改查方法中 public后的 返回值类型

来源:互联网 发布:免费瓷砖设计软件 编辑:程序博客网 时间:2024/05/16 07:14

如一下代码中  public List<Product> findProduct(int start, int pageSize)  。。。{   ...return  XX;}    XX的类型一定要和前面的返回值类型一致。

public List<Product> findProduct(int start, int pageSize)
            throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product limit ?,?";
        return qr.query(sql, new BeanListHandler<Product>(Product.class),
                start, pageSize);
    }
    // 总数
    public int findTotalCount() throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from product";
        return ((Long) qr.query(sql, new ScalarHandler())).intValue();
    }
    // 保存
    public int addProduct(Product product) throws SQLException {
        String sql = "insert into Product values(?,?,?,?,?,?,?,?,?,?)";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        // 返回值 int代表影响的行数
        return runner.update(sql, product.getPid(), product.getPname(), product
                .getMarket_price(), product.getShop_price(), product
                .getPimage(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(product.getPdate()), product.getIs_hot(), product
                .getPdesc(), product.getPflag(), product.getCid());
    }

    // 删除数据库的记录
    public int deleteProduct(String pid) throws SQLException {
        String sql = "delete from product where pid=?";
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        return runner.update(sql, pid);
    }

    // 查询单个product对象
    public Product findProductByPid(String pid) throws SQLException {
        String sql = "select * from product where pid=?";
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        Product product = qr.query(sql,
                new BeanHandler<Product>(Product.class), pid);
        return product;
    }

    // 修改
    public int updateProduct(Product product) throws SQLException {
        {
            QueryRunner runner = new QueryRunner(
                    DataSourceUtils.getDataSource());
            if (product.getPimage() == null) {
                String sql = "update product set pname=?,market_price=?,shop_price=?,"
                        + "is_hot=?,pdesc=?,pflag=?,cid=? where pid=?";
                return runner.update(sql, product.getPname(),
                        product.getMarket_price(), product.getShop_price(),
                        product.getIs_hot(), product.getPdesc(),
                        product.getPflag(), product.getCid(), product.getPid());
            } else {
                String sql = "update product set pname=?,market_price=?,shop_price=?,"
                        + "pimage=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=?";
                return runner.update(sql, product.getPname(),
                        product.getMarket_price(), product.getShop_price(),
                        product.getPimage(), product.getIs_hot(),
                        product.getPdesc(), product.getPflag(),
                        product.getCid(), product.getPid());
            }
        }
    }