一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - MVC+DAO設(shè)計模式下的設(shè)計流程詳解

MVC+DAO設(shè)計模式下的設(shè)計流程詳解

2021-01-29 11:54曉風(fēng)well Java教程

這篇文章主要介紹了MVC+DAO設(shè)計模式下的設(shè)計流程詳解,分別介紹了數(shù)據(jù)庫設(shè)計、設(shè)計符合java bean標(biāo)準(zhǔn)的entity類、設(shè)計訪問數(shù)據(jù)庫的DAO接口等內(nèi)容,具有一定參考價值,需要的朋友可以了解下。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一卡二卡科技有限公司 | 91精品综合 | 久久这里有精品 | 小sao货水好多真紧h的视频 | 精品视频国产 | 四虎影院com | 九九精品视频在线播放 | 无码国产成人777爽死 | 美女脱一光二净的视频 | 国产视频一区在线观看 | kkkk4444在线看片免费 | 亚洲 国产精品 日韩 | 青青草色 | 亚洲啊v天堂 | 亚洲欧美日本在线观看 | 亚洲天堂男人网 | 大桥未久一区二区 | 亚洲精品九色在线网站 | 国产区香蕉精品系列在线观看不卡 | 亚洲 欧美 国产 综合久久 | 视频在线观看高清免费 | 大桥未久midd—962在线 | 亚洲区视频在线观看 | 99热热99 | 久久久久久久国产精品视频 | 好男人资源免费播放在线观看 | 日本国产在线视频 | 亚洲国产欧美另类 | 果冻传媒新在线观看免费 | 欧美男人天堂 | 男人插曲女人下面 | 操美女网址 | 亚洲欧美日韩国产一区图片 | 婷婷网址| free性泰国娇小videos | 色综合久久最新中文字幕 | 亚洲精品www久久久久久久软件 | 国产激情一区二区三区四区 | 日本在线精品视频 | 免费观看欧美性一级 | 91视频99|