1.安装
go get -u /typa01/go-mysql-utils
go get -u /typa01/go-utils
import (
"/typa01/go-mysql-utils"
)
2.创建数据库客户端,执行自动化ORM: Object(struct) Relational Mapping
var dbConfig
= "127.0.0.1"
= "root"
= "123456"
= true
= "test"
client := (dbConfig)
orm := (client)
= true
tabNames := []string{"we_test_tab1", "we_test_tab2"}
(tabNames)
源码地址
/typa01/go-mysql-utils
/typa01/go-mysql-utils/blob/master/
3.自动化ORM结果(表"we_test_tab1"为例)
结果含有:数据库对象DO生成,表行注释[可选],查询,批量查询,插入,主键更新,主键删除,批量插入(获取对应id[可选])
此操作依赖数据库操作,请参考: /typa01_kk/article/details/80144513
/*
test table1
*/
type WeTestTab1 struct {
Id int64 `column:"id"` // The primary key id
Name string `column:"name"` // The user name
Gender int64 `column:"gender"` // The user gerder, 1:male 2:female 0:default
Birthday `column:"birthday"` // The user birthday, eg: 2018-04-16
Stature float64 `column:"stature"` // The user stature, eg: 172.22cm
Weight float64 `column:"weight"` // The user weight, eg: 21.77kg
CreatedTime `column:"created_time"` // created time
ModifiedTime `column:"modified_time"` // record time
IsDeleted int64 `column:"is_deleted"` // Logic to delete(0:normal 1:deleted)
WeTestTab1s [] WeTestTab1 // This value is used for batch queries and inserts.
}
func (weTestTab1 *WeTestTab1) RowToStruct(row *) error {
builder := ()
(&)
(&)
(&)
(&)
(&)
(&)
(&)
(&)
(&)
err := (()...)
if err != nil {
return err
}
return nil
}
func (weTestTab1 *WeTestTab1) RowsToStruct(rows *) error {
var weTestTab1s [] WeTestTab1
builder := ()
for () {
()
(&)
(&)
(&)
(&)
(&)
(&)
(&)
(&)
(&)
err := (()...)
if err != nil {
return err
}
weTestTab1s = append(weTestTab1s, *weTestTab1)
}
if rows != nil {
defer ()
}
weTestTab1.WeTestTab1s = weTestTab1s
return nil
}
func (weTestTab1 *WeTestTab1) Insert(client *DBClient, idSet bool) (int64, error) {
structParam := *weTestTab1
sql := ()
qSql := ()
params := ()
("INSERT INTO ")
("we_test_tab1")
(" (")
ks := (structParam)
vs := (structParam)
for i, ksLen := 0, ()-1; i < ksLen; i++ {
col := (i).("column")
v := (i).Interface()
if col == "id" && !idSet {
continue
}
("`").Append(col).Append("`,")
("?,")
(v)
}
()
()
(") VALUES (").Append(()).Append(");")
defer ()
return ((), ()...)
}
func (weTestTab1 *WeTestTab1) UpdateWeTestTab1ById(client *DBClient) (int64, error) {
structParam := *weTestTab1
sql := ()
params := ()
("UPDATE ")
("we_test_tab1")
(" SET ")
ks := (structParam)
vs := (structParam)
var id interface{}
for i, ksLen := 0, ()-1; i < ksLen; i++ {
col := (i).("column")
v := (i).Interface()
if col == "id" {
id = v
continue
}
(col).Append("=").Append("?,")
(v)
}
()
(id)
(" WHERE id = ?;")
defer ()
return ((), ()...)
}
func (weTestTab1 *WeTestTab1) DeleteWeTestTab1ById(client *DBClient) (int64, error) {
structParam := weTestTab1
sql := ()
("DELETE FROM ")
("we_test_tab1")
(" WHERE id = ?;")
defer ()
return ((), )
}
func (weTestTab1 *WeTestTab1) BatchInsert(client *DBClient, idSet, returnIds bool) ([]int64, error) {
structParam := *weTestTab1
list := structParam.WeTestTab1s
var result []int64
listLen := len(list)
if listLen == 0 {
return result, ("no data needs to be inserted")
}
sql := ()
oneQSql := ()
batchQSql := ()
ks := (structParam)
fieldsNum := () - 1
("INSERT INTO ")
("we_test_tab1")
(" (")
for i := 0; i < fieldsNum; i++ {
iCol := (i).("column")
if iCol == "id" && !idSet {
continue
}
("`").Append(iCol).Append("`,")
}
().Append(") VALUES ")
batchInsertColsLen := ((idSet, fieldsNum, fieldsNum-1))
("(")
for j := 0; j < batchInsertColsLen; j++ {
("?,")
}
().Append(")")
if !returnIds {
for j := 0; j < listLen; j++ {
(()).Append(",")
}
()
batchSql := ().Append(()).Append(()).Append(";").ToString()
batchParams := ()
for k := range list {
item := list[k]
kItem := (item)
for l := 0; l < fieldsNum; l++ {
lCol := (l).("column")
if lCol == "id" && !idSet {
continue
}
((l).Interface())
}
}
id, err := (batchSql, ()...)
if err != nil {
return result, err
}
result = append(result, id)
} else {
oneSql := ().Append(()).Append(()).Append(";").ToString()
oneParams := ()
tx, err := ()
if err != nil {
return result, err
}
for m := range list {
()
item := list[m]
mItem := (item)
for n := 0; n < fieldsNum; n++ {
nCol := (n).("column")
if nCol == "id" && !idSet {
continue
}
((n).Interface())
}
id, err := (tx, oneSql, ()...)
if err != nil {
(tx)
var resultTxRollback []int64
return resultTxRollback, err
}
result = append(result, id)
}
if !(tx) {
return result, ("batch insert (returnIds=true) tx commit failed")
}
}
defer ()
return result, nil
}