生活随笔
收集整理的這篇文章主要介紹了
xorm2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝
go get xorm.io/xorm
連接數據庫,創建引擎
var ( userName
string = "root" passWord
string = "123456" idAddress
string = "127.0.0.1" port
string = "3306" dbName
string = "go_text" charset
string = "utf8" ) dataSourceName
:= fmt
. Sprintf ( "%s:%s@tcp(%s:%s)/%s?charset=%s" , userName
, passWord
, idAddress
, port
, dbName
, charset
) engine
, err
:= xorm
. NewEngine ( "mysql" , dataSourceName
) if err
!= nil { fmt
. Println ( "連接失敗" ) } else { fmt
. Println ( "數據庫連接成功" ) }
定義一個和表同步的結構體,并且自動同步結構體到數據庫
type User
struct { Id
int64 Name
string Age
int Passwd
string `xorm:"varchar(200)"` Created time
. Time
`xorm:"created"` Updated time
. Time
`xorm:"updated"` } err
= engine
. Sync ( new ( User
) ) if err
!= nil { fmt
. Println ( "表結構同步失敗" ) } else { fmt
. Println ( "表結構同步成功" ) }
數據插入
user
:= User
{ Id
: 1001 , Name
: "打阿偉" , Age
: 18 , Passwd
: "123456" } n
, _ := engine
. Insert ( & user
) if n
>= 1 { fmt
. Println ( "數據插入成功" ) } var users
[ ] Userusers
= append ( users
, User
{ Id
: 1003 , Name
: "阿偉" , Age
: 18 , Passwd
: "123456" } ) users
= append ( users
, User
{ Id
: 1004 , Name
: "偉" , Age
: 18 , Passwd
: "123456" } ) n
, _ = engine
. Insert ( & users
) if n
>= 1 { fmt
. Println ( "插入成功" ) }
數據的更新與刪除
user
:= User
{ Name
: "周南" } n
, _ := engine
. ID ( 1000 ) . Update ( & user
) if n
>= 1 { fmt
. Println ( "更新成功" ) } user
= User
{ Name
: "打阿偉" } n
, _ = engine
. ID ( 1001 ) . Delete ( & user
) fmt
. Println ( n
) rs
, err
:= engine
. Exec ( "update user set age =10 where id =1000" ) if err
!= nil { fmt
. Println ( err
) } fmt
. Println ( rs
)
條件查詢和遍歷輸出
result
, err
:= engine
. Query ( "select*from user" ) fmt
. Println ( result
) result2
, err
:= engine
. QueryString ( "select*from user" ) fmt
. Println ( result2
) result3
, err
:= engine
. QueryInterface ( "select*from user" ) fmt
. Println ( result3
) user
:= User
{ } engine
. Get ( & user
) fmt
. Println ( user
) user1
:= User
{ Name
: "阿偉" } engine
. Where ( "name=?" , user1
. Name
) . Desc ( "id" ) . Get ( & user1
) fmt
. Println ( user1
) var name
string engine
. Table ( & user
) . Where ( "id=1003" ) . Cols ( "name" ) . Get ( & name
) fmt
. Println ( name
) var users
[ ] Userengine
. Where ( "passwd=123456" ) . And ( "age=18" ) . Limit ( 10 , 0 ) . Find ( & users
) fmt
. Println ( users
) user
= User
{ Passwd
: "123456" } counts
, err
:= engine
. Count ( & user
) fmt
. Println ( counts
) engine
. Iterate ( & User
{ Passwd
: "123456" } , func ( idx
int , bean
interface { } ) error { user
:= bean
. ( * User
) fmt
. Println ( user
) return nil } ) rows
, err
:= engine
. Rows ( & User
{ Passwd
: "123456" } ) defer rows
. Close ( ) userBean
:= new ( User
) for rows
. Next ( ) { rows
. Scan ( userBean
) fmt
. Println ( userBean
) }
事務處理
session
:= engine
. NewSession ( ) defer session
. Close ( ) session
. Begin ( ) defer func ( ) { err
:= recover ( ) if err
!= nil { fmt
. Println ( err
) fmt
. Println ( "rollback" ) session
. Rollback ( ) } else { session
. Commit ( ) } } ( ) user1
:= User
{ Id
: 1005 , Name
: "皺起遠" , Age
: 18 , Passwd
: "123456" } if _ , err
:= session
. Insert ( & user1
) ; err
!= nil { panic ( err
) } user2
:= User
{ Name
: "朱加祎" , Age
: 18 , Passwd
: "123456" } if _ , err
:= session
. Where ( "id=1000" ) . Update ( & user2
) ; err
!= nil { panic ( err
) } if _ , err
:= session
. Exec ( "delete from user where name='皺起遠'" ) ; err
!= nil { panic ( err
) }
總結
以上是生活随笔 為你收集整理的xorm2 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。