1.1 MVC 模式簡介
??MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫捺檬,一種軟件設計典范旧蛾,用一種業(yè)務邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼树碱,將業(yè)務邏輯聚集到一個部件里面交惯,在改進和個性化定制界面及用戶交互的同時次泽,不需要重新編寫業(yè)務邏輯。MVC被獨特的發(fā)展起來用于映射傳統(tǒng)的輸入席爽、處理和輸出功能在一個邏輯的圖形化用戶界面的結(jié)構(gòu)中意荤。
- JSP + Servlet + JavaBean
- v ---JSP 顯示數(shù)據(jù)
- C ---Servlet 獲取數(shù)據(jù),封裝數(shù)據(jù)只锻,傳輸數(shù)據(jù)(域?qū)ο螅┚料瘢刂屏鞒烫D(zhuǎn),處理業(yè)務邏輯
- M ---JavaBean 增刪改查
這里推薦一般書《大話設計模式》
這里說一下Java開發(fā)經(jīng)過的三個階段
- 開發(fā)3個階段:
- 只有Servlet 沒有jsp 痛苦
- 拋棄Servlet,只用jsp 在jsp里面 操作數(shù)據(jù)庫 操作xml 還要處理國際化 可讀性非常的差
- MVC+三層架構(gòu) 各司其職 jsp擅長頁面的輸出 Servlet擅長邏輯判斷 程序清晰
1.2 三層架構(gòu)與MVC
??這里要很認真的告訴你們MVC和三層架構(gòu)是兩個東西,一定要記住呀
摘自百度百科
??三層架構(gòu)(3-tier architecture) 通常意義上的三層架構(gòu)就是將整個業(yè)務應用劃分為:界面層(User Interface layer)齐饮、業(yè)務邏輯層(Business Logic Layer)捐寥、數(shù)據(jù)訪問層(Data access layer)。區(qū)分層次的目的即為了“高內(nèi)聚低耦合”的思想祖驱。在軟件體系架構(gòu)設計中握恳,分層式結(jié)構(gòu)是最常見,也是最重要的一種結(jié)構(gòu)捺僻。微軟推薦的分層式結(jié)構(gòu)一般分為三層乡洼,從下至上分別為:數(shù)據(jù)訪問層崇裁、業(yè)務邏輯層(又或稱為領域?qū)樱⒈硎緦印?/p>
-
表示層
- 客戶端(瀏覽器)
- Servlet:獲取數(shù)據(jù),封裝數(shù)據(jù)
- jsp: 顯示數(shù)據(jù)
-
業(yè)務邏輯層
- service:處理業(yè)務邏輯
-
數(shù)據(jù)訪問層
- dao:增刪改查
- db:數(shù)據(jù)庫
1.2.1 三層架構(gòu)的優(yōu)點
- 可以分工合作
- 每一個層依賴性比較低,可以實現(xiàn)解耦,分工明確,實現(xiàn)團隊協(xié)作開發(fā)
- 代碼的重用性很高
- Struts Hibernate Spring 3大框架 其實對應著這3層
- Struts 表示層框架也就是web層技術(shù)(處理亂碼,封裝數(shù)據(jù),處理業(yè)務邏輯,實現(xiàn)頁面跳轉(zhuǎn))
- Hibernate 數(shù)據(jù)持久化 對應著我們的dao 以前jdbc自己寫 完全面向?qū)ο?/li>
- Spring 業(yè)務邏輯層 核心技術(shù) IOC(依賴注入) AOP(面向切面編程) 核心思想 - 解耦 通過配置文件 在運行的時候把對象注入進去
1.3 案例
從數(shù)據(jù)庫取得所有數(shù)據(jù)數(shù)據(jù)放到網(wǎng)頁顯示
這個案例只是幫助大家的理解,不是束昵!不是拔稳!不是!項目開發(fā)用的妻怎。
1.3.1 思路分析
??小熊自己也是剛?cè)腴T不久壳炎,這里只是我粗淺的思路,有興趣的可以看看。
圖解
- 從整一個項目來說,貫穿項目的是數(shù)據(jù),數(shù)據(jù)的傳遞,所以為了方便數(shù)據(jù)的傳遞,需要對數(shù)據(jù)進行封裝,這里可以直接使用JavaBean進行封裝逼侦。項目里面,會需要一個JavaBean類匿辩。
- 然后從網(wǎng)頁開始,即表示層,在表示層我們會需要一個JSP來顯示獲取內(nèi)容,還需要一個Servlet獲取和封裝數(shù)據(jù)
- 在業(yè)務邏輯層需要一個Service來處理業(yè)務邏輯。
- 最后需要一個Dao來對數(shù)據(jù)庫曾刪改查榛丢。
- 會使用MySQL的相關(guān)知識铲球。
1.3.2 一起開始寫程序吧!
- 環(huán)境搭建
- 主要是 JDBC的環(huán)境搭建與配置
- 特別是C3p0的配置文件
- 完成Java的編寫
public class Student implements Serializable {
private Integer sid;
private String sname;
private String gender;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 這里我們先寫Servlet 為什么呢其實是我覺的servlet的負責了數(shù)據(jù)的接受與轉(zhuǎn)發(fā)
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取所有的學生數(shù)據(jù)
StudentService service = new StudentService();//這個是我們的Serivce,我們可以先寫著晰赞,然后在去后面補全這個Service 以及包含的方法
List<Student> list = null;
try {
list = service.findAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//保存在域?qū)ο螽斨? request.setAttribute("list", list);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
- Service的編寫
由于我們的Service里面什么邏輯都沒有只有專遞數(shù)據(jù)
public class StudentService {
public List<Student> findAll() throws SQLException {
StudentDao dao = new StudentDao();//Dao層的數(shù)據(jù)傳遞與方法我們后面補上
return dao.findAll();
}
}
- Dao的編寫
JDBC 從數(shù)據(jù)拿取數(shù)據(jù)
public class StudentDao {
public List<Student> findAll() throws SQLException {
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
return qr.query("select * from student", new BeanListHandler<Student>(Student.class));//List<JavaBean>的形式封裝
}
}
工具類
public class JDBCUtils {
// 創(chuàng)建一個連接池:但是這個連接池只需要創(chuàng)建一次即可稼病。
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 獲得連接的方法
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
/**
* 獲得數(shù)據(jù)源:
*/
public static DataSource getDataSource(){
return dataSource;
}
}
- jsp的頁面顯示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="500">
<tr>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
</tr>
<c:forEach items="${list}" var="stu">
<tr>
<td>${stu.sname}</td>
<td>${stu.gender}</td>
<td>${stu.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>