ent 基本使用 二 简单create && query

接上文,前边我们了解了关于基本代码生成以及schema 迁移的学习,下边我们看看基本的数据操作
参考代码: https://github.com/rongfengliang/ent-demo

环境准备

  • mysql 数据库

    使用docker-compose 运行

 
version: "3"
services:
  mysql:
    image: mysql:5.7.16
    ports:
      - 3306:3306
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    environment:
      MYSQL_ROOT_PASSWORD: dalongrong
      MYSQL_DATABASE: gogs
      MYSQL_USER: gogs
      MYSQL_PASSWORD: dalongrong
      TZ: Asia/Shanghai
  • go mod 项目
go mod github.com/rongfengliang/ent-demo
  • 项目约定
    为了方便测试,使用多main 入口,同时使用makefile 进行管理
    项目结构
 
├── Makefile
├── README.md
├── build
├── create
├── migration
└── query
├── cmd
├── create
└── main.go
├── migration
└── main.go
└── query
└── main.go
├── docker-compose.yaml
├── ent
├── client.go
├── config.go
├── context.go
├── ent.go
├── example_test.go
├── migrate
├── migrate.go
└── schema.go
├── predicate
└── predicate.go
├── schema
└── user.go
├── tx.go
├── user
├── user.go
└── where.go
├── user.go
├── user_create.go
├── user_delete.go
├── user_query.go
└── user_update.go
├── go.mod
└── go.sum

说明
cmd 目录为操作,ent 为代码生成
Makefile

 
.PHONY : clean
all: clean cli
clean: 
 rm -rf build/*
cli:
 go build -o build ./cmd/...

常见数据处理

  • schmma 处理
    这个不是必须,如果项目已经运行过 ,可以不用运行 ,首次运行的话需要添加
 
go run cmd/migration/main.go 
  • 添加数据操作
    cmd/create/main.go
 
package main
import (
 "context"
 "fmt"
 "log"
 _ "github.com/go-sql-driver/mysql"
 "github.com/rongfengliang/ent-demo/ent"
)
func main() {
 client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
 if err != nil {
  log.Fatalf("failed opening connection to sqlite: %v", err)
 }
 defer client.Close()
 ctx := context.Background()
 createUser(ctx, client)
}
func createUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
 u, err := client.User.
  Create().
  SetAge(30).
  SetName("a8m").
  Save(ctx)
 if err != nil {
  return nil, fmt.Errorf("failed creating user: %v", err)
 }
 log.Println("user was created: ", u)
 return u, nil
}

说明:
从上边可以看出,使用很简单

  • 运行效果
 
go run cmd/create/main.go
2019/10/14 13:53:20 user was created: User(id=5, age=30, name=a8m)
first git:(master) 

数据查询处理

  • 查询代码
    cmd/query/main.go
 
package main
import (
 "context"
 "fmt"
 "log"
 _ "github.com/go-sql-driver/mysql"
 "github.com/rongfengliang/ent-demo/ent"
 "github.com/rongfengliang/ent-demo/ent/user"
)
func main() {
 client, err := ent.Open("mysql", "root:dalongrong@tcp(127.0.0.1)/gogs")
 if err != nil {
  log.Fatalf("failed opening connection to sqlite: %v", err)
 }
 defer client.Close()
 ctx := context.Background()
 queryUser(ctx, client)
}
func queryUser(ctx context.Context, client *ent.Client) (*ent.User, error) {
 u, err := client.User.
  Query().
  Where(user.ID(1)).
  // `Only` fails if no user found,
  // or more than 1 user returned.
  Only(ctx)
 if err != nil {
  return nil, fmt.Errorf("failed querying user: %v", err)
 }
 log.Println("user returned: ", u)
 return u, nil
}
  • 运行效果
go run cmd/query/main.go
2019/10/14 13:54:49 user returned: User(id=1, age=30, name=a8m)

说明

以上是一个简单的insert 以及查询处理,后边会介绍下关于图查询的处理,实际上ent 还包含了数据校验的处理

参考资料

https://entgo.io/docs/getting-started/
https://github.com/rongfengliang/ent-demo

posted on 2019-10-14 14:01  荣锋亮  阅读(792)  评论(0编辑  收藏  举报

导航