{"id":977,"date":"2025-01-16T00:30:15","date_gmt":"2025-01-15T16:30:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=977"},"modified":"2025-01-16T00:30:15","modified_gmt":"2025-01-15T16:30:15","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8e%9f%e5%a7%8b-sql-%e5%92%8c-orm-%e9%98%b2%e6%ad%a2-sql-%e6%b3%a8%e5%85%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/16\/%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8e%9f%e5%a7%8b-sql-%e5%92%8c-orm-%e9%98%b2%e6%ad%a2-sql-%e6%b3%a8%e5%85%a5%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528\u539f\u59cb SQL \u548c ORM \u9632\u6b62 SQL \u6ce8\u5165\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Golang \u4e2d\u9632\u6b62 SQL \u6ce8\u5165\u7684\u65b9\u6cd5\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4e24\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u539f\u59cb SQL \u548c\u4f7f\u7528 ORM\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u5206\u6790\u548c\u9632\u62a4\u63aa\u65bd\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u4f7f\u7528\u539f\u59cb SQL \u9632\u6b62 SQL \u6ce8\u5165<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u539f\u59cb SQL \u67e5\u8be2\u65f6\uff0cSQL \u6ce8\u5165\u7684\u98ce\u9669\u8f83\u5927\uff0c\u56e0\u4e3a\u6076\u610f\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4e0d\u53d7\u63a7\u5236\u7684\u8f93\u5165\u5c06\u6076\u610f\u4ee3\u7801\u6ce8\u5165\u5230 SQL \u8bed\u53e5\u4e2d\u3002\u4e3a\u4e86\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u5728\u4f7f\u7528\u539f\u59cb SQL \u65f6\u9700\u8981\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff08Prepared Statements\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u6570\u5316\u67e5\u8be2\u662f\u4e00\u79cd\u901a\u8fc7\u7ed1\u5b9a\u53c2\u6570\u6765\u6267\u884c SQL \u67e5\u8be2\u7684\u6280\u672f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u80fd\u6709\u6548\u5730\u9632\u6b62 SQL \u6ce8\u5165\u3002\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u65f6\uff0c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u4e0d\u4f1a\u76f4\u63a5\u5d4c\u5165 SQL \u8bed\u53e5\u4e2d\uff0c\u800c\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u8f93\u5165\uff0c\u4ece\u800c\u907f\u514d\u4e86\u6076\u610f SQL \u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Golang \u4e2d\u7684 <code>database\/sql<\/code> \u5305\u652f\u6301\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u5e38\u89c1\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import (\n    \"database\/sql\"\n    \"log\"\n    _ \"github.com\/go-sql-driver\/mysql\"\n)\n\nfunc getUserByID(db *sql.DB, userID int) (*User, error) {\n    query := \"SELECT id, name FROM users WHERE id = ?\"\n    row := db.QueryRow(query, userID) \/\/ \u4f7f\u7528 ? \u5360\u4f4d\u7b26\n    var user User\n    if err := row.Scan(&amp;user.ID, &amp;user.Name); err != nil {\n        return nil, err\n    }\n    return &amp;user, nil\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u4ee3\u7801\u4e2d\uff0c<code>QueryRow<\/code> \u51fd\u6570\u4f1a\u81ea\u52a8\u5904\u7406\u53c2\u6570 <code>userID<\/code>\uff0c\u4ece\u800c\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4f7f\u7528\u4e8b\u52a1\uff08Transaction\uff09\u907f\u514d\u6570\u636e\u7be1\u6539<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6d89\u53ca\u591a\u4e2a\u67e5\u8be2\u64cd\u4f5c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u6570\u636e\u7684\u539f\u5b50\u6027\u548c\u4e00\u81f4\u6027\u3002\u901a\u8fc7\u4e8b\u52a1\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e00\u7ec4 SQL \u67e5\u8be2\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\uff0c\u4ece\u800c\u907f\u514d\u6076\u610f\u7528\u6237\u901a\u8fc7\u4e2d\u65ad\u6216\u7be1\u6539\u5355\u4e2a SQL \u67e5\u8be2\u6765\u7834\u574f\u64cd\u4f5c\u7684\u5b8c\u6574\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tx, err := db.Begin()\nif err != nil {\n    log.Fatal(err)\n}\n\n_, err = tx.Exec(\"UPDATE users SET name = ? WHERE id = ?\", newName, userID)\nif err != nil {\n    tx.Rollback() \/\/ \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda\u4e8b\u52a1\n    log.Fatal(err)\n}\n\ntx.Commit() \/\/ \u63d0\u4ea4\u4e8b\u52a1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u4f7f\u7528 ORM \u9632\u6b62 SQL \u6ce8\u5165<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u901a\u8fc7\u5c01\u88c5 SQL \u64cd\u4f5c\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u66f4\u9ad8\u7ea7\u522b\u7684\u63a5\u53e3\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002\u4f7f\u7528 ORM \u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u56e0\u4e3a ORM \u81ea\u52a8\u5904\u7406\u4e86\u67e5\u8be2\u7684\u53c2\u6570\u5316\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Golang \u4e2d\u5e38\u7528\u7684 ORM \u6846\u67b6\u6709 <code>GORM<\/code> \u548c <code>beego ORM<\/code>\uff0c\u5b83\u4eec\u90fd\u5185\u5efa\u9632\u6b62 SQL \u6ce8\u5165\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. GORM \u7684\u9632\u6ce8\u5165\u673a\u5236<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">GORM \u662f Golang \u6700\u6d41\u884c\u7684 ORM \u5e93\u4e4b\u4e00\uff0c\u5b83\u901a\u8fc7\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\uff08method chaining\uff09\u548c\u7ed3\u6784\u4f53\u6765\u6784\u5efa SQL \u67e5\u8be2\uff0c\u907f\u514d\u4e86\u76f4\u63a5\u62fc\u63a5 SQL \u5b57\u7b26\u4e32\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5c31\u80fd\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import (\n    \"gorm.io\/gorm\"\n    \"gorm.io\/driver\/mysql\"\n    \"log\"\n)\n\ntype User struct {\n    ID   int\n    Name string\n}\n\nfunc getUserByID(db *gorm.DB, userID int) (*User, error) {\n    var user User\n    if err := db.Where(\"id = ?\", userID).First(&amp;user).Error; err != nil {\n        return nil, err\n    }\n    return &amp;user, nil\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Where(\"id = ?\", userID)<\/code> \u4f7f\u7528\u4e86\u53c2\u6570\u5316\u67e5\u8be2\uff0c<code>userID<\/code> \u88ab\u5b89\u5168\u5730\u4f5c\u4e3a\u67e5\u8be2\u53c2\u6570\u4f20\u9012\uff0c\u9632\u6b62\u4e86 SQL \u6ce8\u5165\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4f7f\u7528\u6a21\u578b\uff08Struct\uff09\u8fdb\u884c\u67e5\u8be2<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">ORM \u8fd8\u652f\u6301\u901a\u8fc7\u6a21\u578b\u76f4\u63a5\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u8fd9\u610f\u5473\u7740\u65e0\u9700\u7f16\u5199 SQL \u8bed\u53e5\uff0cORM \u4f1a\u6839\u636e\u7ed3\u6784\u4f53\u81ea\u52a8\u6784\u5efa\u67e5\u8be2\uff0c\u4ece\u800c\u907f\u514d SQL \u6ce8\u5165\u7684\u98ce\u9669\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var user User\nif err := db.First(&amp;user, userID).Error; err != nil {\n    log.Fatal(err)\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u59cb SQL<\/strong>\uff1a\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff08Prepared Statements\uff09\u548c\u4e8b\u52a1\u6765\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/li>\n\n\n\n<li><strong>ORM<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528\u50cf GORM \u7b49 ORM \u6846\u67b6\uff0c\u5f00\u53d1\u8005\u65e0\u9700\u76f4\u63a5\u7f16\u5199 SQL \u8bed\u53e5\uff0cORM \u4f1a\u81ea\u52a8\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e24\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u59cb SQL<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u67e5\u8be2\u6216\u5bf9\u6027\u80fd\u6709\u4e25\u683c\u8981\u6c42\u7684\u573a\u666f\uff0c\u4f46\u9700\u8981\u5f00\u53d1\u8005\u5c0f\u5fc3\u7f16\u5199 SQL\u3002<\/li>\n\n\n\n<li><strong>ORM<\/strong>\uff1a\u9002\u5408\u5f00\u53d1\u8005\u5feb\u901f\u5f00\u53d1\uff0c\u51cf\u5c11 SQL \u9519\u8bef\u548c\u6ce8\u5165\u98ce\u9669\uff0c\u4f46\u53ef\u80fd\u5728\u6027\u80fd\u4e0a\u7565\u900a\u8272\u4e8e\u624b\u5199\u7684\u539f\u59cb SQL\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Golang \u4e2d\u9632\u6b62 SQL \u6ce8\u5165\u7684\u65b9\u6cd5\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4e24\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u539f\u59cb SQL \u548c\u4f7f\u7528 ORM\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e24\u79cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-977","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=977"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/977\/revisions"}],"predecessor-version":[{"id":978,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/977\/revisions\/978"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}