<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 什么是 DAO 以及如何使用它 > 原文: [https://javatutorial.net/what-is-dao-and-how-to-to-use-it](https://javatutorial.net/what-is-dao-and-how-to-use-it) 在開始實現這些類之前,我們首先要了解什么是 DAO。 如果您已經知道 DAO 是什么,請隨時跳轉至代碼示例。 如果沒有,請忍受我。 ![spring-featured-image](https://img.kancloud.cn/64/23/64231db4bf6e880066d0d4c2e31ac166_780x330.jpg) DAO 代表數據訪問對象,它是一種結構化模式,它通過使用抽象 API 將業務層(邏輯)與持久層(例如數據庫)隔離開來。 簡而言之,對象(DAO)提供了數據庫的抽象接口。 肯定的是,應用程序(業務層/邏輯)不知道有關數據庫的任何詳細信息,因為這是 DAO 的工作。 **它分離業務邏輯和數據**。 讓我們實現 DAO 模式。 使用 DAO 時,最著名的示例是用戶 - 名稱,電子郵件,密碼。 在本文中,我將使用員工示例 – 名稱,電子郵件。 `Employee.java` ```java public class Employee { //members private String name; private String email; // constructor Employee(String n, String e) { name = n; email = e; } // setter methods public void setName(String n) { name = n; } public void setEmail(String e) { email = e; } // getter methods public String getName() { return name; } public String getEmail() { return email; } } ``` **細分** 上一個類沒什么大不了的。 一個標準的構造函數 /setter/getter 方法類。 在編寫`EmployeeDAO`類之前,我們首先來看一下`Dao<T>`接口包含的內容。 ```java public interface Dao<T> { Optional<T> get(long id); List<T> getAll(); void save(T t); void update(T t, String[] params); void delete(T t); } ``` 它具有`get`,`getAll`,`save`,`update`,`delete`方法。 簡而言之,對類型`T`的對象執行 CRUD 操作。現在,我們的下一個目標是在`EmployeeDAO.java`類中實現該接口。 `EmployeeDAO.java` ```java public class EmployeeDAO implements Dao<Employee> { // will act as a "mini-database" private List<Employee> employees = new ArrayList<>(); // Constructor public EmployeeDAO() { // Populate our list of employees with 3 Demos employees.add(new Employee("Demo1", "Demo1@example.com")); employees.add(new Employee("Demo2", "Demo2@example.com")); employees.add(new Employee("Demo3", "Demo3@example.com")); } // Overriding the Dao interface methods @Override public Employee get(long id) { return employees.get((int) id)); } @Override public List<Employee> getAll() { return employees; } @Override public void save(Employee emp) { employees.add(emp); } @Override public void update(Employee employee, String[] params) { // Check for validity if (params[0].length() != 0|| params[1].length() != 0) { // Initialize the employee employee.setName(params[0]); employee.setEmail(params[1]); // Add the Initialized employee to the list of employees (a.k.a. DB) employees.add(employee); } } @Override public void delete(Employee employee) { employees.remove(employee); } } ``` **細分** 由于實現了 Dao<Employee>代碼,我們可以訪問所有被覆蓋的方法,因為我們從 **Dao API**。 如評論中所述,為了簡單起見,可以將雇員列表視為數據庫。 您現在可以看到為什么這種模式非常強大,更重要的是,它是如何工作的嗎? 存在`Employee`類和`EmployeeDAO`而不相互依賴。 如果您熟悉 OO(面向對象)概念,則可以將此關系與 OOP 世界中的**聚合**相關聯。 現在是時候創建應用程序了,也就是業務層或邏輯。 `DemoApplication.java` ```java public class DemoApplication { // Declare an instance of Dao private static Dao employeeDao; public static void main (String[] args) { employeeDao = new EmployeeDAO(); Employee demoEmployee1 = getEmployee(0); employeeDao.update(demoEmployee1, new String[] { "Hugh", "hugh.demo@demo.com" }); Employee demoEmployee2 = getEmployee(1); employeeDao.update(demoEmployee2, new String[] { "Marry", "marry.demo@demo.com" }); Employee demoEmployee3 = getEmployee(2); employeeDao.update(demoEmployee3, new String[] { "Sharry", "sharry.demo@demo.com" }); // print all the employee in the database for (Employee emp : employeeDao.getAll()) { System.out.println(emp.getName()); } // add an entry to the database Employee newDemoEmp = new Employee("Slim", "slimmy@demo.com"); employeeDao.save(newDemoEmp); // print all the employee in the database after we have added another entry for (Employee emp : employeeDao.getAll()) { System.out.println(emp.getName()); } } private static Employee getEmployee(long id) { // Fetch the employee from the Database based on the id provided // Keep in mind that the .get method used below is coming from the DAO class Employee emp = employeeDao.get(id); // Return that employee return emp; } } ``` **細分** 即使上面的代碼被大量注釋,我仍將添加一些注意事項。 在我們的`main`方法之上,我們聲明一個 Dao 實例,該實例將用于我們之前使用的被覆蓋的方法。 注意我們如何在此類中創建另一個名為`getEmployee(long id)`的 getter 方法。 此方法使用另一種`get(long id)"`方法。 內部的`get`方法是我們在`EmployeeDAO`類中覆蓋的方法。 在獲取了具有給定 ID 的員工之后,我們對其進行分配,然后我們立即使用`update`方法來取代。 我們傳遞了要更新的員工實例以及要給該員工的新名稱和新電子郵件。 此后,我們使用覆蓋的`getAll()`方法,這使我們返回了員工列表。 之后,我們只需將一個新創建的`Employee`添加到列表中,然后再次打印該雇員,現在再添加 1 個條目。 ## 結論 DAO 應該執行數據庫操作并以類外可以訪問的方式組織數據。通常,使用 DAO 類的類是`Application`本身或您擁有的`Service`類。 簡而言之, **DAO 為您的服務提供數據**。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看