{"id":1119,"date":"2026-01-01T08:40:55","date_gmt":"2026-01-01T00:40:55","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1119"},"modified":"2026-01-01T08:40:55","modified_gmt":"2026-01-01T00:40:55","slug":"xorm%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/01\/xorm%e4%bb%a3%e7%a0%81\/","title":{"rendered":"xorm\u4ee3\u7801"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u5957<strong>\u5e38\u7528\u3001\u53ef\u76f4\u63a5\u7528\u7684 XORM\uff08Go ORM\uff09\u793a\u4f8b\u4ee3\u7801<\/strong>\uff0c\u8986\u76d6<strong>\u8fde\u63a5\u6570\u636e\u5e93\u3001\u5efa\u8868\u3001CRUD\u3001\u4e8b\u52a1\u3001\u6761\u4ef6\u67e5\u8be2<\/strong>\u7b49\u6838\u5fc3\u7528\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5b89\u88c5\u4f9d\u8d56<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngo get xorm.io\/xorm\ngo get github.com\/go-sql-driver\/mysql\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage main\n\nimport (\n\t&quot;fmt&quot;\n\t&quot;log&quot;\n\n\t_ &quot;github.com\/go-sql-driver\/mysql&quot;\n\t&quot;xorm.io\/xorm&quot;\n)\n\nvar engine *xorm.Engine\n\nfunc initDB() {\n\tvar err error\n\tdsn := &quot;user:password@tcp(127.0.0.1:3306)\/testdb?charset=utf8mb4&amp;amp;parseTime=true&quot;\n\tengine, err = xorm.NewEngine(&quot;mysql&quot;, dsn)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\t\/\/ \u663e\u793a SQL\uff08\u5f00\u53d1\u73af\u5883\u5efa\u8bae\u5f00\u542f\uff09\n\tengine.ShowSQL(true)\n\n\t\/\/ \u6d4b\u8bd5\u8fde\u63a5\n\tif err = engine.Ping(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc main() {\n\tinitDB()\n\tfmt.Println(&quot;DB connected&quot;)\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5b9a\u4e49\u7ed3\u6784\u4f53\uff08\u6620\u5c04\u6570\u636e\u8868\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype User struct {\n\tId        int64  `xorm:&quot;pk autoincr&quot;`\n\tUsername  string `xorm:&quot;varchar(50) notnull unique&quot;`\n\tEmail     string `xorm:&quot;varchar(100)&quot;`\n\tAge       int\n\tCreatedAt time.Time `xorm:&quot;created&quot;`\n\tUpdatedAt time.Time `xorm:&quot;updated&quot;`\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u81ea\u52a8\u5efa\u8868<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nerr := engine.Sync2(new(User))\nif err != nil {\n\tlog.Fatal(err)\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001CRUD \u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u65b0\u589e\u6570\u636e<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuser := &amp;amp;User{\n\tUsername: &quot;ajie&quot;,\n\tEmail:    &quot;ajie@test.com&quot;,\n\tAge:      25,\n}\n\n_, err := engine.Insert(user)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u67e5\u8be2\u4e00\u6761<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar user User\nhas, err := engine.Where(&quot;username = ?&quot;, &quot;ajie&quot;).Get(&amp;amp;user)\nif has {\n\tfmt.Println(user)\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 \u67e5\u8be2\u591a\u6761<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar users &#x5B;]User\nerr := engine.Where(&quot;age &gt; ?&quot;, 18).Limit(10).Find(&amp;amp;users)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4\ufe0f\u20e3 \u66f4\u65b0\u6570\u636e<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n_, err := engine.ID(1).Update(&amp;amp;User{Email: &quot;new@test.com&quot;})\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5220\u9664\u6570\u636e<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n_, err := engine.ID(1).Delete(&amp;amp;User{})\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u4e8b\u52a1\u793a\u4f8b\uff08\u975e\u5e38\u91cd\u8981\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsession := engine.NewSession()\ndefer session.Close()\n\nif err := session.Begin(); err != nil {\n\treturn\n}\n\n_, err := session.Insert(&amp;amp;User{Username: &quot;u1&quot;})\nif err != nil {\n\tsession.Rollback()\n\treturn\n}\n\n_, err = session.Insert(&amp;amp;User{Username: &quot;u2&quot;})\nif err != nil {\n\tsession.Rollback()\n\treturn\n}\n\nsession.Commit()\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5e38\u7528\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6761\u4ef6\u7ec4\u5408<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nengine.Where(&quot;age &gt; ?&quot;, 18).\n\tAnd(&quot;email like ?&quot;, &quot;%@test.com&quot;).\n\tDesc(&quot;id&quot;).\n\tFind(&amp;amp;users)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u53ea\u66f4\u65b0\u975e\u7a7a\u5b57\u6bb5<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nengine.ID(1).Cols(&quot;email&quot;).Update(&amp;amp;user)\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u539f\u751f SQL<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nengine.SQL(&quot;SELECT * FROM user WHERE age &gt; ?&quot;, 20).Find(&amp;amp;users)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u9002\u5408\u54ea\u4e9b\u9879\u76ee\uff1f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 <strong>\u4e2d\u5c0f\u578b Go \u9879\u76ee<\/strong><\/li>\n\n\n\n<li>\u2705 \u7ba1\u7406\u7cfb\u7edf \/ \u540e\u53f0\u63a5\u53e3<\/li>\n\n\n\n<li>\u2705 Spring Boot + MySQL \u9879\u76ee\u8fc1\u79fb\u5230 Go\uff08\u4f60\u4e4b\u524d\u505a\u8fc7\u7c7b\u4f3c\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u613f\u610f\uff0c\u6211\u53ef\u4ee5\u5e2e\u4f60\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83d\udd39 \u5199 <strong>\u5b8c\u6574 MVC \/ REST API \u793a\u4f8b<\/strong><\/li>\n\n\n\n<li>\ud83d\udd39 \u7ed9\u4f60 <strong>XORM vs GORM \u5bf9\u6bd4<\/strong><\/li>\n\n\n\n<li>\ud83d\udd39 \u7ed3\u5408 <strong>Gin + XORM \u5b9e\u6218\u9879\u76ee\u7ed3\u6784<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u662f\u6253\u7b97\u7528\u5728 <strong>\u4ec0\u4e48\u9879\u76ee<\/strong>\u91cc\uff1f\u6211\u53ef\u4ee5\u76f4\u63a5\u6309\u4f60\u7684\u573a\u666f\u7ed9\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u5957\u5e38\u7528\u3001\u53ef\u76f4\u63a5\u7528\u7684 XORM\uff08Go ORM\uff09\u793a\u4f8b\u4ee3\u7801\uff0c\u8986\u76d6\u8fde\u63a5\u6570\u636e\u5e93\u3001&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/01\/xorm%e4%bb%a3%e7%a0%81\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-1"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":1120,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1119\/revisions\/1120"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}