Explorar el Código

自动生成实体类和dao

gujiheimao hace 2 años
commit
2d39c90569
Se han modificado 9 ficheros con 591 adiciones y 0 borrados
  1. 8 0
      .idea/.gitignore
  2. 9 0
      .idea/go-create.iml
  3. 8 0
      .idea/modules.xml
  4. 118 0
      configs/config.go
  5. 9 0
      configs/config.yaml
  6. 42 0
      go.mod
  7. 129 0
      go.sum
  8. 13 0
      main.go
  9. 255 0
      util/MysqlToStruct.go

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 9 - 0
.idea/go-create.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/go-create.iml" filepath="$PROJECT_DIR$/.idea/go-create.iml" />
+    </modules>
+  </component>
+</project>

+ 118 - 0
configs/config.go

@@ -0,0 +1,118 @@
+package configs
+
+import (
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	_ "github.com/go-sql-driver/mysql"
+	"github.com/spf13/viper"
+	"os"
+	"xorm.io/xorm"
+)
+
+var (
+	Config  *viper.Viper
+	Engine  *xorm.Engine
+	RedisDb *redis.Client
+)
+
+type User struct {
+	Id            int    `xorm:"'id'" json:"id"`
+	Username      string `xorm:"'username'" json:"username"`
+	Password      string `xorm:"'password'" json:"password"`
+	Creation_time int    `xorm:"'creation_time'" json:"creation_Time"`
+	Login_time    int    `xorm:"'login_time'" json:"login_Time"`
+	Status        int    `xorm:"'status'" json:"status"`
+	Role_id       int    `xorm:"'role_id'" json:"role_id"`
+	Phone         string `xorm:"'phone'" json:"phone"`
+	Email         string `xorm:"'email'" json:"email"`
+	Name          string `xorm:"'name'" json:"name"`
+	Avatar        string `xorm:"'avatar'" json:"avatar"`
+	RecommendCode string `xorm:"'recommend_code'" json:"recommend_code"`
+}
+
+func (receiver User) String() string {
+	return fmt.Sprintf("User{Id:%d, Username:%s, Password:%s, Creation_time:%d, Login_time:%d, Status:%d, Role_id:%d, Phone:%s, Email:%s, Name:%s, Avatar:%s, RecommendCode:%s}", receiver.Id, receiver.Username, receiver.Password, receiver.Creation_time, receiver.Login_time, receiver.Status, receiver.Role_id, receiver.Phone, receiver.Email, receiver.Name, receiver.Avatar, receiver.RecommendCode)
+}
+
+type Role struct {
+	Id        int    `xorm:"'id'" json:"id"`
+	Role_name string `xorm:"'role_name'" json:"role_Name"`
+}
+type Role_authority struct {
+	Authority_id int `xorm:"'authority_id'" json:"authority_id"`
+	Role_id      int `xorm:"'role_id'" json:"role_id"`
+}
+type Authority struct {
+	Id             int    `xorm:"'id'" json:"id"`
+	Authority_name string `xorm:"'authority_name'" json:"authority_Name"`
+	Authority_path string `xorm:"'authority_path'" json:"authority_Path"`
+}
+type MysqlData struct {
+	User           `xorm:"extends"`
+	Role           `xorm:"extends"`
+	Role_authority `xorm:"extends"`
+	Authority      `xorm:"extends"`
+}
+
+func ConfigInit() {
+	//获取项目的执行路径
+	path, err := os.Getwd()
+	if err != nil {
+		panic(err)
+	}
+	config := viper.New()
+
+	config.AddConfigPath(path + "\\configs") //设置读取的文件路径
+	config.SetConfigName("config")           //设置读取的文件名
+	config.SetConfigType("yaml")             //设置文件的类型
+	//尝试进行配置读取
+	if err := config.ReadInConfig(); err != nil {
+		panic(err)
+	}
+	fmt.Println(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
+	Engine, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
+	Engine.Ping() //连接测试
+	//TODO  显示sql
+	Engine.ShowSQL(true)
+	Engine.Logger().ShowSQL(true)
+	RedisDb = redis.NewClient(&redis.Options{
+		Addr:     fmt.Sprintf("%s:%s", config.Get("redis.host"), config.Get("redis.port")), // Redis地址
+		Password: "",                                                                       // Redis密码,如果没有则为空字符串
+		DB:       0,                                                                        // 使用默认DB
+	})
+	//开启debug模式
+
+	Config = config
+	fmt.Println("xorm 数据库orm框架初始化成功")
+}
+func ConfigInitByUrl(configUrl string) {
+	//获取项目的执行路径
+
+	config := viper.New()
+
+	config.AddConfigPath(configUrl + "\\configs") //设置读取的文件路径
+	config.SetConfigName("config")                //设置读取的文件名
+	config.SetConfigType("yaml")                  //设置文件的类型
+	//尝试进行配置读取
+	if err := config.ReadInConfig(); err != nil {
+		panic(err)
+	}
+	fmt.Println(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
+	Engine, _ := xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
+	Engine.Ping() //连接测试
+	//TODO  显示sql
+	Engine.ShowSQL(true)
+	Engine.Logger().ShowSQL(true)
+	RedisDb = redis.NewClient(&redis.Options{
+		Addr:     fmt.Sprintf("%s:%s", config.Get("redis.host"), config.Get("redis.port")), // Redis地址
+		Password: "",                                                                       // Redis密码,如果没有则为空字符串
+		DB:       0,                                                                        // 使用默认DB
+	})
+	//开启debug模式
+
+	Config = config
+	fmt.Println("xorm 数据库orm框架初始化成功")
+}
+func GoGormConfigInit() {
+
+}

+ 9 - 0
configs/config.yaml

@@ -0,0 +1,9 @@
+mysql:
+    host: localhost
+    port: "3306"
+    user: root
+    password: "root"
+    database: virtual_mall
+redis:
+    host: localhost
+    port: "6379"

+ 42 - 0
go.mod

@@ -0,0 +1,42 @@
+module go-create
+
+go 1.19
+
+require (
+	github.com/go-redis/redis/v8 v8.11.5
+	github.com/go-sql-driver/mysql v1.8.1
+	github.com/spf13/viper v1.18.2
+	xorm.io/xorm v1.3.9
+)
+
+require (
+	filippo.io/edwards25519 v1.1.0 // indirect
+	github.com/cespare/xxhash/v2 v2.1.2 // indirect
+	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+	github.com/fsnotify/fsnotify v1.7.0 // indirect
+	github.com/goccy/go-json v0.8.1 // indirect
+	github.com/golang/snappy v0.0.4 // indirect
+	github.com/hashicorp/hcl v1.0.0 // indirect
+	github.com/json-iterator/go v1.1.12 // indirect
+	github.com/magiconair/properties v1.8.7 // indirect
+	github.com/mitchellh/mapstructure v1.5.0 // indirect
+	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
+	github.com/modern-go/reflect2 v1.0.2 // indirect
+	github.com/pelletier/go-toml/v2 v2.1.0 // indirect
+	github.com/sagikazarmark/locafero v0.4.0 // indirect
+	github.com/sagikazarmark/slog-shim v0.1.0 // indirect
+	github.com/sourcegraph/conc v0.3.0 // indirect
+	github.com/spf13/afero v1.11.0 // indirect
+	github.com/spf13/cast v1.6.0 // indirect
+	github.com/spf13/pflag v1.0.5 // indirect
+	github.com/subosito/gotenv v1.6.0 // indirect
+	github.com/syndtr/goleveldb v1.0.0 // indirect
+	go.uber.org/atomic v1.9.0 // indirect
+	go.uber.org/multierr v1.9.0 // indirect
+	golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
+	golang.org/x/sys v0.15.0 // indirect
+	golang.org/x/text v0.14.0 // indirect
+	gopkg.in/ini.v1 v1.67.0 // indirect
+	gopkg.in/yaml.v3 v3.0.1 // indirect
+	xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
+)

+ 129 - 0
go.sum

@@ -0,0 +1,129 @@
+filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
+filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
+gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
+gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
+github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
+github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
+github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
+github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
+github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
+github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
+github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
+github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
+github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
+github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
+github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
+github.com/goccy/go-json v0.8.1 h1:4/Wjm0JIJaTDm8K1KcGrLHJoa8EsJ13YWeX+6Kfq6uI=
+github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
+github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
+github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
+github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
+github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
+github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
+github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
+github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
+github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
+github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
+github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
+github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
+github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
+github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
+github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
+github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
+github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
+github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
+github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
+github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
+github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
+github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
+github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
+github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
+github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
+github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
+github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
+github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
+github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
+github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
+github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
+github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
+github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
+github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
+github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
+github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
+github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
+github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
+github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
+github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
+github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
+github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
+go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
+go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
+go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
+go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
+golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
+golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
+golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
+golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
+golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
+golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
+golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
+gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
+gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
+gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
+gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
+gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
+gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
+modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw=
+modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw=
+modernc.org/libc v1.22.2 h1:4U7v51GyhlWqQmwCHj28Rdq2Yzwk55ovjFrdPjs8Hb0=
+modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
+modernc.org/memory v1.4.0 h1:crykUfNSnMAXaOJnnxcSzbUGMqkLWjklJKkBK2nwZwk=
+modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
+modernc.org/sqlite v1.20.4 h1:J8+m2trkN+KKoE7jglyHYYYiaq5xmz2HoHJIiBlRzbE=
+modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
+modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg=
+xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 h1:bvLlAPW1ZMTWA32LuZMBEGHAUOcATZjzHcotf3SWweM=
+xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
+xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU=
+xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw=

+ 13 - 0
main.go

@@ -0,0 +1,13 @@
+package main
+
+import (
+	"go-create/configs"
+	"go-create/util"
+)
+
+func init() {
+	configs.ConfigInit()
+}
+func main() {
+	util.MysqlToDao(configs.Engine)
+}

+ 255 - 0
util/MysqlToStruct.go

@@ -0,0 +1,255 @@
+package util
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strings"
+	"xorm.io/xorm"
+)
+
+type MTable struct {
+	Name     string
+	AaBbName string
+	aaBbName string
+	Comment  string
+	existId  bool
+	MColumns []MColumn
+}
+type MColumn struct {
+	Name        string //aa_bb
+	AaBbName    string
+	aaBbName    string
+	Comment     string
+	SqlTypeName string
+	TypeName    string
+	Length      int64
+	Length2     int64
+	IsKey       bool
+}
+
+func (receiver MTable) String() string {
+	return fmt.Sprintf("Name:%s,Comment:%s,MColumns:%s\n\n\n", receiver.Name, receiver.Comment, receiver.MColumns)
+}
+func (receiver MColumn) String() string {
+	return fmt.Sprintf("Name:%s,AaBbName:%s,aaBbName:%s,Comment:%s,SqlTypeName:%s,TypeName:%s,Length:%d,Length2:%d\n",
+		receiver.Name, receiver.AaBbName, receiver.aaBbName, receiver.Comment, receiver.SqlTypeName, receiver.TypeName, receiver.Length, receiver.Length2)
+}
+
+var MTables = make([]MTable, 0)
+
+func InitMTableArray(engine *xorm.Engine) {
+	metas, err := engine.DBMetas()
+	if err != nil {
+		fmt.Println(err)
+	}
+	for _, meta := range metas {
+		var mTable MTable
+		mTable.Name = meta.Name
+		mTable.AaBbName, mTable.aaBbName = formatName(meta.Name)
+		mTable.Comment = meta.Comment
+
+		var cols = meta.Columns()
+		for i := range cols {
+			var mColumn MColumn
+			mColumn.Name = cols[i].Name
+			mColumn.AaBbName, mColumn.aaBbName = formatName(cols[i].Name)
+			mColumn.IsKey = cols[i].IsPrimaryKey
+
+			if cols[i].IsPrimaryKey && cols[i].IsAutoIncrement {
+				mTable.existId = true
+			}
+			mColumn.Comment = cols[i].Comment
+			mColumn.SqlTypeName = cols[i].SQLType.Name
+			mColumn.TypeName = formatSqlType(mColumn.SqlTypeName)
+			mColumn.Length = cols[i].Length
+			mColumn.Length2 = cols[i].Length2
+			mTable.MColumns = append(mTable.MColumns, mColumn)
+
+		}
+		MTables = append(MTables, mTable)
+		fmt.Println(mTable)
+	}
+}
+
+func formatName(name string) (string, string) {
+	split := strings.Split(strings.ToLower(name), "_")
+	if len(split) == 1 {
+		return strings.ToUpper(split[0][0:1]) + split[0][1:], split[0]
+	} else {
+		AaBb := ""
+		aaBb := ""
+		for j := range split {
+			AaBb += strings.ToUpper(split[j][0:1]) + split[j][1:]
+			if j == 0 {
+				aaBb += split[j]
+			} else {
+				aaBb += strings.ToUpper(split[j][0:1]) + split[j][1:]
+			}
+		}
+		return AaBb, aaBb
+	}
+}
+
+func formatSqlType(str string) string {
+	str = strings.ToLower(str)
+	switch str {
+	case "varchar":
+		return "string"
+	case "int":
+		return "int"
+	case "decimal":
+		return "float64"
+	case "datetime":
+		return "time.Time"
+	case "longblob":
+		return "string"
+	default:
+		panic("未定义的类型:" + str)
+	}
+}
+
+//----------------------------------------------------------
+
+// MysqlToStruct 根据mysql 生成对应的struct及String函数
+func MysqlToStruct(engine *xorm.Engine) {
+	InitMTableArray(engine)
+	var structString = ""
+	for _, mtable := range MTables {
+		structString += fmt.Sprint("//", mtable.AaBbName, " ", mtable.Comment, "\n",
+			"type ", mtable.AaBbName, " struct {\n")
+
+		cols := mtable.MColumns
+		var toString = ""
+		for i := range cols {
+			if cols[i].aaBbName == "id" && cols[i].IsKey {
+				structString += fmt.Sprint("    ", cols[i].AaBbName, " ", cols[i].TypeName, " `json:\"", cols[i].aaBbName, "\"`",
+					"// ", cols[i].Length, " ", cols[i].Length2, " 注释:", cols[i].Comment, "\n")
+				toString += fmt.Sprint(cols[i].AaBbName, ":\", receiver.", cols[i].AaBbName, ", \",")
+
+			} else {
+				structString += fmt.Sprint("    ", cols[i].AaBbName, " ", cols[i].TypeName, " `xorm:\"", cols[i].Name, "\"  json:\"", cols[i].aaBbName, "\"`",
+					"// ", cols[i].Length, " ", cols[i].Length2, " 注释:", cols[i].Comment, "\n")
+				toString += fmt.Sprint(cols[i].AaBbName, ":\", receiver.", cols[i].AaBbName, ", \",")
+			}
+		}
+		structString += "}\n\n"
+		structString += fmt.Sprint("func (receiver ", mtable.AaBbName, ") String() string {\n")
+		structString += fmt.Sprint("    return fmt.Sprint(\"", mtable.AaBbName, "{", toString[0:len(toString)-1], "}\")\n}\n\n")
+		fmt.Println(structString)
+	}
+	fileUrl := "E:\\project\\kkc\\kkc-go\\data\\domain\\DoMain.go"
+	//清空文件
+	os.Truncate(fileUrl, 0)
+	file, _ := os.OpenFile(fileUrl, os.O_CREATE|os.O_RDWR, 0666)
+	defer file.Close()
+
+	writer := bufio.NewWriter(file)
+	writer.WriteString("package domain\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\n")
+	writer.WriteString(structString)
+	writer.Flush()
+
+	//file.Write([]byte("package domain\n\nimport \"time\"\n"))
+	//file.Write([]byte(structString))
+}
+
+func MysqlToDao(engine *xorm.Engine) {
+	InitMTableArray(engine)
+	var baseUrl = "E:\\project\\kkc\\kkc-go\\data\\dao\\manage\\"
+	tables := MTables
+	for _, table := range tables {
+		var dao = ""
+		fileName := table.AaBbName + "Dao.go"
+		dao += "package manage\n\nimport (\n\t\"demo/configs\"\n\t\"demo/data/domain\"\n\t\"demo/data/domain/vo\"\n)\n"
+		//get
+		if table.Name == "role_authority" {
+			fmt.Println(table.existId, "====================>")
+		}
+		var idColumn MColumn
+		for _, column := range table.MColumns {
+			if column.IsKey {
+				idColumn = column
+				break
+			}
+		}
+
+		if table.existId {
+			dao += "func GetById" + table.AaBbName + "(id int) (domain." + table.AaBbName + ", error) {" +
+				"\n\tvar " + table.aaBbName + " domain." + table.AaBbName + "" +
+				"\n\t_, err := configs.Engine.Where(\"id = ?\", id).Get(&" + table.aaBbName + ")" +
+				"\n\tif err != nil {" +
+				"\n\t\treturn " + table.aaBbName + ", err" +
+				"\n\t}" +
+				"\n\treturn " + table.aaBbName + ", nil" +
+				"\n}\n\n"
+		}
+		dao += "func GetInIdList" + table.AaBbName + "(ids []int) ([]domain." + table.AaBbName + ", error) {" +
+			"\n\trows, err := configs.Engine.In(\"id\", ids).Rows(&domain." + table.AaBbName + "{})" +
+			"\n\tif err != nil {" +
+			"\n\t\treturn make([]domain." + table.AaBbName + ", 0), err" +
+			"\n\t}" +
+			"\n\tdefer rows.Close()" +
+			"\n\tvar list []domain." + table.AaBbName + "" +
+			"\n\tfor rows.Next() {" +
+			"\n\t\tvar u domain." + table.AaBbName + "" +
+			"\n\t\tlist = append(list, u)" +
+			"\n\t}" +
+			"\n\treturn list, nil" +
+			"\n}\n\n"
+
+		//list
+		dao += "func GetList" + table.AaBbName + "(" + table.aaBbName + " domain." + table.AaBbName + ", pageNum, pageSize int) (vo.BaseListVo, error) {" +
+			"\n\trows, err := configs.Engine.Limit(pageSize, (pageNum-1)*pageSize).Desc(\"id\").Rows(&" + table.aaBbName + ")" +
+			"\n\tvar vo vo.BaseListVo" +
+			"\n\tif err != nil {" +
+			"\n\t\treturn vo, err" +
+			"\n\t}" +
+			"\n\tvo.PageNum = pageNum" +
+			"\n\tvo.PageSize = pageSize" +
+			"\n\tvo.List = make([]any, 0)" +
+			"\n\tfor rows.Next() {" +
+			"\n\t\tvar u domain." + table.AaBbName +
+			"\n\t\trows.Scan(&u)" +
+			"\n\t\tvo.List = append(vo.List, u)" +
+			"\n\t}" +
+			"\n\t//查询数量" +
+			"\n\tcount, err := configs.Engine.Count(&" + table.aaBbName + ")" +
+			"\n\tvo.Total = int(count)" +
+			"\n\treturn vo, nil" +
+			"\n}\n\n"
+
+		dao += "func Save" + table.AaBbName + "(" + table.aaBbName + " *domain." + table.AaBbName + ") (domain." + table.AaBbName + ", error) {" +
+			"\n\t_, err := configs.Engine.Insert(&" + table.aaBbName + ")" +
+			"\n\tif err != nil {" +
+			"\n\t\treturn *" + table.aaBbName + ", err" +
+			"\n\t}" +
+			"\n\treturn *" + table.aaBbName + ", nil" +
+			"\n}\n\n"
+		dao += "func Set" + table.AaBbName + "(" + table.aaBbName + "Map map[string]interface{}, id int) (domain." + table.AaBbName + ", bool, error) {" +
+			"\n\tvar " + table.aaBbName + " domain." + table.AaBbName + "" +
+			"\n\t_, err := configs.Engine.Table(\"" + table.Name + "\").Where(\"" + idColumn.Name + " = ?\", id).Update(" + table.aaBbName + "Map)" +
+			"\n\tif err != nil {" +
+			"\n\t\treturn " + table.aaBbName + ", false, err" +
+			"\n\t}" +
+			"\n\treturn " + table.aaBbName + ", true, nil" +
+			"\n}\n\n"
+		if table.existId {
+			dao += "func Delete" + table.AaBbName + "(" + table.aaBbName + " domain." + table.AaBbName + ") bool {" +
+				"\n\ti, err := configs.Engine.Where(\"id = ?\", " + table.aaBbName + ".Id).Delete(&" + table.aaBbName + ")" +
+				"\n\tif err != nil {" +
+				"\n\t\treturn false" +
+				"\n\t}" +
+				"\n\treturn i > 0" +
+				"\n}"
+		}
+		//创建文件
+		url := baseUrl + fileName
+		os.Truncate(url, 0)
+		file, _ := os.OpenFile(url, os.O_CREATE|os.O_RDWR, 0666)
+		writer := bufio.NewWriter(file)
+		writer.WriteString(dao)
+		writer.Flush()
+		file.Close()
+	}
+
+}