使用golang 中的orm, 即gorm 进行多表联合查询
环境: Gin, gin-vue-admin, mysql

方法一

1、定义get请求参数

type ReqArticleItem struct {
	Page   int.  `form: page`
	PageSize int `form:"pageSize"`
	CategoryNameCn     string  `form:"categoryNameCn"`
	ContentNameCn     string  `form:"contentNameCn"`
	Title             string  `form:"title"`
}

其中form 标签一定需要,否则,ShouldBindQuery 无法获取get请求参数

2 service

三张表:article_categories, article_contents, article_items。联合查询 + 字段模糊查询

func (ais *ArticleItemService) GetItemList (reqArticleItem ReqArticle.ReqArticleItem) (err error, itemList []RespArticle.RespArticleItem, total int64) {
	limit := reqArticleItem.PageSize
	offset := reqArticleItem.PageSize * (reqArticleItem.Page - 1 )
	err = global.GVA_DB.Model(&articles.ArticleItem{}).Count(&total).Error
	db := global.GVA_DB.Model(&articles.ArticleItem{})
	db.Select("article_items.id,article_items.content_id,article_items.title,article_items.article_text,article_contents.category_id,article_contents.content_name_cn,article_categories.name_cn,article_items.created_at,article_items.updated_at")
	db.Joins("JOIN article_contents  ON article_contents.id = article_items.content_id")
	db.Joins("JOIN article_categories ON article_categories.id = article_contents.category_id")
	if strings.TrimSpace(reqArticleItem.ContentNameCn) != "" {
		db.Where("article_contents.content_name_cn like ?", "%"+strings.TrimSpace(reqArticleItem.ContentNameCn)+"%")
	}
	if strings.TrimSpace(reqArticleItem.Title) != "" {
		db.Where("article_items.title like ?", "%"+strings.TrimSpace(reqArticleItem.Title)+"%")
	}
	if strings.TrimSpace(reqArticleItem.CategoryNameCn) != "" {
		db.Where("article_categories.name_cn like ?", "%"+strings.TrimSpace(reqArticleItem.CategoryNameCn)+"%")
	}
	err = db.Limit(limit).Offset(offset).Scan(&itemList).Error
	//err = global.GVA_DB.Model(&articles.ArticleItem{}).Limit(limit).Offset(offset).Find(&itemList).Error
	return
}

3、更换字段名

从数据库中查询的数据为:

type RespArticleItem struct {
      ItemId     int  `json:"itemId"`
	ContentId     int  `json:"contentId"`
	NameCn.  string.  `json:"nameCn"`
}

那么 select 中对字段进行重命名,要跟 RespArticleItem 中的字段名一致。

db.Select("article_items.id as ItemId,article_items.content_id as ContentId,article_categories.name_cn as NameCn")
	db.Joins("JOIN article_contents  ON article_contents.id = article_items.content_id")
	db.Joins("JOIN article_categories ON article_categories.id = article_contents.category_id")

以上仅为说明使用的方法的示例。

方法二

err = global.GVA_DB.Model(business.BuBusinessGood{}).
             Joins("inner join bu_business_consignees on good_no=consignee_no").
        Joins("inner join bu_business_ships on good_no=ship_no").
        Joins("inner join bu_business_trailers on good_no=trailer_no").
        Joins("inner join bu_business_declarations on good_no=declaration_no").
        Joins("inner join bu_business_mores on good_no=more_no").Order("bu_business_goods.updated_at desc").
         Select("bu_business_goods.*, bu_business_consignees.*,bu_business_ships.*,bu_business_trailers.*,bu_business_declarations.*,bu_business_mores.* ").
        Limit(limit).Offset(offset).Find(&bsList).Error
Logo

前往低代码交流专区

更多推荐