ブロッカー、1つ来て、1つ行ってください。
関連クエリを征服します。
今日、私は会社のフロントエンドに尋ねて、gitlab API仕様をチェックしました。
昨夜の考えは素朴すぎることを証明し、リレーショナルクエリで一度実行するほうが形式的でした。
だから、達成するために関連するケースを見つけます。
https://segmentfault.com/a/1190000019331511
https://www.jianshu.com/p/b2de317bfe4a
私はプリロードを使用しています。実装のアイデアは次のとおりです(カプセル化を使用しているため、より複雑になります)。
1つは、データベースがどのように見えるか
user.go
type User struct { gorm.Model CreatedBy string `json:" created_by "` UpdatedBy string `json:" updated_by "` Deleted uint `json:" deteled "` State uint `json:" state "gorm:" default:1 "` ユーザー名文字列 `json:" username "` パスワード文字列 `json:" password "` アバター文字列 `json:" avatar "` UserType uint `json:" user_type "` アプリケーション* []アプリケーション }
project.go
// Project项目结构体型 Project struct { gorm.Model CreatedBy string `json:" created_by "` UpdatedBy string `json:" updated_by "` Deleted uint `json:" deteled "` State uint `json:" state "` Name文字列 `json:" name "` CnName文字列 `json:" cn_name "` 説明文字列 `json:" description "` UserID uint `json:" user_id "` アプリケーション[]アプリケーション }
外部キーを使用したApplication.go
type Application struct { gorm.Model CreatedBy string `json:" created_by "` UpdatedBy string `json:" updated_by "` Deleted uint `json:" deteled "` State uint `json:" state "gorm:" default:1 "` 名前文字列 `json:" name "` CnName文字列 `json:" cn_name "` 説明文字列 `json:" description "` Git文字列 `json:" git "` Jenkins文字列 `json:" jenkins "` UserID uint `json: "user_id" ` ProjectID uint` json: "project_id" ` ユーザーユーザー` gorm: "foreignkey:UserID" ` プロジェクトプロジェクト` gorm: "foreignkey:ProjectID "` }
次に、データモデルのrepository.goを読み取ります
func(a * ApplicationRepository)GetApplications(PageNum uint、PageSize uint、total * uint64、where interface {})* [] models.Application { var applications [] models.Application other:= map [string] string { "order": "ID desc"、 "foreignkey": "User、Project"、 } err:= a.Base.GetPages(&models.Application {}、&applications、PageNum、PageSize、total、where、other) if err!= nil { a .Log.Errorf( "获取文章信息失败"、err) } return&applications }
3、最後に基本的なbaseRepository.goを呼び出します
// GetPages分页返回数据 func(b * BaseRepository)GetPages(model interface {}、out interface {}、pageIndex、pageSize uint、totalCount * uint64、where interface {}、other map [string] string)error { db:= b.Source.DB()。Model(model).Where(model).Preload( "User")。Preload( "Project") if len(other)> 0 { if _、ok:= other ["foreignkey" ]; ok { for _、foreignkey:= range strings.Split(other ["foreignkey"]、 "、"){ db = db.Preload(strings.TrimSpace(foreignkey)) } } if _、ok:= other ["order "]; ok { for _、order:= range strings.Split(other ["order"]、 "、"){ db = db.Order(strings。 if err!= nil { b.Log.Errorf( "查询总数出错"、err) return err } if * totalCount == 0 { return nil } return db.Offset((pageIndex-1)* pageSize).Limit(pageSize ).Find(out).Error }
4番目に、フロントエンドに送信されるデータ
第五に、フロントエンドはどのように見えるか