JavaWeb MVC設(shè)計(jì)模式

開發(fā)模式

1.C/S

客戶端/服務(wù)器,需要安裝客戶端軟件,稱為胖客戶端

2.B/S

瀏覽器/服務(wù)器,只要有瀏覽器即可,稱為瘦客戶端

MVC設(shè)計(jì)模式

即Model+View+Controller,在JavaWeb中分別對應(yīng):JavaBean/JSP/Servlet
注:
JavaBean:一個帶有無參構(gòu)造方法、屬性私有,并帶有公共的getter和setter方法的類

常用目錄分層

1.Bean:用于存放JavaBean對象
2.DAO:Data Access Object,定義數(shù)據(jù)訪問接口
3.DAO.IMPL:DAO接口的實(shí)現(xiàn)類
4.Service:定義業(yè)務(wù)邏輯操作接口
5.Service.IMPL:Service接口的實(shí)現(xiàn)類
6.Servlet:用于處理網(wǎng)絡(luò)業(yè)務(wù)
7.Util:用于存放數(shù)據(jù)庫等配置,配置信息一般從.properties文件中讀取

MVC示例

1.xxx.properties

主要存放一些數(shù)據(jù)庫的配置信息

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/database
username=xxx
password=xxx
2.Util

主要讀取資源文件和進(jìn)行數(shù)據(jù)庫參數(shù)配置,并向外提供連接和關(guān)閉數(shù)據(jù)庫方法

public class DBUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        ResourceBundle rb = ResourceBundle.getBundle("xxx");  //讀取資源文件,不用加后綴
        driver = rb.getString("driverClass");  //設(shè)置基本配置信息
        url = rb.getString("url");
        username = rb.getString("username");
        password = rb.getString("password");
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection open() {  //提供連接數(shù)據(jù)庫方法
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {  //提供關(guān)閉連接方法
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
            stmt = null;
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
            conn = null;
    }
}

3.Bean

主要定義一些JavaBean對象

public class User {
    private int id;
    private String name;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
4.DAO

定義一些數(shù)據(jù)操作的接口

public interface UserDao {
    public void addUser(User user);  //添加用戶接口
    public void selectUser(String name, String password);
}
5.DAO.IMPL

實(shí)現(xiàn)數(shù)據(jù)操作接口的類

public class UserDaoImpl implements UserDao {

    public void addUser(Stu stu) {
        Connection conn = null;
        PreparedStatement p = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.open();
            p = conn.prepareStatement("xxx");
            rs = p.executeQuery();
            ...
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, p, conn);
        }
    }

    public void selectUser(String name, String password) {
        sql操作...
    }
}
6.Service

定義具體的邏輯操作接口

public interface UserService {
    public void regist(String name, String password);  //注冊功能接口
    public void login(String name, String password);
}
7.Service.IMPL

實(shí)現(xiàn)邏輯操作接口類

public class UserServiceImpl implements UserService{
    UserDao userDao = new UserDaoImpl();

    public void regist(String name, String password) {  //具體注冊功能操作
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        userDao.addUser(user);
        ...
    }

    public void login(String name, String password) {
        userDao.selectUser(name, password);
        ...
    }
}
8.Servlet

實(shí)現(xiàn)一些網(wǎng)絡(luò)請求處理

public class UserServlet extends HttpServlet {  //處理注冊請求Servlet

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Stu stu = new Stu();
        String name = (String) request.getParameter("name");
        String password = (String) request.getParameter("password");
        UserServiceImpl usi = new UserServiceImpl();
        usi.regist(name, password);
        ...
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容