gorm common cases
2018年5月18日
GORM
加载数据库连接池
1 | package db |
定义model
1 | // CodeSnippet model of snippet |
1 | mysql> show create table code_snippet\G; |
column: 列名
type: 数据类型
创建一条数据
1 | func CreateSnippet(snippet *CodeSnippet) error { |
查询 by id
1 | func GetSnippetByID(id string) (*CodeSnippet, error) { |
查询列表
1 | func GetSnippetsByAuthor(AuthorID string, limit, offset int32) ([]*CodeSnippet, error) { |
Order by, limit, offset
1 | func GetSnipeptsByAuthor(authorID string, limit, offset int32) ([]*Collection, error) { |
更新
1 | func UpdateSnippet(snippet *CodeSnippet) (string, error) { |
删除
1 | func DeleteSnippet(id string) error { |
事务
1 | tx := db.ORM.Begin() |