{"id":98401,"date":"2020-03-25T21:39:12","date_gmt":"2020-03-25T13:39:12","guid":{"rendered":"http:\/\/4563.org\/?p=98401"},"modified":"2020-05-21T22:25:21","modified_gmt":"2020-05-21T14:25:21","slug":"golang-%e5%b0%86%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bd%ac%e6%8d%a2%e4%b8%ba-gorm-%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=98401","title":{"rendered":"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <span><i><\/i> 50<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>gormt<\/h1>\n<p> <\/p>\n<h3>\u4e00\u6b3e mysql \u6570\u636e\u5e93\u8f6c struct \u5de5\u5177<\/h3>\n<ul>\n<li>\u53ef\u4ee5\u5c06 mysql \u6570\u636e\u5e93\u81ea\u52a8\u751f\u6210 golang \u7ed3\u6784<\/li>\n<li>\u81ea\u52a8\u751f\u6210\u5feb\u6377\u64cd\u4f5c\u51fd\u6570<\/li>\n<li>\u652f\u6301\u7d22\u5f15,\u5916\u952e<\/li>\n<li>\u5e26\u5927\u9a7c\u5cf0\u547d\u540d\u89c4\u5219<\/li>\n<li>\u5e26 json \u6807\u7b7e<\/li>\n<\/ul>\n<h2><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200521_5ec68efca0f8f.gif\" alt=\"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784\" \/><\/h2>\n<h2>1. \u901a\u8fc7\u5f53\u524d\u76ee\u5f55 config.toml \u6587\u4ef6\u914d\u7f6e\u9ed8\u8ba4\u914d\u7f6e\u9879<\/h2>\n<pre><code>out_dir : \".\"  # \u8f93\u51fa\u76ee\u5f55 singular_table : false  # \u8868\u540d\u590d\u6570,\u662f\u5426\u5927\u9a7c\u5cf0\u6784\u5efa \u53c2\u8003:gorm.SingularTable simple : false #\u7b80\u5355\u8f93\u51fa is_out_sql : false # \u662f\u5426\u8f93\u51fa sql \u539f\u4fe1\u606f is_out_func : true # \u662f\u5426\u8f93\u51fa \u5feb\u6377\u51fd\u6570 is_json_tag : false #\u662f\u5426\u6253 json \u6807\u8bb0 is_foreign_key : true #\u662f\u5426\u5bfc\u51fa\u5916\u952e\u5173\u8054 mysql_info :     host : \"127.0.0.1\"     port : 3306     username : \"root\"     password : \"qwer\"     database : \"oauth_db\"  <\/code><\/pre>\n<h2>2. \u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u66f4\u65b0\u914d\u7f6e\u9879<\/h2>\n<pre><code>.\/gormt -H=127.0.0.1 -d=oauth_db -p=qwer -u=root --port=3306 <\/code><\/pre>\n<h2>3. \u67e5\u770b\u5e2e\u52a9<\/h2>\n<pre><code>.\/gormt -h  ------------------------------------------------------- base on gorm tools for mysql database to golang struct  Usage:   main [flags]  Flags:   -d, --database string   \u6570\u636e\u5e93\u540d   -h, --help              help for main   -H, --host string       \u6570\u636e\u5e93\u5730\u5740.(\u6ce8\u610f-H \u4e3a\u5927\u5199)   -o, --outdir string     \u8f93\u51fa\u76ee\u5f55   -p, --password string   \u5bc6\u7801.       --port int          \u7aef\u53e3\u53f7 (default 3306)   -s, --singular          \u662f\u5426\u7981\u7528\u8868\u540d\u590d\u6570   -u, --user string       \u7528\u6237\u540d.    <\/code><\/pre>\n<h2>4. \u652f\u6301 gorm \u76f8\u5173\u5c5e\u6027<\/h2>\n<ul>\n<li>\u6570\u636e\u5e93\u8868,\u5217\u5b57\u6bb5\u6ce8\u91ca\u652f\u6301<\/li>\n<li>singular_table \u8868\u540d\u590d\u6570(\u5927\u9a7c\u5cf0)<\/li>\n<li>json tag json \u6807\u7b7e\u8f93\u51fa<\/li>\n<li>gorm.Model \u57fa\u672c\u6a21\u578b \u652f\u6301 gorm.Model \u6a21\u5f0f\u5bfc\u51fa&gt;&gt;&gt;<\/li>\n<li>PRIMARY_KEY \u5c06\u5217\u6307\u5b9a\u4e3a\u4e3b\u952e<\/li>\n<li>UNIQUE \u5c06\u5217\u6307\u5b9a\u4e3a\u552f\u4e00<\/li>\n<li>NOT NULL \u5c06\u5217\u6307\u5b9a\u4e3a\u975e NULL<\/li>\n<li>INDEX \u521b\u5efa\u5177\u6709\u6216\u4e0d\u5e26\u540d\u79f0\u7684\u7d22\u5f15, \u5982\u679c\u591a\u4e2a\u7d22\u5f15\u540c\u540d\u5219\u521b\u5efa\u590d\u5408\u7d22\u5f15<\/li>\n<li>UNIQUE_INDEX \u548c INDEX \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u521b\u5efa\u7684\u662f\u552f\u4e00\u7d22\u5f15<\/li>\n<li>\u652f\u6301\u5916\u952e\u76f8\u5173\u5c5e\u6027 \u7b80\u5355\u5e26\u5916\u952e\u6a21\u5f0f\u5bfc\u51fa&gt;&gt;&gt;<\/li>\n<li>\u652f\u6301\u51fd\u6570\u5bfc\u51fa(\u5305\u62ec:\u5916\u952e\uff0c\u5173\u8054\u4f53\uff0c\u7d22\u5f15\u5173&#8230;)\u7b80\u5355\u51fd\u6570\u5bfc\u51fa\u793a\u4f8b&gt;&gt;&gt;<\/li>\n<\/ul>\n<h2>5. \u793a\u4f8b\u5c55\u793a<\/h2>\n<h6>&#8212;&gt;\u5bfc\u51fa\u7ed3\u679c\u793a\u4f8b<\/h6>\n<ul>\n<li>\u53c2\u6570:singular_table = false simple = false isJsonTag = true<\/li>\n<\/ul>\n<pre><code>\/\/ \u7528\u6237\u4fe1\u606f type UserAccountTbl struct {  ID          int       `gorm:\"primary_key;column:id;type:int(11);not null\" json:\"-\"`                                                   \/\/  Account     string    `gorm:\"unique;column:account;type:varchar(64);not null\" json:\"account\"`                                         \/\/  Password    string    `gorm:\"column:password;type:varchar(64);not null\" json:\"password\"`                                              \/\/  AccountType int       `gorm:\"column:account_type;type:int(11);not null\" json:\"account_type\"`                                          \/\/ \u5e10\u53f7\u7c7b\u578b:0 \u624b\u673a\u53f7\uff0c1 \u90ae\u4ef6  AppKey      string    `json:\"app_key\" gorm:\"unique_index:UNIQ_5696AD037D3656A4;column:app_key;type:varchar(255);not null\"`            \/\/ authbucket_oauth2_client \u8868\u7684 id  UserInfoID  int       `gorm:\"unique_index:UNIQ_5696AD037D3656A4;index;column:user_info_id;type:int(11);not null\" json:\"user_info_id\"` \/\/  RegTime     time.Time `gorm:\"column:reg_time;type:datetime\" json:\"reg_time\"`                                                          \/\/  RegIP       string    `gorm:\"column:reg_ip;type:varchar(15)\" json:\"reg_ip\"`                                                           \/\/  BundleID    string    `json:\"bundle_id\" gorm:\"column:bundle_id;type:varchar(255)\"`                                                    \/\/  Describ     string    `gorm:\"column:describ;type:varchar(255)\" json:\"describ\"`                                                        \/\/ } <\/code><\/pre>\n<ul>\n<li>\u53c2\u6570:singular_table = false simple = true isJsonTag = false<\/li>\n<\/ul>\n<h6>&#8212;&gt;\u5bfc\u51fa\u7ed3\u679c<\/h6>\n<pre><code>\/\/ \u7528\u6237\u4fe1\u606f type UserAccountTbl struct {  ID          int       `gorm:\"primary_key\"` \/\/  Account     string    `gorm:\"unique\"`      \/\/  Password    string    \/\/  AccountType int       \/\/ \u5e10\u53f7\u7c7b\u578b:0 \u624b\u673a\u53f7\uff0c1 \u90ae\u4ef6  AppKey      string    `gorm:\"unique_index:UNIQ_5696AD037D3656A4\"`       \/\/ authbucket_oauth2_client \u8868\u7684 id  UserInfoID  int       `gorm:\"unique_index:UNIQ_5696AD037D3656A4;index\"` \/\/  RegTime     time.Time \/\/  RegIP       string    \/\/  BundleID    string    \/\/  Describ     string    \/\/ } <\/code><\/pre>\n<h3>\u66f4\u591a&gt;&gt;&gt;<\/h3>\n<h2>6. \u652f\u6301\u51fd\u6570\u5bfc\u51fa(\u5bfc\u51fa\u51fd\u6570\u53ea\u662f gorm \u7684\u8f85\u52a9\u7c7b\u51fd\u6570\uff0c\u5b8c\u5168\u517c\u5bb9 gorm \u76f8\u5173\u51fd\u6570\u96c6)<\/h2>\n<pre><code>\/\/ FetchByPrimaryKey primay or index \u83b7\u53d6\u552f\u4e00\u5185\u5bb9 func (obj *_UserAccountTblMgr) FetchByPrimaryKey(ID int) (result UserAccountTbl, err error) {  err = obj.DB.Table(obj.GetTableName()).Where(\"id = ?\", ID).Find(&amp;result).Error  if err == nil &amp;&amp; obj.isRelated {   {    var info UserInfoTbl \/\/ \u7528\u6237\u4fe1\u606f    err = obj.DB.Table(\"user_info_tbl\").Where(\"id = ?\", result.UserInfoTblID).Find(&amp;info).Error    if err != nil {     return    }    result.UserInfoTbl = info   }  }   return }  <\/code><\/pre>\n<h3>\u66f4\u591a&gt;&gt;&gt;<\/h3>\n<h3>\u51fd\u6570\u8c03\u7528\u793a\u4f8b&gt;&gt;&gt;<\/h3>\n<h2>7. \u6784\u5efa<\/h2>\n<pre><code>make windows make linux make mac <\/code><\/pre>\n<p>or<\/p>\n<pre><code>go generate <\/code><\/pre>\n<h2>8. \u4e0b\u4e00\u6b65\u8ba1\u5212<\/h2>\n<ul>\n<li>\u66f4\u65b0\uff0c\u5220\u9664\u529f\u80fd\u51fd\u6570\u6dfb\u52a0<\/li>\n<li>\u4f18\u5316<\/li>\n<\/ul>\n<h2>9. \u63d0\u4f9b\u4e00\u4e2a windows \u53ef\u89c6\u5316\u5de5\u5177<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/94f50c21d4fa259c152f18c6da3dfe57.png\" alt=\"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784\" \/> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200521_5ec68f26898ba.jpg\" alt=\"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784\" \/> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200521_5ec68f3701730.jpg\" alt=\"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784\" \/> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200521_5ec68f4a5ab21.jpg\" alt=\"golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a gorm \u7ed3\u6784\" \/><\/p>\n<p>\u4e0b\u8f7d\u5730\u5740<\/p>\n<ul>\n<li>\n<h6>\u4f20\u9001\u95e8<\/h6>\n<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"709969\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : littlewing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u624b\u52a8\u70b9\u8d5e\uff0c\u4e4b\u524d\u7528\u8fc7\u4e00\u4e2a chrome \u63d2\u4ef6\uff0c\u73b0\u5728\u627e\u4e0d\u7740\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"709970\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"709971\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sun522198558 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c8c\u4f3c\u5f88\u65e9\u5c31\u70b9\u8d5e\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"709972\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sun522198558 \u8c22\u8c22\uff0c\u56e0\u4e3a\u6700\u8fd1\u66f4\u65b0\u4e86\u5f88\u591a\u5f88\u5b9e\u7528\u7684\u529f\u80fd\u3002\u6240\u4ee5\u53c8\u63a8\u5e7f\u4e00\u6ce2\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"709973\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sun522198558 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xie1xiao1jun #4 \u5927\u4f6c\u725b\u903c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"709974\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xie1xiao1jun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sun522198558 <\/p>\n<p>\u8fd9\u6b21\u4e3b\u8981\u6dfb\u52a0\u4e86\u5feb\u6377\u529f\u80fd\u51fd\u6570\u5bfc\u51fa\u529f\u80fd\u3002\u51fd\u6570\u8bf7\u770b\uff1a<br \/>https:\/\/github.com\/xxjwxc\/gormt\/blob\/master\/data\/view\/genfunc\/genfunc_test.go<\/p>\n<p>\u7edd\u5bf9\u5f00\u53d1\u4e2d\u975e\u5e38\u5b9e\u7528\u4e14\u65b9\u4fbf\u3002<\/p>\n<p>\u7d22\u5f15\uff0c\u5916\u952e\u90fd\u81ea\u52a8\u6784\u5efa\u51fd\u6570\u51fa\u6765\u3002\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p>last : \u4e0d\u662f\u5927\u4f6c\uff0c\u53ea\u662f\u4e3a\u4e86\u793e\u533a\u505a\u4e00\u70b9\u70b9\u8d21\u732e\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u5c06\u6570\u636e\u5e93\u8f6c\u6362\u4e3a go&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98401"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98401"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98401\/revisions"}],"predecessor-version":[{"id":98403,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98401\/revisions\/98403"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98401"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}