{"id":29463,"date":"2025-04-20T09:25:59","date_gmt":"2025-04-20T01:25:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/29463.html"},"modified":"2025-04-20T09:25:59","modified_gmt":"2025-04-20T01:25:59","slug":"%e6%96%bd%e7%a3%8a%e8%80%81%e5%b8%88%e5%9f%ba%e4%ba%8emuduo%e7%bd%91%e7%bb%9c%e5%ba%93%e7%9a%84%e9%9b%86%e7%be%a4%e8%81%8a%e5%a4%a9%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/29463.html","title":{"rendered":"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09)"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e1a\u52a1\u6a21\u5757ChatService<\/li>\n<li>\n<ul>\n<li>\u7f51\u7edc\u6a21\u5757-\u8fde\u63a5\u56de\u8c03<\/li>\n<li>\u5b9e\u73b0\u89e3\u8026<\/li>\n<li>\u4e1a\u52a1\u5934\u6587\u4ef6<\/li>\n<li>\u516c\u5171\u5934\u6587\u4ef6<\/li>\n<li>\u4e1a\u52a1\u51fd\u6570\u5b9a\u4e49\u6587\u4ef6<\/li>\n<li>\u8865\u5145\u7f51\u8def\u6a21\u5757onMessgae()<\/li>\n<li>\u6d4b\u8bd5<\/li>\n<li>\u81f3\u6b64<\/li>\n<\/ul>\n<\/li>\n<li>mysql\u6570\u636e\u5e93\u4ee3\u7801\u5c01\u88c5<\/li>\n<li>\n<ul>\n<li>ORM (\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04) \u6846\u67b6<\/li>\n<li>DAO (Data Access Object) \u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u6a21\u5f0f<\/li>\n<li>\u5206\u79bb\u6570\u636e\u5c42\u4e0e\u4e1a\u52a1\u5c42<\/li>\n<li>\u6570\u636e\u5e93\u8bfb\u53d6\u5934\u6587\u4ef6<\/li>\n<li>\u6570\u636e\u5e93\u7c7b\u51fd\u6570\u6e90\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528ORM, \u5b9e\u73b0\u6ce8\u518c\u4e1a\u52a1<\/li>\n<li>\n<ul>\n<li>**\u7b2c\u4e00\u6b65:**<\/li>\n<li>**\u7b2c\u4e8c\u6b65:**<\/li>\n<li>**\u7b2c\u4e09\u6b65:**<\/li>\n<li>\u9519\u8bef1:<\/li>\n<li>\u5c0f\u6280\u5de7&#8211;\u7ea2\u8272\u6ce2\u6d6a\u7ebf\u95ee\u9898<\/li>\n<li>\u8865\u5145\u5e76\u6d4b\u8bd5\u6ce8\u518c\u4e1a\u52a1<\/li>\n<li>mysql\u8fde\u63a5\u5931\u8d25\u89e3\u51b3<\/li>\n<\/ul>\n<\/li>\n<li>\u9636\u6bb5\u603b\u7ed3-1<\/li>\n<\/ul>\n<h2>\u4e1a\u52a1\u6a21\u5757ChatService<\/h2>\n<h3>\u7f51\u7edc\u6a21\u5757-\u8fde\u63a5\u56de\u8c03<\/h3>\n<p>\u5148\u8865\u5145\u4e00\u4e0b \u8fd9\u90e8\u5206 ChatServer::onConnect \u51fd\u6570<\/p>\n<p>\u5148\u8003\u8651\u4e00\u4e0b:<\/p>\n<p> \u4e0d\u60f3\u5728\u8fd9\u91cc \u5199 ifelse\u7b49\u7b49, \u8c03\u7528\u767b\u5f55\u4e1a\u52a1\u5565\u7684,\u6ce8\u518c\u670d\u52a1\u5565\u7684&#8212;- \u8fd9\u6837\u4e0d\u597d&#8212;-\u8fd9\u6837\u628a \u7f51\u7edc\u6a21\u5757\u548c \u4e1a\u52a1\u6a21\u5757 \u653e\u4e00\u8d77\u4e86, \u5f3a\u8026\u5408\u6027, \u5e76\u6ca1\u6709\u89e3\u8026<\/p>\n<p>\u5f3a\u8026\u5408&#xff08;Tight Coupling&#xff09;\u662f\u6307 \u6a21\u5757&#xff08;\u7c7b\u3001\u7ec4\u4ef6\u3001\u670d\u52a1&#xff09;\u4e4b\u95f4\u9ad8\u5ea6\u4f9d\u8d56\u5f7c\u6b64\u7684\u5b9e\u73b0\u7ec6\u8282<\/p>\n<h3>\u5b9e\u73b0\u89e3\u8026<\/h3>\n<p>\u4e0d\u8981\u5728 \u7f51\u7edc\u6a21\u5757\u91cc \u76f4\u63a5\u8c03\u7528\u4e1a\u52a1\u6a21\u5757\u7684 \u4ee3\u7801\u548c\u65b9\u6cd5, \u6211\u4eec\u5e0c\u671b \u5728\u7f51\u7edc\u6a21\u5757\u91cc, \u662f\u770b\u4e0d\u5230\u4e1a\u52a1\u6a21\u5757\u7684 \u4ee3\u7801<\/p>\n<p>\u89e3\u8026<\/p>\n<ul>\n<li>\u7f51\u7edc\u6a21\u5757\u5e94\u907f\u514d\u76f4\u63a5\u4f9d\u8d56\u4e1a\u52a1\u6a21\u5757\u7684\u4ee3\u7801\u6216\u65b9\u6cd5&#xff0c;\u4fdd\u6301\u6a21\u5757\u95f4\u7684\u9694\u79bb\u6027\u3002\u7406\u60f3\u60c5\u51b5\u4e0b&#xff0c;\u7f51\u7edc\u6a21\u5757\u7684\u4ee3\u7801\u4e2d\u4e0d\u5e94\u51fa\u73b0\u4e1a\u52a1\u903b\u8f91\u76f8\u5173\u7684\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p>\u600e\u4e48\u5b9e\u73b0\u89e3\u8026\u5462?<\/p>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u3001\u534f\u8bae\u62bd\u8c61\u6216\u4e8b\u4ef6\u901a\u77e5\u7b49\u65b9\u5f0f\u5b9e\u73b0\u89e3\u8026&#xff0c;\u786e\u4fdd\u7f51\u7edc\u6a21\u5757\u53ea\u5904\u7406\u901a\u4fe1\u76f8\u5173\u903b\u8f91&#xff0c;\u4e1a\u52a1\u6a21\u5757\u8d1f\u8d23\u5177\u4f53\u7684\u6570\u636e\u5904\u7406\u548c\u4e1a\u52a1\u89c4\u5219\u3002<\/li>\n<\/ul>\n<p>\u5728oop\u8bed\u8a00\u91cc\u8fb9&#xff0c;\u8981\u89e3\u5076\u6a21\u5757\u4e4b\u95f4\u7684\u8fd9\u4e2a\u5173\u7cfb\u554a&#xff0c;\u4e00\u822c\u6709\u4e24\u79cd\u65b9\u6cd5:<\/p>\n<li>\n<p>\u4e00\u79cd\u5c31\u662f\u4f7f\u7528\u57fa\u4e8e\u9762\u5411\u63a5\u53e3\u7684\u7f16\u7a0b<\/p>\n<p>\u5728C&#043;&#043;\u91cc\u8fb9\u6ca1\u6709\u6240\u8c13\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a&#xff0c;\u5728C&#043;&#043;\u91cc\u8fb9\u63a5\u53e3\u5c31\u662f\u62bd\u8c61\u7c7b\u561b&#xff0c;\u62bd\u8c61\u57fa\u7c7b&#xff0c;\u9762\u5411\u62bd\u8c61\u57fa\u7c7b\u7684\u7f16\u7a0b&#xff0c;\u6216\u8005\u5c31\u662f\u57fa\u4e8e\u8fd9\u4e2a\u3002<\/p>\n<\/li>\n<li>\n<p>\u2026<\/p>\n<\/li>\n<p>\u82f1\u8bed: \u5728\u7f16\u7a0b\u4e2d&#xff0c;handler&#xff08;\u4e2d\u6587\u5e38\u8bd1\u4e3a\u201c\u5904\u7406\u5668\u201d\u6216\u201c\u5904\u7406\u7a0b\u5e8f\u201d&#xff09;\u901a\u5e38\u6307\u7528\u4e8e\u54cd\u5e94\u7279\u5b9a\u4e8b\u4ef6\u6216\u7ba1\u7406\u7279\u5b9a\u4efb\u52a1\u7684\u4ee3\u7801\u5355\u5143\u3002<\/p>\n<h3>\u4e1a\u52a1\u5934\u6587\u4ef6<\/h3>\n<p>include\/server\/chatservice.hpp<\/p>\n<p>\/\/ \u4ec5\u9700\u8981\u4e00\u4e2a \u5b9e\u4f8b\u5373\u53ef \u56e0\u6b64\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/p>\n<p>#ifndef CHATSERVICE_H<br \/>\n#define CHATSERVICE_H<\/p>\n<p>#include &lt;muduo\/net\/TcpConnection.h&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace muduo;<br \/>\nusing namespace muduo::net;<\/p>\n<p>#include &#034;json.hpp&#034;<br \/>\nusing namespace nlohmann;<\/p>\n<p>\/\/\u8868\u793a\u5904\u7406\u6d88\u606f\u7684\u4e8b\u4ef6\u56de\u8c03\u65b9\u6cd5\u7c7b\u578b<br \/>\nusing MsgHandler &#061; std::function&lt;void(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time)&gt;; \/\/ #1<\/p>\n<p>\/\/ \u804a\u5929\u670d\u52a1\u5668 \u4e1a\u52a1\u7c7b<br \/>\nclass ChatService<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/ \u83b7\u53d6\u5355\u4f8b\u5bf9\u8c61\u7684\u63a5\u53e3\u51fd\u6570 #6<br \/>\n    static ChatService *instance();<\/p>\n<p>    \/\/ \u5904\u7406\u7684\u767b\u5f55\u4e1a\u52a1 #2<br \/>\n    void login(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time);<br \/>\n    \/\/ \u5904\u7406\u6ce8\u518c\u4e1a\u52a1 #3<br \/>\n    void reg(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time);<\/p>\n<p>    \/\/\u83b7\u53d6\u6d88\u606f\u5bf9\u5e94\u7684\u5904\u7406\u5668 #7<br \/>\n    MsgHandler getHandler(int msgid);<\/p>\n<p>private:<br \/>\n    \/\/ \u5355\u4f8b\u6a21\u5f0f&#8212;-\u6784\u9020\u51fd\u6570\u79c1\u6709\u5316,\u5e76\u5199\u4e00\u4e2a\u60df\u4e00\u7684\u5b9e\u4f8b<br \/>\n    ChatService(); \/\/ #5<\/p>\n<p>    \/\/ \u5b58\u50a8\u6d88\u606fid \u548c \u5176\u5bf9\u5e94\u7684 \u4e1a\u52a1\u5904\u7406\u65b9\u6cd5<br \/>\n    unordered_map&lt;int, MsgHandler&gt; _msghandlermap; \/\/ #4<br \/>\n};<\/p>\n<p>#endif<\/p>\n<h3>\u516c\u5171\u5934\u6587\u4ef6<\/h3>\n<p>include\/public.hpp<\/p>\n<p>#ifndef PUBLIC_H<br \/>\n#define PUBLIC_H<br \/>\n\/*<br \/>\nserver \u548c client \u7684\u516c\u5171\u6587\u4ef6<br \/>\n*\/<\/p>\n<p>enum EnMsgType  \/\/ \u679a\u4e3e<br \/>\n{<br \/>\n    LOGIN_MSG&#061;1,   \/\/ \u4e0e\u4e1a\u52a1\u7684login\u51fd\u6570\u8fde\u63a5<br \/>\n    REG_MSG  \/\/\u6ce8\u518c\u6d88\u606f<br \/>\n};<\/p>\n<p>#endif<\/p>\n<h3>\u4e1a\u52a1\u51fd\u6570\u5b9a\u4e49\u6587\u4ef6<\/h3>\n<p>src\/server\/service.cpp<\/p>\n<p>\u4f7f\u7528\u4e86 \u7ebf\u7a0b\u5b89\u5168\u7684 \u975e\u4e92\u65a5\u9501 \u61d2\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f\u2014\u8be6\u89c1c&#043;&#043;\u7b14\u8bb0\u4e13\u680f<\/p>\n<p>\u5728 C&#043;&#043; \u4e2d&#xff0c;\u5982\u679c\u5f02\u5e38\u6ca1\u6709\u88ab\u6355\u83b7&#xff08;uncaught exception&#xff09;&#xff0c;\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u8c03\u7528 std::terminate()&#xff0c;\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\u3002\u8fd9\u662f C&#043;&#043; \u5f02\u5e38\u5904\u7406\u673a\u5236\u7684\u6838\u5fc3\u884c\u4e3a\u4e4b\u4e00\u3002<\/p>\n<p>\u201c\u5982\u679c\u7f51\u7edc\u6a21\u5757&#xff08;\u5982 muduo&#xff09;\u8c03\u7528\u4e86\u670d\u52a1\u6a21\u5757&#xff08;\u5982 ChatService&#xff09;&#xff0c;\u800c\u670d\u52a1\u6a21\u5757\u629b\u51fa\u4e86\u5f02\u5e38&#xff0c;\u4f46\u7f51\u7edc\u6a21\u5757\u6ca1\u6709\u6355\u83b7\u5b83&#xff0c;\u90a3\u6574\u4e2a\u8fdb\u7a0b\u5c31\u4f1a\u5d29\u6e83\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b&#xff0c;\u6211\u4eec\u662f\u4e0d\u662f\u5fc5\u987b\u5728\u7f51\u7edc\u6a21\u5757\u91cc\u5904\u7406\u670d\u52a1\u6a21\u5757\u629b\u51fa\u7684\u5f02\u5e38&#xff1f;\u5426\u5219\u7a0b\u5e8f\u5c31\u5b8c\u86cb\u4e86&#xff1f;\u201d<\/p>\n<p>\u7b54\u6848\u662f&#xff1a;\u662f\u7684&#xff01; \u5982\u679c\u5f02\u5e38\u6ca1\u6709\u88ab\u6355\u83b7&#xff0c;\u5b83\u4f1a\u5bfc\u81f4 \u7a0b\u5e8f\u7ec8\u6b62&#xff08;std::terminate \u88ab\u8c03\u7528&#xff09;&#xff0c;\u8fd9\u5728\u670d\u52a1\u5668\u7a0b\u5e8f\u4e2d\u662f \u707e\u96be\u6027\u7684&#xff08;\u6240\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u90fd\u4f1a\u65ad\u5f00&#xff0c;\u670d\u52a1\u4e0d\u53ef\u7528&#xff09;\u3002<\/p>\n<p>#include &#034;chatservice.hpp&#034;<br \/>\n#include &#034;public.hpp&#034;<br \/>\n#include &lt;muduo\/base\/Logging.h&gt;<br \/>\nusing namespace muduo;<\/p>\n<p>\/\/ \u83b7\u53d6\u5355\u4f8b\u5bf9\u8c61\u7684 \u51fd\u6570\u63a5\u53e3<br \/>\nChatService *ChatService::instance()<br \/>\n{<br \/>\n    static ChatService service; \/\/ \u7ebf\u7a0b\u5b89\u5168 \u61d2\u6c49\u5f0f\u5355\u4f8b<br \/>\n    return &amp;service;<br \/>\n}<\/p>\n<p>\/\/ \u6ce8\u518c\u6d88\u606f\u4ee5\u53ca\u5bf9\u5e94\u7684 handler \u56de\u8c03<br \/>\nChatService::ChatService()<br \/>\n{<br \/>\n    \/\/ \u7f51\u7edc\u6a21\u5757\u548c\u4e1a\u52a1\u6a21\u5757 \u89e3\u8026\u7684\u6838\u5fc3<\/p>\n<p>    _msghandlermap.insert({LOGIN_MSG, std::bind(&amp;ChatService::login, this, _1, _2, _3)});<\/p>\n<p>    _msghandlermap.insert({REG_MSG, std::bind(&amp;ChatService::reg, this, _1, _2, _3)});<br \/>\n}<\/p>\n<p>\/\/\u83b7\u53d6\u6d88\u606f\u5bf9\u5e94\u7684\u5904\u7406\u5668<br \/>\nMsgHandler ChatService::getHandler(int msgid)<br \/>\n{<br \/>\n    \/\/\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7, msgid \u6ca1\u6709\u5bf9\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03<br \/>\n    auto it &#061; _msghandlermap.find(msgid);<br \/>\n    if(it &#061;&#061; _msghandlermap.end())<br \/>\n    {<br \/>\n        \/\/ \u4f7f\u7528 muduo \u81ea\u5e26\u7684\u65e5\u5fd7\u7cfb\u7edf<br \/>\n        \/\/ \u4e0d\u8981endl, \u5df2\u7ecf\u5c01\u88c5\u4e86<br \/>\n        \/\/ LOG_ERROR&lt;&lt;&#034;msgid: &#034;&lt;&lt;msgid&lt;&lt;&#034;can not find Handler!&#034;; <\/p>\n<p>        return [&#061;](const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time){<br \/>\n            LOG_ERROR&lt;&lt;&#034;msgid: &#034;&lt;&lt;msgid&lt;&lt;&#034;can not find Handler!&#034;;<br \/>\n        };<br \/>\n    }<\/p>\n<p>    else<br \/>\n    {<br \/>\n        return _msghandlermap[msgid];<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>\/\/ \u5904\u7406\u767b\u5f55\u4e1a\u52a1<br \/>\nvoid ChatService::login(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time)<br \/>\n{<br \/>\n    LOG_INFO&lt;&lt;&#034;do login service&#034;; \/\/\u6d4b\u8bd5\u7528<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406\u6ce8\u518c\u4e1a\u52a1<br \/>\nvoid ChatService::reg(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time)<br \/>\n{<br \/>\n    LOG_INFO&lt;&lt;&#034;do reg service&#034;; \u6d4b\u8bd5\u7528<br \/>\n}<\/p>\n<h3>\u8865\u5145\u7f51\u8def\u6a21\u5757onMessgae()<\/h3>\n<p>void ChatServer::onMessage(const TcpConnectionPtr&amp; conn,<br \/>\n    Buffer* buffer,<br \/>\n    Timestamp time)<br \/>\n{<br \/>\n    string buf &#061; buffer-&gt;retrieveAllAsString();<br \/>\n    \/\/ \u6570\u636e\u53cd\u5e8f\u5217\u5316<br \/>\n    json js &#061; json::parse(buf);<\/p>\n<p>     \/\/\u8fbe\u5230\u7684\u76ee\u7684: \u5b8c\u5168\u89e3\u8026\u7f51\u7edc\u6a21\u5757\u7684\u4ee3\u7801 \u548c \u4e1a\u52a1\u6a21\u5757\u7684 \u4ee3\u7801<br \/>\n     \/\/\u901a\u8fc7js[&#034;msgid&#034;]\u83b7\u53d6&#061;&gt;\u4e1a\u52a1handler&#061;&gt;\u4f20 conn, js, time\u7b49\u4fe1\u606f<br \/>\n    \/*<br \/>\n        \u8fd9\u6837, \u4e1a\u52a1\u4ec5\u6709 \u4e24\u884c\u4ee3\u7801, \u6ca1\u6709\u4efb\u4f55\u4e1a\u52a1\u5c42\u7684\u65b9\u6cd5(login,reg\u7b49)\u8c03\u7528<br \/>\n        \u4ec5\u9700\u5728 \u670d\u52a1\u5c42\u5185\u90e8, \u505a\u4e00\u4e2a \u4e1a\u52a1\u76f8\u5e94\u7684 \u56de\u8c03<br \/>\n    *\/<br \/>\n     \/\/\u83b7\u53d6msgid \u5bf9\u5e94\u7684\u5904\u7406\u5668<br \/>\n     auto msgHandler &#061; ChatService::instance()-&gt;getHandler(js[&#034;msgid&#034;].get&lt;int&gt;());  \/\/json\u952e\u662f\u5b57\u7b26\u4e32, \u8981\u6362\u6210\u6574\u578b&#8212;-get\u65b9\u6cd5, \u4f1a\u7528\u5c31\u884c<br \/>\n     \/\/ \u56de\u8c03\u6d88\u606f\u7ed1\u5b9a\u597d\u7684 \u4e8b\u4ef6\u5904\u7406\u5668, \u6765\u6267\u884c\u76f8\u5e94\u7684\u4e1a\u52a1\u5904\u7406<br \/>\n    msgHandler(conn, js, time);<\/p>\n<p>}<\/p>\n<h3>\u6d4b\u8bd5<\/h3>\n<p>\u6ce8\u610f: \u524d\u9762\u7684 CMakeLists.txt \u8981\u6dfb\u52a0 json \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84<\/p>\n<p>\/\/client<br \/>\n{&#034;msgid&#034;:1}<\/p>\n<p>\/\/server \u6253\u5370<br \/>\n20250418 03:41:52.258725Z 31993 INFO  do login service &#8211; chatservice.cpp:50<\/p>\n<h3>\u81f3\u6b64<\/h3>\n<p>\u6bcf\u4e00\u4e2a\u5927\u7684\u6a21\u5757, \u5199\u5b8c\u90fd\u8981\u6d4b\u8bd5\u4e00\u4e0b<\/p>\n<p>\u7f51\u7edc \u6a21\u5757 \u5b8c\u6bd5, \u5269\u4e0b\u7684 \u5c06\u4e13\u6ce8\u4e8e \u4e1a\u52a1\u5c42 \u4e0e \u6570\u636e\u5c42!!!<\/p>\n<h2>mysql\u6570\u636e\u5e93\u4ee3\u7801\u5c01\u88c5<\/h2>\n<p>\u4e1a\u52a1\u5c42\u5904\u7406 \u6570\u636e\u5c42&#8212;-&gt; \u8981\u4f4e\u8026\u5408, \u4e0d\u8981\u5728 \u4e1a\u52a1\u5c42 \u5199\u6570\u636e\u5e93\u4ee3\u7801<\/p>\n<h3>ORM (\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04) \u6846\u67b6<\/h3>\n<p>ORM (Object-Relational Mapping) \u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f&#xff0c;\u7528\u4e8e\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5b9e\u73b0\u4e0d\u517c\u5bb9\u7c7b\u578b\u7cfb\u7edf\u4e4b\u95f4\u7684\u6570\u636e\u8f6c\u6362&#xff0c;\u5373\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u548c\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u4e4b\u95f4\u5efa\u7acb\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n<h3>DAO (Data Access Object) \u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u6a21\u5f0f<\/h3>\n<p>DAO \u662f\u4e00\u79cd\u6838\u5fc3\u7684J2EE\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u7528\u4e8e\u62bd\u8c61\u548c\u5c01\u88c5\u5bf9\u6570\u636e\u6e90\u7684\u8bbf\u95ee&#xff0c;\u5c06\u5e95\u5c42\u6570\u636e\u8bbf\u95ee\u903b\u8f91\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\u3002<\/p>\n<p>1. \u5b9a\u4e49<\/p>\n<p>DAO (Data Access Object) \u662f\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u6a21\u5f0f\u7684\u7b80\u79f0&#xff0c;\u5b83&#xff1a;<\/p>\n<ul>\n<li>\u4f4d\u4e8e\u4e1a\u52a1\u903b\u8f91\u4e0e\u6301\u4e45\u5316\u5b58\u50a8\u4e4b\u95f4<\/li>\n<li>\u5c01\u88c5\u6240\u6709\u6570\u636e\u8bbf\u95ee\u7ec6\u8282<\/li>\n<li>\u4e3a\u4e0a\u5c42\u63d0\u4f9b\u7edf\u4e00\u7684\u6570\u636e\u64cd\u4f5c\u63a5\u53e3<\/li>\n<\/ul>\n<p>2. \u6838\u5fc3\u7ec4\u4ef6<\/p>\n<ul>\n<li>DAO\u63a5\u53e3&#xff1a;\u5b9a\u4e49\u6570\u636e\u64cd\u4f5c\u7684\u6807\u51c6\u65b9\u6cd5<\/li>\n<li>DAO\u5b9e\u73b0\u7c7b&#xff1a;\u9488\u5bf9\u4e0d\u540c\u6570\u636e\u6e90\u7684\u5177\u4f53\u5b9e\u73b0<\/li>\n<li>\u6570\u636e\u4f20\u8f93\u5bf9\u8c61(DTO)&#xff1a;\u5728\u5c42\u95f4\u4f20\u8f93\u6570\u636e\u7684\u8f7d\u4f53<\/li>\n<\/ul>\n<h3>\u5206\u79bb\u6570\u636e\u5c42\u4e0e\u4e1a\u52a1\u5c42<\/h3>\n<p>\u4e1a\u52a1\u5c42\u64cd\u4f5c\u7684\u90fd\u662f \u5bf9\u8c61 \u2014&gt; \u4f7f\u7528ORM<\/p>\n<p>\u6570\u636e\u5c42 \u5c01\u88c5\u4e86\u6240\u6709\u6570\u636e\u5e93\u64cd\u4f5c \u2014&gt; \u4f7f\u7528DAO<\/p>\n<h3>\u6570\u636e\u5e93\u8bfb\u53d6\u5934\u6587\u4ef6<\/h3>\n<p>\u6dfb\u52a0 \u6570\u636e\u5c42\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u4ee5\u53ca \u7f16\u8bd1\u8981\u52a0 mysqlclient<\/p>\n<p>include_directories(${PROJECT_SOURCE_DIR}\/include\/server\/db) #\u6570\u636e\u5c42\u5934\u6587\u4ef6<\/p>\n<p>target_link_libraries(Chatserver muduo_net muduo_base pthread mysqlclient)<\/p>\n<p>\u4e00\u5b9a\u8981\u5b89\u88c5 libmysqlclient-dev, \u8981\u6709\u8fd9\u4e2a\u5e93\u6587\u4ef6<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">apt-get<\/span> <span class=\"token function\">install<\/span> libmysqlclient-devsudo <span class=\"token function\">apt-get<\/span> <span class=\"token function\">install<\/span> libmysqlclient-dev<\/p>\n<p>include\/db\/db.h<\/p>\n<p>#ifndef DB_H<br \/>\n#define DB_H<\/p>\n<p>#include &lt;mysql\/mysql.h&gt; \/\/\u53ea\u6709\u5b89\u88c5\u4e86 \u5e93, \u624d\u6709\u8fd9\u4e2a\u5934\u6587\u4ef6<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f<br \/>\nstatic string server &#061; &#034;127.0.0.1&#034;;<br \/>\nstatic string user &#061; &#034;root&#034;;<br \/>\nstatic string password &#061; &#034;123456&#034;;<br \/>\nstatic string dbname &#061; &#034;chat&#034;;<br \/>\n\/\/ \u6570\u636e\u5e93\u64cd\u4f5c\u7c7b<br \/>\nclass MySQL<br \/>\n{<br \/>\npublic:<br \/>\n    \/\/ \u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5<br \/>\n    MySQL();<\/p>\n<p>    ~MySQL();<br \/>\n    \/\/ \u8fde\u63a5\u6570\u636e\u5e93<br \/>\n    bool connect();<\/p>\n<p>    \/\/ \u66f4\u65b0\u64cd\u4f5c<br \/>\n    bool update(string sql);<\/p>\n<p>    \/\/ \u67e5\u8be2\u64cd\u4f5c<br \/>\n    MYSQL_RES *query(string sql);<\/p>\n<p>private:<br \/>\n    MYSQL *_conn;<br \/>\n};<\/p>\n<p>#endif<\/p>\n<h3>\u6570\u636e\u5e93\u7c7b\u51fd\u6570\u6e90\u6587\u4ef6<\/h3>\n<p>\u8981\u6ce8\u610f: CMakeLists.txt \u7f16\u8bd1\u65f6 \u8981\u5305\u542b\u8fd9\u4e2acpp<\/p>\n<p># \u6240\u6709\u6e90\u6587\u4ef6<br \/>\naux_source_directory(. SRC_LIST)<br \/>\n# \u6570\u636e\u5c42\u6b65\u9aa4 \u9700\u8981\u6dfb\u52a0<br \/>\naux_source_directory(.\/db DB_LIST)<br \/>\n# \u751f\u6210\u53ef\u6267\u884c<br \/>\nadd_executable(Chatserver ${SRC_LIST} ${DB_LIST})<\/p>\n<p># \u94fe\u63a5\u5e93<br \/>\ntarget_link_libraries(Chatserver muduo_net muduo_base pthread mysqlclient)<\/p>\n<p>src\/db\/db.cpp<\/p>\n<p>\u4ee3\u7801\u770b\u4e0d\u660e\u767d&#8212;-\u9700\u8981\u8865\u5145mysql\u77e5\u8bc6<\/p>\n<p>#include &#034;db.h&#034;<br \/>\n#include &lt;muduo\/base\/Logging.h&gt;<br \/>\nusing namespace muduo;<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5<br \/>\nMySQL::MySQL()<br \/>\n{<br \/>\n    _conn &#061; mysql_init(nullptr);<br \/>\n}<br \/>\n\/\/ \u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5\u8d44\u6e90\u8fd9\u91cc\u7528UserModel\u793a\u4f8b&#xff0c;\u901a\u8fc7UserModel\u5982\u4f55\u5bf9\u4e1a\u52a1\u5c42\u5c01\u88c5\u5e95\u5c42\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b&#xff1a;<br \/>\nMySQL::~MySQL()<br \/>\n{<br \/>\n    if (_conn !&#061; nullptr)<br \/>\n        mysql_close(_conn);<br \/>\n}<br \/>\n\/\/ \u8fde\u63a5\u6570\u636e\u5e93<br \/>\nbool MySQL::connect()<br \/>\n{<br \/>\n    MYSQL *p &#061; mysql_real_connect(_conn, server.c_str(), user.c_str(),<br \/>\n                                  password.c_str(), dbname.c_str(), 3306, nullptr, 0);<br \/>\n    if (p !&#061; nullptr)<br \/>\n    {<br \/>\n        \/\/ c\/c&#043;&#043;\u4ee3\u7801\u9ed8\u8ba4\u7684\u7f16\u7801\u5b57\u7b26\u662fASCII, \u5982\u679c\u4e0d\u8bbe\u7f6e, \u4ecemysql \u62c9\u4e0b\u6765\u7684\u4e2d\u6587\u663e\u793a \u4e0d\u80fd\u6b63\u5e38\u7528<br \/>\n        mysql_query(_conn, &#034;set names gbk&#034;);<br \/>\n    }<br \/>\n    return p;<br \/>\n}<br \/>\n\/\/ \u66f4\u65b0\u64cd\u4f5c<br \/>\nbool MySQL::update(string sql)<br \/>\n{<br \/>\n    if (mysql_query(_conn, sql.c_str()))<br \/>\n    {<br \/>\n        LOG_INFO &lt;&lt; __FILE__ &lt;&lt; &#034;:&#034; &lt;&lt; __LINE__ &lt;&lt; &#034;:&#034;<br \/>\n                 &lt;&lt; sql &lt;&lt; &#034;\u66f4\u65b0\u5931\u8d25!&#034;;<br \/>\n        return false;<br \/>\n    }<br \/>\n    return true;<br \/>\n}<br \/>\n\/\/ \u67e5\u8be2\u64cd\u4f5c<br \/>\nMYSQL_RES *MySQL::query(string sql)<br \/>\n{<br \/>\n    if (mysql_query(_conn, sql.c_str()))<br \/>\n    {<br \/>\n        LOG_INFO &lt;&lt; __FILE__ &lt;&lt; &#034;:&#034; &lt;&lt; __LINE__ &lt;&lt; &#034;:&#034;<br \/>\n                 &lt;&lt; sql &lt;&lt; &#034;\u67e5\u8be2\u5931\u8d25!&#034;;<br \/>\n        return nullptr;<br \/>\n    }<br \/>\n    return mysql_use_result(_conn);<br \/>\n}<\/p>\n<h2>\u4f7f\u7528ORM, \u5b9e\u73b0\u6ce8\u518c\u4e1a\u52a1<\/h2>\n<h3>\u7b2c\u4e00\u6b65:<\/h3>\n<p>\u9700\u8981\u5148\u5b9a\u4e49\u7c7b, \u8fd9\u4e2a\u7c7b \u9700\u8981\u8ddf \u6570\u636e\u5e93\u91cc\u7684\u8868 \u4e00\u4e00\u5bf9\u5e94<\/p>\n<p>sprintf \u662f\u4e00\u4e2a\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u51fd\u6570&#xff0c;\u5b83\u5c06\u683c\u5f0f\u5316\u7684\u6570\u636e\u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u7f13\u51b2\u533a&#xff0c;\u800c\u4e0d\u662f\u50cf printf \u90a3\u6837\u76f4\u63a5\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa&#xff08;stdout&#xff09;<\/p>\n<p>\u4ee5\u7528\u6237\u7c7b\u4e3a\u4f8b:<\/p>\n<p>include\/server\/user.hpp<\/p>\n<p>\/\/ \u6620\u5c04\u7c7b<br \/>\n\/\/ user\u7684 ORM\u7c7b, \u7528\u4e8e\u6620\u5c04\u5b57\u6bb5<\/p>\n<p>#ifndef USER_H<br \/>\n#define USER_H<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>class User<br \/>\n{<br \/>\npublic:<br \/>\n    User(int id&#061;-1, string name&#061;&#034;&#034;, string password &#061; &#034;&#034;, string state&#061;&#034;offline&#034;)<br \/>\n    {<br \/>\n        this-&gt;id &#061; id;<br \/>\n        this-&gt;name &#061; name;<br \/>\n        this-&gt;password&#061;password;<br \/>\n        this-&gt;state&#061;state;<br \/>\n    }<\/p>\n<p>    \/\/ \u4fee\u6539\u63a5\u53e3<br \/>\n    void setId(int id) {this-&gt;id &#061; id;}<br \/>\n    void setName(string name) {this-&gt;name &#061; name;}<br \/>\n    void setPwd(string pwd) {this-&gt;password &#061; pwd;}<br \/>\n    void setState(string state) {this-&gt;state &#061; state;}<\/p>\n<p>    \/\/ \u83b7\u53d6<br \/>\n    int getId() {return this-&gt;id ;}<br \/>\n    string getName() {return this-&gt;name;}<br \/>\n    string getPwd() {return this-&gt;password;}<br \/>\n    string getState() {return this-&gt;state;}<\/p>\n<p>private:<br \/>\n    int id;<br \/>\n    string name;<br \/>\n    string password;<br \/>\n    string state;<br \/>\n};<\/p>\n<p>#endif<\/p>\n<h3>\u7b2c\u4e8c\u6b65:<\/h3>\n<p>\u4fee\u6539 \u7528\u6237\u7c7b\u7684 \u5934\u6587\u4ef6<\/p>\n<p>include\/server\/usermodel.hpp<\/p>\n<p>\/\/ \u5b9e\u9645\u64cd\u4f5c \u8868 \u7684 \u7c7b&#8212;-\u589e\u5220\u6539\u67e5<\/p>\n<p>#ifndef USERMODEL_H<br \/>\n#define USERMODEL_H<\/p>\n<p>#include &#034;user.hpp&#034;<\/p>\n<p>\/\/User \u8868\u7684 \u6570\u636e\u64cd\u4f5c\u7c7b<br \/>\nclass UserModel<br \/>\n{<br \/>\n    public:<br \/>\n    \/\/user\u8868\u7684 \u589e\u52a0\u65b9\u6cd5<br \/>\n    bool insert(User &amp;user);<br \/>\n};<\/p>\n<p>#endif;<\/p>\n<h3>\u7b2c\u4e09\u6b65:<\/h3>\n<p>\u5b9a\u4e49 \u4fee\u6539 \u7c7b \u7684 \u51fd\u6570<\/p>\n<p>db.h \u4e0e db.cpp \u6dfb\u52a0 \u83b7\u53d6\u6570\u636e\u5e93\u5bf9\u8c61\u7684 \u65b9\u6cd5<\/p>\n<p>MYSQL *MySQL::getConnection()<br \/>\n{<br \/>\n    return _conn;<br \/>\n}<\/p>\n<p>src\/server\/usermodel.cpp<\/p>\n<p>#include &#034;usermodel.hpp&#034;<br \/>\n#include &#034;db.h&#034;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/user\u8868\u7684 \u589e\u52a0\u65b9\u6cd5<br \/>\nbool UserModel::insert(User &amp;user)<br \/>\n{<br \/>\n    \/\/1. \u7ec4\u88c5 sql\u8bed\u53e5<br \/>\n    char sql[1024]&#061;{0};<br \/>\n    sprintf(sql, &#034;insert into user(name, password, state) values(&#039;%s&#039;, &#039;%s&#039;, &#039;%s&#039;)&#034;, user.getName().c_str(), user.getPwd().c_str(), user.getState().c_str());  \/\/ \u6ce8\u610f %s, \u6bcf\u4e2a\u8981\u5355\u72ec&#039;&#039;\u8d77\u6765, \u6574\u4e2a&#039;&#039;\u662f\u8fde\u8d77\u6765\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32<\/p>\n<p>    MySQL mysql;<br \/>\n    if(mysql.connect())<br \/>\n    {<br \/>\n        if(mysql.update(sql))<br \/>\n        {<br \/>\n            \/\/\u83b7\u53d6\u63d2\u5165\u6210\u529f\u7684 \u7528\u6237\u6570\u636e\u751f\u6210\u7684 \u4e3b\u952eid<br \/>\n            user.setId(mysql_insert_id(mysql.getConnection()));<br \/>\n            return true;<br \/>\n        }<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<h3>\u9519\u8bef1:<\/h3>\n<p>sprintf(sql, &#034;insert into user(name, password, state) values(&#039;%s&#039;, &#039;%s&#039;, &#039;%s&#039;)&#034;, user.getName().c_str(), user.getPwd().c_str(), user.getState().c_str());<\/p>\n<p>insert into user(name, password, state) values(&#039;%s&#039;, &#039;%s&#039;, &#039;%s&#039;);  \/\/ \u8fd9\u662fmysql\u4e2d\u7684\u6dfb\u52a0\u8bed\u53e5, user\u4e0e\u8868\u540d\u5fc5\u987b\u4e00\u81f4!!<\/p>\n<h3>\u5c0f\u6280\u5de7\u2013\u7ea2\u8272\u6ce2\u6d6a\u7ebf\u95ee\u9898<\/h3>\n<p>\u5728vscode\u91cc, \u6709\u65f6\u5019cmake\u80fd\u6b63\u5e38\u7f16\u8bd1, \u4f46\u662f \u7f16\u5199\u4ee3\u7801 \u6709\u7684 \u4f1a\u51fa\u73b0 \u7ea2\u8272\u6ce2\u6d6a\u7ebf, \u5c3d\u7ba1\u5305\u542b\u4e86\u5934\u6587\u4ef6, \u5b83\u4e5f\u8fd8\u5728 , \u8fd9\u4e2a \u5bf9\u4e8e \u6709\u70b9\u5f3a\u8feb\u75c7\u7684, \u53ef\u5fcd\u53d7\u4e0d\u4e86!!!<\/p>\n<p>\u4fee\u6539\u4e00\u4e0b .vscode\u7684 json\u6587\u4ef6: c_cpp_properties.json<\/p>\n<p>&#034;includePath&#034;: [<br \/>\n                &#034;${workspaceFolder}\/**&#034;<br \/>\n    }<\/p>\n<p>\/\/ \u6539\u6210 \u4e0b\u9762\u8fd9\u4e2a, \u5177\u4f53\u770b\u81ea\u5df1\u7684 \u5934\u6587\u4ef6\u5728\u54ea<\/p>\n<p>&#034;includePath&#034;: [<br \/>\n                &#034;${workspaceFolder}\/**&#034;,<br \/>\n                &#034;${workspaceFolder}\/include\/**&#034;,<br \/>\n                &#034;${workspaceFolder}\/thirdparty\/**&#034;<br \/>\n            ],<\/p>\n<p>\u66f4\u7b80\u5355\u7684: \u8ba9 cmake \u7ba1\u7406 \u8def\u5f84<\/p>\n<p>&#034;configurationProvider&#034;: &#034;ms-vscode.cmake-tools&#034;  \/\/ \u5173\u952e&#xff01;\u8ba9CMake\u63a5\u7ba1\u914d\u7f6e<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u662f \u53f3\u4e0b\u89d2 \u4f1a\u6709 IntelliSense \u63d0\u793a \u672a\u914d\u7f6e, \u9ec4\u8272\u611f\u53f9\u53f7, \u53ef\u4ee5\u70b9\u51fb \u4ee5\u4e0b, \u9009\u62e9 cmake , \u4e5f\u80fd\u89e3\u51b3<\/p>\n<p>\u4e00\u5171\u4e09\u79cd \u65b9\u6cd5, \u90a3\u79cd\u80fd\u7528 \u7528\u54ea\u4e2a<\/p>\n<h3>\u8865\u5145\u5e76\u6d4b\u8bd5\u6ce8\u518c\u4e1a\u52a1<\/h3>\n<p>model \u7ed9 \u4e1a\u52a1 \u63d0\u4f9b\u7684 \u90fd\u662f \u5bf9\u8c61!!<\/p>\n<p>select * from user;   \/\/mysql\u91cc\u67e5\u770b user\u8868\u7684\u5168\u90e8\u4fe1\u606f<\/p>\n<p>\u7b2c\u4e00\u6b65: \u4e1a\u52a1\u4e2d\u6dfb\u52a0 \u6570\u636e\u64cd\u4f5c\u7c7b\u5bf9\u8c61<\/p>\n<p>chatservice.hpp<\/p>\n<p>private:<br \/>\n\/\/ \u6ce8\u518c\u4e1a\u52a1\u6d4b\u8bd5<br \/>\n    \/\/ \u6570\u636e\u64cd\u4f5c\u7c7b\u5bf9\u8c61<br \/>\n    UserModel _usermodel;<\/p>\n<p>\u7b2c\u4e8c\u6b65:\u8865\u5145 \u6ce8\u518c\u4e1a\u52a1\u4ee3\u7801<\/p>\n<p>src\/server\/chatservice.cpp<\/p>\n<p>\/\/ \u5904\u7406\u6ce8\u518c\u4e1a\u52a1<br \/>\nvoid ChatService::reg(const TcpConnectionPtr &amp;conn, json &amp;js, Timestamp time)<br \/>\n{<br \/>\n    \/\/ LOG_INFO&lt;&lt;&#034;do reg service&#034;; \u6d4b\u8bd5\u7528<\/p>\n<p>    \/\/ \u6ce8\u518c\u4ec5\u9700\u8981 name password<br \/>\n    string name &#061; js[&#034;name&#034;];<br \/>\n    string password &#061; js[&#034;password&#034;];<\/p>\n<p>    User user;<br \/>\n    user.setName(name);<br \/>\n    user.setPwd(password);<br \/>\n    bool state &#061; _usermodel.insert(user);<br \/>\n    if(state)<br \/>\n    {<br \/>\n        \/\/\u6ce8\u518c\u6210\u529f<br \/>\n        json response;<br \/>\n        response[&#034;msgid&#034;] &#061; REG_MSG_ACK;<br \/>\n        response[&#034;errno&#034;] &#061; 0;<br \/>\n        response[&#034;id&#034;] &#061; user.getId();<br \/>\n        conn-&gt;send(response.dump());<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        \/\/\u6ce8\u518c\u5931\u8d25<br \/>\n        json response;<br \/>\n        response[&#034;msgid&#034;] &#061; REG_MSG_ACK;<br \/>\n        response[&#034;errno&#034;] &#061; 1;<br \/>\n        conn-&gt;send(response.dump());<br \/>\n    }<\/p>\n<p>}<\/p>\n<h3>mysql\u8fde\u63a5\u5931\u8d25\u89e3\u51b3<\/h3>\n<ul>\n<li>auth_socket \u63d2\u4ef6\u53ea\u5141\u8bb8\u901a\u8fc7 Unix Socket \u672c\u5730\u8fde\u63a5&#xff08;\u4e0d\u80fd\u7528\u4e8e TCP\/IP \u6216\u8fdc\u7a0b\u8fde\u63a5&#xff09;<\/li>\n<li>\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4f7f\u7528 127.0.0.1&#xff08;TCP\/IP&#xff09;\u8fde\u63a5&#xff0c;auth_socket \u4f1a\u62d2\u7edd\u8ba4\u8bc1 &#xff08;\u5373\u4f7f\u662f\u5728\u672c\u673a&#xff0c;127.0.0.1 \u4e5f\u7b97 TCP\/IP \u8fde\u63a5&#xff0c;\u4e0d\u662f Unix Socket&#xff09;<\/li>\n<\/ul>\n<p>\u67e5\u770b\u5f53\u524d\u8ba4\u8bc1\u63d2\u4ef6&#xff1a;<\/p>\n<p>SELECT user, plugin FROM mysql.user;<\/p>\n<p>\u4fee\u6539 root \u8ba4\u8bc1\u65b9\u5f0f&#xff08;\u4e24\u79cd\u9009\u62e9&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6539\u4e3a mysql_native_password&#xff08;\u517c\u5bb9\u65e7\u5ba2\u6237\u7aef&#xff09;&#xff1a;<\/p>\n<p> ALTER USER &#039;root&#039;&#064;&#039;localhost&#039; IDENTIFIED WITH mysql_native_password BY &#039;123456&#039;;<br \/>\nFLUSH PRIVILEGES;\n <\/li>\n<\/ul>\n<h2>\u9636\u6bb5\u603b\u7ed3-1<\/h2>\n<p>\u81ea\u5df1\u7684 \u8bed\u8a00:<\/p>\n<p>db.h\u2014&gt; \u63d0\u4f9b \u6570\u636e\u5e93 \u57fa\u672c\u8fde\u63a5,\u67e5\u8be2\u7b49\u7684 \u58f0\u660e<\/p>\n<p>db.cpp\u2014&gt; \u63d0\u4f9bdb.h \u7684\u5177\u4f53\u5b9e\u73b0<\/p>\n<p>user.hpp \u2014&gt; \u6570\u636e\u5e93\u7684 \u6a21\u578b\u7c7b , \u7531\u4e8e\u5185\u5bb9\u5c11, \u76f4\u63a5 \u5305\u542b\u4e86 \u5177\u4f53\u5b9e\u73b0<\/p>\n<p>usermodel.hpp \u2014&gt; \u63d0\u4f9b \u7ed9 \u4e1a\u52a1 \u7684 \u5bf9\u8c61, \u4ec5\u5305\u542b \u64cd\u4f5c\u65b9\u6cd5\u7684 \u58f0\u660e<\/p>\n<p>usermodel.cpp \u2014&gt; \u5bf9 \u64cd\u4f5c\u65b9\u6cd5\u7684 \u5177\u4f53\u5b9e\u73b0<\/p>\n<p>\u5728 \u4e1a\u52a1\u6a21\u5757\u4e2d, \u4f8b\u5982 chatservice.hpp \u548c cpp, \u90fd\u662f \u4ec5\u63a5\u6536\u4e86 \u5bf9\u8c61, \u8c03\u7528 \u5bf9\u5e94\u65b9\u6cd5, \u6ca1\u6709\u8bbe\u8ba1 \u5177\u4f53\u5b9e\u73b0, \u8fbe\u5230\u4e86 \u89e3\u8026\u6027<\/p>\n<p>ai \u6da6\u8272:<\/p>\n<li>\u6570\u636e\u5e93\u57fa\u7840\u5c42\n<ul>\n<li>db.h&#xff1a;\u58f0\u660e\u6570\u636e\u5e93\u57fa\u672c\u8fde\u63a5\u3001\u67e5\u8be2\u7b49\u63a5\u53e3<\/li>\n<li>db.cpp&#xff1a;\u5b9e\u73b0\u6570\u636e\u5e93\u6838\u5fc3\u529f\u80fd\u7684\u5177\u4f53\u7ec6\u8282<\/li>\n<\/ul>\n<\/li>\n<li>\u6570\u636e\u6a21\u578b\u5c42\n<ul>\n<li>user.hpp&#xff1a;\u5b9a\u4e49\u4e0e\u6570\u636e\u5e93\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u7c7b&#xff08;\u91c7\u7528\u5934\u6587\u4ef6\u5b9e\u73b0\u6a21\u5f0f&#xff09;<\/li>\n<\/ul>\n<\/li>\n<li>\u4e1a\u52a1\u903b\u8f91\u5c42\n<ul>\n<li>usermodel.hpp&#xff1a;\u58f0\u660e\u9762\u5411\u4e1a\u52a1\u7684\u64cd\u4f5c\u63a5\u53e3<\/li>\n<li>usermodel.cpp&#xff1a;\u5b9e\u73b0\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<li>\u670d\u52a1\u5c42\n<ul>\n<li>chatservice.hpp\/cpp&#xff1a;\u901a\u8fc7\u63a5\u53e3\u8c03\u7528\u4e1a\u52a1\u65b9\u6cd5&#xff0c;\u5b8c\u5168\u9694\u79bb\u5177\u4f53\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u4e1a\u52a1\u6a21\u5757, \u6570\u636e\u5e93\u6a21\u5757, \u4f7f\u7528ORM\u6846\u67b6 \u5b9e\u73b0\u6ce8\u518c\u4e1a\u52a1, \u5e76\u8fdb\u884c\u6d4b\u8bd5<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,2382,43,78],"topic":[],"class_list":["post-29463","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-linux","tag-2382","tag-43","tag-78"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/29463.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u4e1a\u52a1\u6a21\u5757, \u6570\u636e\u5e93\u6a21\u5757, \u4f7f\u7528ORM\u6846\u67b6 \u5b9e\u73b0\u6ce8\u518c\u4e1a\u52a1, \u5e76\u8fdb\u884c\u6d4b\u8bd5\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/29463.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-20T01:25:59+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/29463.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/29463.html\",\"name\":\"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-20T01:25:59+00:00\",\"dateModified\":\"2025-04-20T01:25:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/29463.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/29463.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/29463.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/29463.html","og_locale":"zh_CN","og_type":"article","og_title":"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\uff0c\u70b9\u8d5e28\u6b21\uff0c\u6536\u85cf34\u6b21\u3002\u4e1a\u52a1\u6a21\u5757, \u6570\u636e\u5e93\u6a21\u5757, \u4f7f\u7528ORM\u6846\u67b6 \u5b9e\u73b0\u6ce8\u518c\u4e1a\u52a1, \u5e76\u8fdb\u884c\u6d4b\u8bd5","og_url":"https:\/\/www.wsisp.com\/helps\/29463.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-20T01:25:59+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/29463.html","url":"https:\/\/www.wsisp.com\/helps\/29463.html","name":"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-20T01:25:59+00:00","dateModified":"2025-04-20T01:25:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/29463.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/29463.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/29463.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u65bd\u78ca\u8001\u5e08\u57fa\u4e8emuduo\u7f51\u7edc\u5e93\u7684\u96c6\u7fa4\u804a\u5929\u670d\u52a1\u5668(\u4e09)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/29463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=29463"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/29463\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=29463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=29463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=29463"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=29463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}