DAO設(shè)計 :
DAO層主要是做數(shù)據(jù)持久層的工作,負(fù)責(zé)與數(shù)據(jù)庫進(jìn)行聯(lián)絡(luò)的一些任務(wù)都封裝在此,DAO層的設(shè)計首先是設(shè)計DAO的接口,然后在Spring的配置文件中定義此接口的實現(xiàn)類,然后就可在模塊中調(diào)用此接口來進(jìn)行數(shù)據(jù)業(yè)務(wù)的處理,而不用關(guān)心此接口的具體實現(xiàn)類是哪個類,顯得結(jié)構(gòu)非常清晰,DAO層的數(shù)據(jù)源配置,以及有關(guān)數(shù)據(jù)庫連接的參數(shù)都在Spring的配置文件中進(jìn)行配置。
在該層主要完成對象-關(guān)系映射的建立,通過這個映射,再通過訪問業(yè)務(wù)對象即可實現(xiàn)對數(shù)據(jù)庫的訪問,使得開發(fā)中不必再用SQL語句編寫復(fù)雜的數(shù)據(jù)庫訪問程序,這樣就簡化了對數(shù)據(jù)庫的訪問,提高了開發(fā)效率。同時通過對象-關(guān)系映射的配置,可以建立業(yè)務(wù)對象之間的復(fù)雜關(guān)系,如一對多、多對一、一對一、多對多等關(guān)系。這樣就不再需要在數(shù)據(jù)庫中建立表之間的復(fù)雜聯(lián)系,使得業(yè)務(wù)對象之間的關(guān)系和數(shù)據(jù)庫相分離,簡化了數(shù)據(jù)庫的建立和維護(hù)。在這一層中主要使用Hibernate框架來實現(xiàn)。
針對以上問題,產(chǎn)生了基于MVC模式Model層的DAO模式(Data Access Object),主要由工廠類(Factory)、代理類(Proxy)、實現(xiàn)類(DAOImpl)、DAO接口和值對象類(VO)以及數(shù)據(jù)庫連接類組成。
這里總結(jié)一下MVC+DAO的設(shè)計流程。通過MVC+DAO的設(shè)計模式,可以使項目在設(shè)計過程中結(jié)構(gòu)更為明晰,并且能夠方便的進(jìn)行修改。MVC是一個分層模型,即模型、視圖、控制器。DAO是一個數(shù)據(jù)庫訪問模型,隔離數(shù)據(jù)庫操作。
環(huán)境說明:
數(shù)據(jù)庫:mysql
開發(fā)語言:JSP + Servlet + Java
服務(wù)器:tomcat 7.x
包規(guī)劃:
entity 放置與數(shù)據(jù)庫中的表相對應(yīng)的實體類
dao 放置DAO設(shè)計模式下實現(xiàn)訪問數(shù)據(jù)庫的接口
dao.impl 放置DAO對應(yīng)的接口實現(xiàn)類
servlet 放置Servlet
util 工具包
設(shè)計流程綜述:
0.設(shè)計數(shù)據(jù)庫以及視圖頁面(VIEW)
1.設(shè)計數(shù)據(jù)庫的工具類
2.設(shè)計符合java bean標(biāo)準(zhǔn)的entity類 (MODEL)
3.設(shè)計訪問數(shù)據(jù)庫的DAO接口
4.設(shè)計實現(xiàn)DAO接口的實現(xiàn)類
5.創(chuàng)建Servlet響應(yīng)請求(CONTROLLER)
例子:以一個簡單的登錄頁面設(shè)計為例
0.設(shè)計數(shù)據(jù)庫以及視圖頁面
數(shù)據(jù)庫設(shè)計:
1
2
3
4
5
6
|
CREATE TABLE `NewTable` ( `id` int (10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT , ` name ` varchar (10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , ` password ` varchar (10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , PRIMARY KEY (`id`) ) |
頁面視圖:
index.jsp的核心代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< form action = "/iMath/servlet/AdminServlet?method=login" method = "post" > < table border = '1' align = "center" > < caption >USER LOGIN</ caption > < tr > < th >username</ th > < th >< input type = "text" name = "username" /></ th > </ tr > < tr > < th >password</ th > < th >< input type = "password" name = "password" /></ th > </ tr > < tr > < td colspan = "2" align = "center" >< input type = "submit" value = "submit" /></ td > </ tr > </ table > </ form > |
核心代碼就是一個form表單,用于提供視圖,為用戶提供輸入的接口。核心是指定action和method屬性。這應(yīng)該是最簡單的一步,下面的工作則進(jìn)入真正的代碼編寫階段。
轉(zhuǎn)發(fā)頁面:
message.jsp核心代碼如下:其實就句話
1
2
3
|
< body > ${message} </ body > |
1.設(shè)計數(shù)據(jù)庫的工具類
這步的操作應(yīng)該是大同小異的,目的只是抽取公共代碼,簡化程序流程。
dbConfig.properties 文件存放數(shù)據(jù)庫的配置文件,這么做的優(yōu)點的是可以項目編譯后也能方便的修改數(shù)據(jù)庫配置的相關(guān)信息。
1
2
3
4
|
driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/imath?useUnicode=true&characterEncoding=utf-8 user = root password =1234 |
DBUtil類設(shè)計:該類用實現(xiàn)建立數(shù)據(jù)庫連接和關(guān)閉數(shù)據(jù)庫連接的公共操作。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package cn.imath.util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public final class DBUtil { private static String driver; private static String url; private static String user; private static String password; /** * load the property file */ static { Properties props = new Properties(); //get the class loader InputStream is = DBUtil. class .getClassLoader().getResourceAsStream( "cn/imath/util/dbConfig.properties" ); try { props.load(is); } catch (Exception e) { e.printStackTrace(); } driver = props.getProperty( "driver" ); url = props.getProperty( "url" ); user = props.getProperty( "user" ); password = props.getProperty( "password" ); } /** * register the driver */ static { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * OPEN THE DATABASE CONNECTION * @return */ public static Connection getConn(){ Connection conn = null ; try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * CLOSE THE DATABASE CONNECTION * @param rs */ public static void closeAll(ResultSet rs,Statement stmt,Connection conn){ close(rs); close(stmt); close(conn); } public static void close(ResultSet rs){ if (rs!= null ){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Statement stmt){ if (stmt!= null ){ try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close(Connection conn){ if (conn!= null ){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } } |
這里要注意的是:導(dǎo)入的包均為java.sql包。這里建立了兩個核心方法,后面會經(jīng)常用到 getConn() 和 closeAll()方法,分別用于取得數(shù)據(jù)庫連接和關(guān)閉數(shù)據(jù)庫連接。
2.設(shè)計符合java bean標(biāo)準(zhǔn)的entity類
這里的entity類對應(yīng)于上面的admin表。因此設(shè)計Admin類如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package cn.imath.entity; public class Admin { 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; } } |
entity類的設(shè)計是為了實現(xiàn)對數(shù)據(jù)的封裝,只要對照數(shù)據(jù)庫設(shè)計來就行,然后最好符合java bean的設(shè)計標(biāo)準(zhǔn),用getter/setter實現(xiàn)訪問。
3.設(shè)計訪問數(shù)據(jù)庫的DAO接口
dao接口的設(shè)計非常簡單,目的是為后面的具體的業(yè)務(wù)方法提供一個模版。
AdminDao接口如下:
1
2
3
4
5
6
7
8
9
10
11
|
package cn.imath.dao; import cn.imath.entity.Admin; public interface AdminDao { /** * LOGIN METHOD * @param username * @param password * @return INSTANCE OF Admin */ public Admin login(String username,String password); } |
4.設(shè)計實現(xiàn)DAO接口的實現(xiàn)類
接下來設(shè)計上面DAO接口的實現(xiàn)類,用于實現(xiàn)具體的業(yè)務(wù)。這里就能體現(xiàn)上面模版的作用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package cn.imath.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.imath.dao.AdminDao; import cn.imath.entity.Admin; import cn.imath.util.DBUtil; public class AdminDaoImpl implements AdminDao { /** * LOGIN METHOD */ public Admin login(String username, String password) { String sql = "select * from admin where name=? and password=? " ; Connection conn = DBUtil.getConn(); try { PreparedStatement pstmt = conn.prepareStatement(sql); //set the query parameters pstmt.setString( 1 ,username); pstmt.setString( 2 , password); ResultSet rs = pstmt.executeQuery(); if (rs.next()){ int id = rs.getInt( 1 ); Admin ad = new Admin(); ad.setId(id); ad.setPassword(password); ad.setName(username); return ad; } } catch (SQLException e) { e.printStackTrace(); } return null ; } } |
dao的實現(xiàn)類實現(xiàn)了具體的業(yè)務(wù)方法,封裝了sql的相關(guān)操作。
5.創(chuàng)建Servlet響應(yīng)請求
Servlet負(fù)責(zé)處理請求。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package cn.imath.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.imath.dao.AdminDao; import cn.imath.dao.impl.AdminDaoImpl; import cn.imath.entity.Admin; public class AdminServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "utf-8" ); String method = request.getParameter( "method" ); if (method!= null ){ if ( "login" .equals(method)){ this .login(request,response); } } } private void login(HttpServletRequest request, HttpServletResponse response) { String username = request.getParameter( "username" ); String password = request.getParameter( "password" ); AdminDao dao = new AdminDaoImpl(); Admin ad = dao.login(username, password); if (ad!= null ){ request.setAttribute( "message" , "Login Success" ); } else { request.setAttribute( "message" , "Login Failed" ); } try { request.getRequestDispatcher( "/message.jsp" ).forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
這里將login方法獨立于doPost方法,這樣有助于對功能的擴(kuò)充。
這里介紹的只是MVC+DAO的設(shè)計流程,通過這種模式可以改進(jìn)項目的設(shè)計,使項目結(jié)構(gòu)更為清晰,有了合理的方法,具體的項目才不致太混亂。最后,說一下需要改進(jìn)的地方:
1.數(shù)據(jù)庫的連接控制,可以用連接池進(jìn)行改進(jìn),如DBCP或C3P0
2.數(shù)據(jù)庫操作可以用common dbutils進(jìn)行改進(jìn)
總結(jié)
以上就是本文關(guān)于MVC+DAO設(shè)計模式下的設(shè)計流程詳解的全部內(nèi)容,希望對大家有所幫助。有什么問題,可以隨時留言指出。感謝大家!
原文鏈接:http://blog.csdn.net/tao_sun/article/details/19124853