zoukankan      html  css  js  c++  java
  • JAVA学习笔记(十四)

    项目中常见的分包(分层)

      当程序规模小的时候,可以一个人全部完成;但当程序规模大的时候,一个人难以完成,这时候,要采用多人合作的方式来完成程序的开发。

      通过用户注册功能来讲解一下项目中常见的分包(分层)。如下图

    view层作用:视图层,控制项目中主界面显示的内容。

    controller层:控制层, 获取界面上的数据,为界面设置数据; 将要实现的功能交给业务层处理。

    service层:业务层, 功能的实现, 与controller控制层和数据访问层DAO交互, 将对数据库的操作交给DAO数据访问层来处理。

    dao层:数据访问层, 用来操作数据库表的数据。

    DB数据库:这里指的MySQL。

    domain实体包:存放JavaBean。

    tools工具包:存放项目中使用到的工具类。

    test测试包:存放项目功能测试的代码。

    下面是一个Store项目,用来练习项目的分包

    view层

    package com.oracle.view;
    
    import java.sql.PreparedStatement;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import com.oracle.controller.StoreController;
    import com.oracle.domain.Store;
    
    public class StoreView {
        StoreController storeController=new StoreController();
        Scanner sc=new Scanner(System.in);
                public void run() {
                    Scanner sc=new Scanner(System.in);
                    while(true) {
                        System.out.println("--------商城管理系统--------");
                        System.out.println("1.查询所有商品信息");
                        System.out.println("2.新增商品信息");
                        System.out.println("3.修改商品信息");
                        System.out.println("4.删除商品信息");
                        System.out.println("5.退出");
                        System.out.println("请输入您的选择");
                        int choose=sc.nextInt();
                        switch(choose) {
                        case 1:  find();
                            break;
                        case 2: add();
                            break;
                        case 3: update();
                            break;
                        case 4:del();
                            break;
                        case 5:
                            return;
                        default:
                            break;
                        }
                        }
                    }
                //查询
                public void find() {
                    
                    ArrayList<Store> list=storeController.find();
                    System.out.println("商品编号	商品名称	商品大小	商品价格	商品数量");
                    for(int i=0;i<list.size();i++) {
                        System.out.println(list.get(i).getSid()+"	"+list.get(i).getBrands()+"	"
                                +list.get(i).getSize()+"	"+list.get(i).getPrice()+"	"
                                +list.get(i).getCounts());
                    }
                }
                //添加
                public void add() {
                    Store store=new Store();
                    System.out.println("请输入您要添加的商品名称:");
                    String name=sc.next();
                    store.setBrands(name);
                    System.out.println("请输入您要添加的商品大小:");
                    int size=sc.nextInt();
                    store.setSize(size);
                    System.out.println("请输入您要添加的商品价格:");
                    Double price=sc.nextDouble();
                    store.setPrice(price);
                    System.out.println("请输入您要添加的商品数量:");
                    int counts=sc.nextInt();
                    store.setCounts(counts);
                    String mes=storeController.add(store);
                    System.out.println(mes);
                }
                public void update() {
                    Store store=new Store();
                    System.out.println("请选择您要修改的产品id");
                    int id=sc.nextInt();
                    System.out.println("请输入修改后的品牌名");
                    String name=sc.next();
                    System.out.println("请输入修改后的尺寸");
                    Double size=sc.nextDouble();
                    System.out.println("请输入修改后的价格");
                    Double price=sc.nextDouble();
                    System.out.println("请输入修改后的库存");
                    int counts=sc.nextInt();
                    store.setBrands(name);
                    store.setSize(size);
                    store.setPrice(price);
                    store.setCounts(counts);
                    store.setSid(id);
                    String mes=storeController.update(store);
                    System.out.println(mes);
                }
                public void del() {
                    Store store=new Store();
                    System.out.println("请输入要删除的商品id");
                    int id=sc.nextInt();
                    store.setSid(id);
                    String mes=storeController.del(store);
                    System.out.println(mes);
                }
        }

    tools工具包

    package com.oracle.tools;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    public class Dbutil {
            private Dbutil() {}
            public static Connection getConn() {
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    String username="root";
                    String password="123456";
                    String url="jdbc:mysql://localhost:3306/demo_jdbc";
                    Connection conn=DriverManager.getConnection(url,username,password);
                    return conn;
                    }catch(Exception ex){
                        throw new RuntimeException(ex+"数据库连接失败");
                    }
            }
            public static void close(Statement sta,Connection conn) {
                if(sta!=null) {
                    try {
                        sta.close();
                    }catch(SQLException ex) {}
                }
                if(conn!=null) {
                    try {
                        conn.close();
                    }catch(SQLException ex) {}
                }
            }
            public static void close(ResultSet rs,Statement sta,Connection conn) {
                if(rs!=null) {
                    try {
                        rs.close();            
                        }catch(SQLException ex) {}
                }
                close(sta,conn);
            }
            
    }

    service层

    package com.oracle.service;
    
    import java.util.ArrayList;
    
    import com.oracle.dao.StoreDao;
    import com.oracle.domain.Store;
    
    public class StoreService {
                private StoreDao storeDao=new StoreDao();
                //查询
                public ArrayList<Store> find(){
                    return storeDao.find();
                }
                //添加
                public int add(Store store) {
                    return storeDao.add(store);
                }
                //修改
                public int update(Store store) {
                    return storeDao.update(store);
                }
                public int del(    Store store) {
                    return storeDao.del(store);
                }
    }

    controller层

    package com.oracle.controller;
    
    import java.util.ArrayList;
    
    import com.oracle.domain.Store;
    import com.oracle.service.StoreService;
    
    public class StoreController {
            private StoreService storeService=new StoreService();
            public ArrayList<Store> find(){
                return storeService.find();
            }
            public String add(Store store) {
                int row=storeService.add(store);
                String mes=null;
                if(row!=0) {
                    mes="添加商品成功";
                }else {
                    mes="添加商品失败";
                }
                return mes;
            }
            public String update(Store store) {
                int row=storeService.update(store);
                String mes=null;
                if(row!=0) {
                    mes="修改商品成功";
                }else {
                    mes="修改商品失败";
                }
                return mes;
            }
            public String del(Store store) {
                int row=storeService.del(store);
                String mes=null;
                if(row!=0) {
                    mes="删除商品成功";
                }else {
                    mes="删除商品失败";
                }
                return mes;
            }
    }

    Dao层(Data Access Object)数据访问对象

    package com.oracle.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    
    import com.oracle.domain.Store;
    import com.oracle.tools.Dbutil;
    
    public class StoreDao {
        //查询
            public ArrayList<Store> find() {
                try {
                    Connection conn=Dbutil.getConn();
                    Statement sta=conn.createStatement();
                    String sql="select * from store";
                    ResultSet rs=sta.executeQuery(sql);
                    ArrayList<Store> list=new ArrayList<Store>();
                    while(rs.next()) {
                        Store s=new Store();
                        s.setSid(rs.getInt("sid"));
                        s.setBrands(rs.getString("brands"));
                        s.setSize(rs.getDouble("size"));
                        s.setPrice(rs.getDouble("price"));
                        s.setCounts(rs.getInt("counts"));
                        list.add(s);
                    }
                    Dbutil.close(rs, sta, conn);
                    return list;
                }catch(SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException("商品查询失败");
            }
            }
            public int add(Store store) {
                try {
                    Connection conn=Dbutil.getConn();
                    String sql="insert into store(brands,size,price,counts) values(?,?,?,?)";
                    PreparedStatement pst=conn.prepareStatement(sql);
                    pst.setString(1, store.getBrands());
                    pst.setDouble(2, store.getSize());
                    pst.setDouble(3, store.getPrice());
                    pst.setInt(4, store.getCounts());
                    int row=pst.executeUpdate();
                    Dbutil.close(pst, conn);
                    return row;
                }catch(SQLException ex) {
                    System.out.println(ex);
                    throw new RuntimeException("商品添加失败");
                }
            }
            public int update(Store store) {
                try {
                    Connection conn=Dbutil.getConn();
                    String sql="update store set brands=?,size=?,price=?,counts=? where sid=?";
                    PreparedStatement pst=conn.prepareStatement(sql);
                    pst.setString(1, store.getBrands());
                    pst.setDouble(2, store.getSize());
                    pst.setDouble(3, store.getPrice());
                    pst.setInt(4, store.getCounts());
                    pst.setInt(5, store.getSid());
                    int row=pst.executeUpdate();
                    Dbutil.close(pst, conn);
                    return row;
                }catch(SQLException ex){
                    System.out.println(ex);
                    throw new RuntimeException("商品修改失败");
                }
            }
            public int del(Store store) {
                try {
                    Connection conn=Dbutil.getConn();
                    String sql="delete from store where sid=?";
                    PreparedStatement pst=conn.prepareStatement(sql);
                    pst.setInt(1, store.getSid());
                    int d=pst.executeUpdate();
                    Dbutil.close(pst, conn);
                    return d;
                }catch(SQLException ex) {
                    System.out.println(ex);
                    throw new RuntimeException("商品删除失败");
                }
            }
    }

    domain层

    package com.oracle.domain;
    
    public class Store {
        private int sid;
        private String brands;
        private double size;
        private double price;
        private int counts;
        public Store() {
            
        }
        public Store(int sid,String brands,double size,double price,int counts ) {
            this.sid=sid;
            this.brands=brands;
            this.size=size;
            this.price=price;
            this.counts=counts;
        }
        public int getSid() {
            return sid;
        }
        public void setSid(int sid) {
            this.sid = sid;
        }
        public String getBrands() {
            return brands;
        }
        public void setBrands(String brands) {
            this.brands = brands;
        }
        public double getSize() {
            return size;
        }
        public void setSize(double size) {
            this.size = size;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public int getCounts() {
            return counts;
        }
        public void setCounts(int counts) {
            this.counts = counts;
        }
    }

    测试类

    package com.oracle.test;
    
    import com.oracle.view.StoreView;
    
    public class StoreApp {
    
        public static void main(String[] args) {
            new StoreView().run();
        }
    
    }
  • 相关阅读:
    微信二维码 场景二维码 用于推送事件,关注等 注册用户 ,经过测试
    简单的 Helper 封装 -- CookieHelper
    简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
    Java反射机制
    Windows Azure Web Site (13) Azure Web Site备份
    Windows Azure Virtual Machine (1) IaaS用户手册
    Windows Azure Web Site (1) 用户手册
    Windows Azure Web Site (12) Azure Web Site配置文件
    Windows Azure Web Site (11) 使用源代码管理器管理Azure Web Site
    Windows Azure Web Site (10) Web Site测试环境
  • 原文地址:https://www.cnblogs.com/boringLee/p/8882778.html
Copyright © 2011-2022 走看看