012-Go ORM框架之Gorm测试

1:参考:https://github.com/jinzhu/gorm

2:数据库脚本(pg)

复制代码
--
create table posts(
    id            serial primary key,
    content        text,
    author        varchar(100),
    create_time    timestamptz
);

create table comments(
    id            serial primary key,
    content        text,
    author        varchar(100),
    post_id        int references posts(id),
    create_time    timestamptz
);
复制代码

3:posts.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package posts
 
import(
    "fmt"
    "github.com/jinzhu/gorm"
    _"github.com/lib/pq"
    "time"
)
 
 
type Comment struct{
    ID          int        
    Content     string      `sql:"not null"`
    Author      string      `sql:"not null"`
    PostId      int         `sql:"post_id"`
    CreateTime  time.Time   `sql:"create_time"`
}
 
type Post struct{
    ID          int        
    Content     string      `sql:"not null"`
    Author      string      `sql:"not null"`
    CreateTime  time.Time   `sql:"create_time"`
    Comments    []Comment
}
 
 
const(
    host = "192.168.72.128"
    port = 5432
    user = "test"
    password = "test"
    dbname = "testdb"
)
 
var Db *gorm.DB
 
func init(){
    var err error
     
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",host, port, user, password, dbname)
 
    Db, err = gorm.Open("postgres", psqlInfo)
    if err != nil{
        panic(err)
    }
 
    Db.AutoMigrate(&Post{}, &Comment{})
}
 
func (post *Post) CreatePost() error{
    return Db.Create(post).Error
}
 
func (comment *Comment) CreateComment(post *Post) error{
    return Db.Model(post).Association("Comments").Append(comment).Error
}
 
func (post *Post) GetComments() (comments []Comment, err error){
    Db.Where("author=$1", "王五").First(post)
    err = Db.Model(&post).Related(&comments).Error
    return
}

 4:main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
 
import(
    "fmt"
    "time"
    "Chapter02/posts"
)
 
func main(){
    post := posts.Post{
        Content:"Hello go!",
        Author:"王五",
        CreateTime: time.Now(),
    }
 
    fmt.Println(post)
 
    err := post.CreatePost()
    if err!=nil{
        panic(err)
    }
    fmt.Println(post)
 
    comment := posts.Comment{
        Content:"不错哟",
        Author:"小二",
        CreateTime: time.Now(),
    }
    err = comment.CreateComment(&post)
    if err != nil{
        panic(err)
    }
 
    post = posts.Post{}
    comments, err := post.GetComments()
    if err != nil{
        panic(err)
    }
    for _,p :=  range comments{
        fmt.Printf("%s-%s\n", p.Author,p.Content)
    }
}

  

 

posted @   yshy  阅读(2409)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· DeepSeek智能编程
· 精选4款基于.NET开源、功能强大的通讯调试工具
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
历史上的今天:
2014-06-06 jdbc 通过rs.getString()获取数据库中的时间字段问题
点击右上角即可分享
微信分享提示