{"id":153419,"date":"2024-11-02T07:17:20","date_gmt":"2024-11-01T23:17:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/153419\/"},"modified":"2024-11-02T07:17:21","modified_gmt":"2024-11-01T23:17:21","slug":"%e5%be%9e%e6%ba%90%e9%a0%ad%e9%96%8b%e5%a7%8b%ef%bc%8cdao-%e5%b1%a4%e5%a6%82%e4%bd%95%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-dao-%e5%b1%a4-%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/153419\/","title":{"rendered":"\u5f9e\u6e90\u982d\u958b\u59cb\uff0cDAO \u5c64\u5982\u4f55\u93c8\u63a5\u6578\u64da\u5eab\uff1f (DAO \u5c64 \u93c8\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%be%9e%e6%ba%90%e9%a0%ad%e9%96%8b%e5%a7%8b%ef%bc%8cdao-%e5%b1%a4%e5%a6%82%e4%bd%95%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-xsNKgbuNvc\">\u5f9e\u6e90\u982d\u958b\u59cb\uff0cDAO \u5c64\u5982\u4f55\u93c8\u63a5\u6578\u64da\u5eab\uff1f<\/h1>\n<p>\u5728\u73fe\u4ee3\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u6578\u64da\u7684\u7ba1\u7406\u548c\u5b58\u53d6\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5f0f\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u958b\u767c\u8005\u9700\u8981\u4e00\u7a2e\u6709\u6548\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u3002\u6578\u64da\u8a2a\u554f\u5c0d\u8c61\uff08DAO\uff0cData Access Object\uff09\u5c64\u662f\u4e00\u7a2e\u8a2d\u8a08\u6a21\u5f0f\uff0c\u65e8\u5728\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u8207\u696d\u52d9\u908f\u8f2f\u5206\u96e2\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0eDAO\u5c64\u5982\u4f55\u93c8\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afdao%e5%b1%a4%ef%bc%9f-xsNKgbuNvc\">\u4ec0\u9ebc\u662fDAO\u5c64\uff1f<\/h2>\n<p>DAO\u5c64\u662f\u4e00\u7a2e\u8a2d\u8a08\u6a21\u5f0f\uff0c\u4e3b\u8981\u7528\u65bc\u5c01\u88dd\u6578\u64da\u5b58\u53d6\u908f\u8f2f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u62bd\u8c61\u5c64\uff0c\u4f7f\u5f97\u61c9\u7528\u7a0b\u5f0f\u7684\u5176\u4ed6\u90e8\u5206\u4e0d\u9700\u8981\u76f4\u63a5\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u4f7f\u5f97\u6578\u64da\u5eab\u7684\u66f4\u6539\u4e0d\u6703\u5f71\u97ff\u5230\u696d\u52d9\u908f\u8f2f\u3002<\/p>\n<h2 id=\"dao%e5%b1%a4%e7%9a%84%e7%b5%90%e6%a7%8b-xsNKgbuNvc\">DAO\u5c64\u7684\u7d50\u69cb<\/h2>\n<p>DAO\u5c64\u901a\u5e38\u7531\u4ee5\u4e0b\u5e7e\u500b\u7d44\u4ef6\u7d44\u6210\uff1a<\/p>\n<ul>\n<li><strong>DAO\u63a5\u53e3\uff1a<\/strong>\u5b9a\u7fa9\u6578\u64da\u8a2a\u554f\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u589e\u3001\u522a\u3001\u6539\u3001\u67e5\u3002<\/li>\n<li><strong>DAO\u5be6\u73fe\uff1a<\/strong>\u5be6\u73feDAO\u63a5\u53e3\uff0c\u5177\u9ad4\u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/strong>\u8ca0\u8cac\u8207\u6578\u64da\u5eab\u5efa\u7acb\u9023\u63a5\uff0c\u901a\u5e38\u4f7f\u7528\u9023\u63a5\u6c60\u4f86\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u6578\u64da\u6a21\u578b\uff1a<\/strong>\u7528\u65bc\u8868\u793a\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u7d50\u69cb\u3002<\/li>\n<\/ul>\n<h2 id=\"dao%e5%b1%a4%e5%a6%82%e4%bd%95%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-xsNKgbuNvc\">DAO\u5c64\u5982\u4f55\u93c8\u63a5\u6578\u64da\u5eab<\/h2>\n<p>DAO\u5c64\u93c8\u63a5\u6578\u64da\u5eab\u7684\u904e\u7a0b\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%bb%ba%e7%ab%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-xsNKgbuNvc\">1. \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u9019\u901a\u5e38\u901a\u904e\u4f7f\u7528\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528JDBC\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    private static final String URL = \"jdbc:mysql:\/\/localhost:3306\/mydatabase\";\n    private static final String USER = \"username\";\n    private static final String PASSWORD = \"password\";\n\n    public static Connection getConnection() throws SQLException {\n        return DriverManager.getConnection(URL, USER, PASSWORD);\n    }\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%af%a6%e7%8f%bedao%e6%8e%a5%e5%8f%a3-xsNKgbuNvc\">2. \u5be6\u73feDAO\u63a5\u53e3<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5be6\u73feDAO\u63a5\u53e3\uff0c\u4e26\u5728\u5176\u4e2d\u7de8\u5beb\u5177\u9ad4\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u908f\u8f2f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u4e00\u500b\u7528\u65bc\u64cd\u4f5c\u7528\u6236\u6578\u64da\u7684DAO\u5be6\u73fe\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\n\npublic class UserDaoImpl implements UserDao {\n    @Override\n    public User getUserById(int id) {\n        User user = null;\n        String sql = \"SELECT * FROM users WHERE id = ?\";\n        \n        try (Connection connection = DatabaseConnection.getConnection();\n             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {\n             \n            preparedStatement.setInt(1, id);\n            ResultSet resultSet = preparedStatement.executeQuery();\n            \n            if (resultSet.next()) {\n                user = new User(resultSet.getInt(\"id\"), resultSet.getString(\"name\"));\n            }\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n        return user;\n    }\n}\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8dao%e5%b1%a4-xsNKgbuNvc\">3. \u4f7f\u7528DAO\u5c64<\/h3>\n<p>\u6700\u5f8c\uff0c\u5728\u696d\u52d9\u908f\u8f2f\u4e2d\u4f7f\u7528DAO\u5c64\u4f86\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>UserDao userDao = new UserDaoImpl();\nUser user = userDao.getUserById(1);\nSystem.out.println(\"User Name: \" + user.getName());\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xsNKgbuNvc\">\u7e3d\u7d50<\/h2>\n<p>DAO\u5c64\u662f\u4e00\u7a2e\u6709\u6548\u7684\u8a2d\u8a08\u6a21\u5f0f\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u8207\u696d\u52d9\u908f\u8f2f\u5206\u96e2\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u901a\u904e\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u3001\u5be6\u73feDAO\u63a5\u53e3\u4ee5\u53ca\u5728\u696d\u52d9\u908f\u8f2f\u4e2d\u4f7f\u7528DAO\u5c64\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u6578\u64da\u5b58\u53d6\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u7ba1\u7406\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u8a2a\u554f\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22DAO\u5c64\u5982\u4f55\u5f9e\u6e90\u982d\u958b\u59cb\u6709\u6548\u93c8\u63a5\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u8207\u61c9\u7528\u7684\u9ad8\u6548\u6574\u5408\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-153419","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=153419"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153419\/revisions"}],"predecessor-version":[{"id":153420,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153419\/revisions\/153420"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=153419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=153419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=153419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}