zoukankan      html  css  js  c++  java
  • 一个小白写易买网的自诉

     专业的易买网的后台商品管理模块讲解

    数据库表:SQL Server

     
    相关联的表有:
    1.商品信息表  EASYBUY_PRODUCT
    2.商品分类表  EASYBUY_PRODUCT_CATEGORY
     
    项目架构图:
     
    dao编写接口,书写实现功能的方法
    public interface ProductDao_hb {
        
        //获取所有的商品信息
        public List<Product> getAllInfo() throws Exception;
        //获取当前页的商品信息
        public List<Product> getOneProductData(int pageIndex,int pageSize) throws Exception;
        //总记录数
        public int getAllCount() throws Exception;
        //新增商品
        public boolean AddProduct(Product p) throws Exception;
        //修改商品信息
        public boolean UpProduct(Product p) throws Exception;
        //删除商品
        public boolean DelProduct(int id) throws Exception;
    }

    impl实现dao接口并且重写所有dao方法

    这里一般是执行SQL语句

    public class ProductDaoImpl_hb extends BaseDao implements ProductDao_hb{
         //查询ID,照片,商品名称
        public List<Product> getAllInfo() throws Exception {
             List<Product> list=new ArrayList<Product>();
             String sql="select EP_ID,EP_FILE_NAME,EP_NAME from EASYBUY_PRODUCT";
             ResultSet rs=executeSelect(sql);
            if (rs!=null) {
                while (rs.next()) {
                    //实体类
                     Product pros=new Product();
                    pros.setId(rs.getInt("EP_ID"));
                    pros.setFileName(rs.getString("EP_FILE_NAME"));
                    pros.setName(rs.getString("EP_NAME"));
                    list.add(pros);
                }
                
            }
            return list;
        }
        /**
         * 分页显示数据
         * pageindex:显示有几页
         * pagesize:显示几条数据
         * @throws Exception 
         */
        public List<Product> getOneProductData( int pageIndex,  int pageSize) throws Exception {
             List<Product> list=new ArrayList<Product>();
             String sql="select top "+pageSize+" * from EASYBUY_PRODUCT where EP_ID not in (select top "+(pageIndex-1)*pageSize+" EP_ID  from EASYBUY_PRODUCT)";
             ResultSet rs=executeSelect(sql);
            if(rs!=null){
                while(rs.next()){
                     Product pro=new Product();
                    pro.setId(rs.getInt("EP_ID"));
                    pro.setFileName(rs.getString("EP_FILE_NAME"));
                    pro.setName(rs.getString("EP_NAME"));
                    
                    list.add(pro);
                }
                
            }
            return list;
        }
        //查询总记录数
        public int getAllCount() throws Exception {
            int result=0;
             String sql="select count(1) as num from EASYBUY_PRODUCT";
             ResultSet rs=executeSelect(sql);
            if(rs!=null){
                if (rs.next()) {
                    result=rs.getInt("num");
                }    
            }
            return result;
        }
        //添加商品信息
        public boolean AddProduct( Product p) throws Exception {
            boolean flag=false;
             String sql="select EPC_PARENT_ID from dbo.EASYBUY_PRODUCT_CATEGORY where EPC_ID="+p.getChildCategoryId()+"";
             ResultSet rs=this.executeSelect(sql);
            if (rs!=null) {
                while (rs.next()) {
                    p.setCategoryId(rs.getInt("EPC_PARENT_ID"));
                    flag=true;
                }
            }
            //添加列的名称
            //(EP_ID, EP_NAME, EP_DESCRIPTION, EP_PRICE, EP_STOCK, EPC_ID, EPC_CHILD_ID, EP_FILE_NAME)
             String Addsql="insert into EASYBUY_PRODUCT values(?,?,?,?,?,?,?)";
             Object[] objs={
                    p.getName(),
                    p.getDescription(),
                    p.getPrice(),
                    p.getStock(),
                    p.getCategoryId(),
                    p.getChildCategoryId(),
                    p.getFileName(),
            };
             flag =this.executeUpdate(Addsql, objs);
            return flag;
        }
        //修改商品信息
        public boolean UpProduct( Product p) throws Exception {
            boolean flag=false;
            String sql="update EASYBUY_PRODUCT set EP_NAME=?,EP_DESCRIPTION=?,EPC_ID=?,EPC_CHILD_ID=?,EP_PRICE=?,EP_STOCK=?,EP_FILE_NAME=? where EP_ID=?";
            Object[] paras={
                    p.getName(),
                    p.getDescription(),
                    p.getCategoryId(),
                    p.getChildCategoryId(),
                    p.getPrice(),
                    p.getStock(),
                    p.getFileName(),
                    p.getId()
            };
            flag=executeUpdate(sql, paras);
            return flag;
        }
        //不修改图片的方法
        //修改商品信息
            public boolean UpPic( Product p) throws Exception {
                boolean flag=false;
                String sql="update EASYBUY_PRODUCT set EP_NAME=?,EP_DESCRIPTION=?,EPC_ID=?,EPC_CHILD_ID=?,EP_PRICE=?,EP_STOCK=? where EP_ID=?";
                Object[] paras={
                        p.getName(),
                        p.getDescription(),
                        p.getCategoryId(),
                        p.getChildCategoryId(),
                        p.getPrice(),
                        p.getStock(),
                        p.getId()
                };
                flag=executeUpdate(sql, paras);
                return flag;
            }
        //删除商品信息
        public boolean DelProduct( int id) throws Exception {
            
             String sql ="delete from EASYBUY_PRODUCT where EP_ID=?";
            Object[] prams={id};
            return executeUpdate(sql, prams);
        }    
        //通过id查询出商品信息并保存到list里面
         public List<Product> updateInfo(int id) throws Exception{
             List<Product> list=new ArrayList<Product>();
             String sql="select * from EASYBUY_PRODUCT where EP_ID=?";
             Object[] paras={id};
             ResultSet rs=executeSelect(sql,paras);
            if(rs!=null){
                while(rs.next()){
                     Product pro=new Product();
                    pro.setId(rs.getInt("EP_ID"));
                    pro.setFileName(rs.getString("EP_FILE_NAME"));
                    pro.setName(rs.getString("EP_NAME"));
                    pro.setDescription(rs.getString("EP_DESCRIPTION"));
                    pro.setPrice(rs.getFloat("EP_PRICE"));
                    pro.setStock(rs.getInt("EP_STOCK"));
                    pro.setCategoryId(rs.getInt("EPC_ID"));
                    pro.setChildCategoryId(rs.getInt("EPC_CHILD_ID"));
                    list.add(pro);
                }
                
            }
            return list;
         }
    }

    在servlet用来:请求对象的信息,处理请求,访问其他资源,获得需要的信息

    public class Servlet extends HttpServlet {
        
        public void doGet( HttpServletRequest request,  HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
        
        ProductDaoImpl_hb pdimpl=new ProductDaoImpl_hb();
        ProoductCategoryDaoImpl_hyj pcdimpl=new ProoductCategoryDaoImpl_hyj();
        
        public void doPost( HttpServletRequest request,  HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            String opr=request.getParameter("opr");
            
            if(opr!=null){
        
                if(opr.equals("addProduct")){
                    //----------加载下拉框中的信息
                    String name=request.getParameter("name");
                    
                    try {
                        //1获取一级分类
                        List<ProductCategory> listOnes=pcdimpl.getAllOneLeveInfo(3);
                        List<ProductCategory> listOne=pcdimpl.getAllOneLeveInfo(0);
                        //2.获取二级分类
                        List<ProductCategory> listTwo=pcdimpl.getAllTowLeveInfo(0); 
                        for ( ProductCategory item : listOne) {
                            System.out.println(item.getName());
                        }
                        //3.设置到作用域中
                        request.getSession().setAttribute("listOne",listOne);
                        request.getSession().setAttribute("listTwo",listTwo);
                        request.getSession().setAttribute("listOnes",listOnes);
                        //4.跳转到添加页面 进行判断跳转页面
                        if (name.equals("in")) { //修改页面
                                String id=request.getParameter("nid");
                                int nid=0;
                                if (id!=null&&!(id.equals(""))) {
                                    nid=Integer.parseInt(id);
                                }
                                 try {
                                    List<Product> list=pdimpl.updateInfo(nid);
                                    request.setAttribute("cid",list.get(0).getChildCategoryId());
                                    System.out.println(list.get(0).getChildCategoryId());
                                    request.setAttribute("list",list);
                                    request.getRequestDispatcher("/manage/product-modify.jsp").forward(request, response);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }    
                        }else{//新增页面
                            request.getRequestDispatcher("/manage/product-add.jsp").forward(request, response);
                        }
                        } catch ( Exception e) {
                            e.getMessage();
                        }
                }
                if(opr.equals("addNewProduct")){
                //添加商品
                try {
                     Product p=AddProduct(request,response);
                     boolean flag=pdimpl.AddProduct(p);
                    if (flag) {
                        request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response);
                    }
                } catch ( Exception e) {
                    e.printStackTrace();
                }
                
                }
                //删除
                if(opr.equals("delete")){
                String pid=request.getParameter("name");
                if(pid!=null){
                    int id =Integer.parseInt(pid);
                    try {
                        boolean flag=pdimpl.DelProduct(id);
                        if (flag) {
                            request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response);
                            
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            
                }
                //修改
                if (opr.equals("update")) {
                try {
                     Product p=new  Product();
                     boolean flag=false;
                      p= AddProduct(request,response);
                      p.setId(Integer.parseInt(request.getParameter("id")));
                      if (p.getFileName()==null) {
                          flag=pdimpl.UpPic(p);
                     }else{
                        flag=pdimpl.UpProduct(p);
                     }
                     if (flag) {
                        request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                }
                if(opr.equals("list")){
                 ProductDaoImpl_hb dao=new ProductDaoImpl_hb();
                //分页
                try {
                    //实例化page对象,给page的index和size赋值
                    //显示第一页
                     Page_hb page=new Page_hb();
                    //显示的数量
                     int pageSize=3;
                    page.setPageSize(pageSize);
                    int myindex=1;
                    //当前页
                     String pageIndex=request.getParameter("pageIndex");
                    if (pageIndex!=null&&(!pageIndex.equals(""))) {
                        myindex=Integer.parseInt(pageIndex);
                    
                    int mypageCount=0;
                    //总页数
                     int pageCount=dao.getAllCount();
                    if (pageCount%pageSize==0) {
                        mypageCount=pageCount/pageSize;
                    }else {
                        mypageCount=pageCount/pageSize+1;
                    }
                    page.setPageCount(mypageCount);
                    }
                    if (myindex<1) {
                        myindex=1;
                    }else if(myindex>page.getPageCount()){
                        myindex=page.getPageCount();
                    }
                    //当前页赋值
                    page.setPageIndex(myindex);
                     List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize());
                    page.setList(list);
                    request.setAttribute("listone", page);
                    request.getRequestDispatcher("/manage/product.jsp").forward(request, response);
                } catch ( Exception e) {
                    e.printStackTrace();
                }
            
                }
            }    
        }
        //添加商品方法
        public Product AddProduct( HttpServletRequest request,  HttpServletResponse response)
                throws Exception {
             Product p = new Product();
             request.setCharacterEncoding("utf-8");
            //文件上传
            //上传文件名
            String uploadFileName="";
            //表单字段元素
            String fieldName="";
            //请求信息中的额内容是否是multipart类型
             boolean isMultipart=ServletFileUpload.isMultipartContent(request);
            //上传文件存储路径(服务器文件系统上的绝对路径)
             String uploadFilePath=request.getSession().getServletContext().getRealPath("/images/product/");
            if (isMultipart) {
                request.setCharacterEncoding("utf-8");
                 FileItemFactory factory=new DiskFileItemFactory();
                //解析器
                 ServletFileUpload upload=new ServletFileUpload(factory);
                try {
                    //解析from表单中所有文件
                     List<FileItem> items = upload.parseRequest(request);
                     Iterator<FileItem> iter =items.iterator();
                    while (iter.hasNext()) {
                        //依次处理每个文件
                         FileItem item=(FileItem) iter.next();
                        //普通表单字段
                        if (item.isFormField()) {
                            fieldName= item.getFieldName();        //表单字段的name属性
                             if(fieldName.equals("productName")){
                                 //输出表单阻断的值
                                 //System.out.println(item.getString("utf-8"));    
                                 p.setName(item.getString("utf-8"));
                             }else if(fieldName.equals("productDetail")){
                                 //System.out.println(item.getString("utf-8"));
                                 p.setDescription(item.getString("utf-8"));
                             }else if(fieldName.equals("parentId")){
                                 //System.out.println(item.getString("utf-8"));
                                 p.setChildCategoryId(Integer.parseInt(item.getString("utf-8")));
                             }else if(fieldName.equals("productPrice")){
                                 //System.out.println(item.getString("utf-8")); 
                                 p.setPrice(Float.valueOf(item.getString("utf-8")));
                             }else if(fieldName.equals("productNumber")){
                                 p.setStock(Integer.parseInt(item.getString("utf-8")));
                             }
                            }else{        //文件表单字段
                                 String fileName=item.getName();      //文件名字
                                if(fileName!=null && !fileName.equals("")){
                                    File fullFile=new File(item.getName()); 
                                    File saveFile = new File(uploadFilePath,fullFile.getName());
                                    item.write(saveFile);
                                    uploadFileName=fullFile.getName();        //上传文件名
                                    p.setFileName(uploadFileName);
                            }
                            
                        }
                            
                    }
                    
                } catch ( Exception e) {
                    e.printStackTrace();
                }
            }
            return p;
        }
    
    }

    这里我讲解一下分页吧

                //分页
                try {
                    //实例化page对象,给page的index和size赋值
                    //显示第一页
                     Page_hb page=new Page_hb();
                    //显示的数量
                     int pageSize=3;
                    page.setPageSize(pageSize);
                    int myindex=1;
                    //当前页
                     String pageIndex=request.getParameter("pageIndex");
                    if (pageIndex!=null&&(!pageIndex.equals(""))) {
                        myindex=Integer.parseInt(pageIndex);
                    
                    int mypageCount=0;
                    //总页数
                     int pageCount=dao.getAllCount();
                    if (pageCount%pageSize==0) {
                        mypageCount=pageCount/pageSize;
                    }else {
                        mypageCount=pageCount/pageSize+1;
                    }
                    page.setPageCount(mypageCount);
                    }
                    if (myindex<1) {
                        myindex=1;
                    }else if(myindex>page.getPageCount()){
                        myindex=page.getPageCount();
                    }
                    //当前页赋值
                    page.setPageIndex(myindex);
                     List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize());
                    page.setList(list);
                    request.setAttribute("listone", page);
                    request.getRequestDispatcher("/manage/product.jsp").forward(request, response);
                } catch ( Exception e) {
                    e.printStackTrace();
                }    

    这是分页的代码,众所周知当你在网上happy(搜索信息)的时候,当前的页面没有你需要信息你会点击下一页来继续寻找信息,我所书写的功能和这个类似吧

    工具类:

    /**
     *提供分页使用的工具类
     * @author HYJ
     *
     */
    public class Page_hb {
        private int pageIndex=1;//当前显示第几页数据
        private int pageSize=3;//每页显示几条数据
        private int pageSum;//总记录数
        private int pageCount;//总页数
        private List<Product> list;//保存商品分页信息的集合
        public Page_hb() {
            super();
            // TODO Auto-generated constructor stub
        }
        public Page_hb(int pageIndex, int pageSize, int pageSum, int pageCount,
                List<Product> list) {
            super();
            this.pageIndex = pageIndex;
            this.pageSize = pageSize;
            this.pageSum = pageSum;
            this.pageCount = pageCount;
            this.list = list;
        }
        public int getPageIndex() {
            return pageIndex;
        }
        public void setPageIndex(int pageIndex) {
            //pageindex是外界调用这个方法传过来的值,当pageIndex的值大于总页数的,就让他显示最后一页
            if(pageIndex>pageCount){
                pageIndex=pageCount;//把pageIndex的值改为总页数显示最后一页
            }
            else if(pageIndex<0){//如果传过来的值小于0,就让pageIndex的值为1,显示第一页的数据
                pageIndex=1;
            }
            else{
                this.pageIndex = pageIndex;
                
            }
        }
        public int getPageSize() {
            return pageSize;
        }
        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }
        public int getPageSum() {
            return pageSum;
        }
        public void setPageSum(int pageSum) {
            this.pageSum = pageSum;
        }
        public int getPageCount() {
            return pageCount;
        }
        public void setPageCount(int pageCount) {
            this.pageCount = pageCount;
        }
        public List<Product> getList() {
            return list;
        }
        public void setList(List<Product> list) {
            this.list = list;
        }
        
    }

    根据工具类:设置设置显示的数量

    //实例化page对象,给page的index和size赋值
    //显示第一页
     Page_hb page=new Page_hb();
    //显示的数量
    int pageSize=3;
    page.setPageSize(pageSize);
    

    设置当前页,和根据从数据库中查询的count值除每页显示的数量=总页数

    并且判断当前页的值和1去比较如果小于1那么就显示第一页的数据

    没有理由别问我

                    int myindex=1;
                    //当前页
                     String pageIndex=request.getParameter("pageIndex");
                    if (pageIndex!=null&&(!pageIndex.equals(""))) {
                        myindex=Integer.parseInt(pageIndex);
                    
                    int mypageCount=0;
                    //总页数
                     int pageCount=dao.getAllCount();
                    if (pageCount%pageSize==0) {
                        mypageCount=pageCount/pageSize;
                    }else {
                        mypageCount=pageCount/pageSize+1;
                    }
                    page.setPageCount(mypageCount);
                    }
                    if (myindex<1) {
                        myindex=1;
                    }else if(myindex>page.getPageCount()){
                        myindex=page.getPageCount();
                    }

    把当前页的值赋给页面

                    //当前页赋值
                    page.setPageIndex(myindex);
                     List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize());
                    page.setList(list);
                    request.setAttribute("listone", page);
                    request.getRequestDispatcher("/manage/product.jsp").forward(request, response);

    写这个项目的大概流程就是这些了

    可能写的不是太好,请大家多多理解

    谢谢欣赏

    public interface ProductDao_hb {
        
        //获取所有的商品信息
        public List<Product> getAllInfo() throws Exception;
        //获取当前页的商品信息
        public List<Product> getOneProductData(int pageIndex,int pageSize) throws Exception;
        //总记录数
        public int getAllCount() throws Exception;
        //新增商品
        public boolean AddProduct(Product p) throws Exception;
        //修改商品信息
        public boolean UpProduct(Product p) throws Exception;
        //删除商品
        public boolean DelProduct(int id) throws Exception;
    }

  • 相关阅读:
    react.js+axios跨域
    O2O项目之一 环境搭配
    跟scss相关的两个包
    [nodemon] app crashed
    解决node.js链接数据库时出现的报错 --- client does not support authentication
    在Xshell 运行angular 项目时,找不到node-sass模块,安装node-sass模块时,又出现权限问题
    ajax请求数据时,get和post的区别
    web前端如何性能优化提高加载速度
    js数组去重
    前端跨域
  • 原文地址:https://www.cnblogs.com/shuaibin/p/5714185.html
Copyright © 2011-2022 走看看