美文网首页GoGoGo
使用beego开发时候需要注意的地方

使用beego开发时候需要注意的地方

作者: 小歪子go | 来源:发表于2018-02-04 10:57 被阅读104次

    一、多表查询的时候建议使用原生sql查询,若使用beego提供的orm表定义的方法查询,返回字段形式固定,对于有代码强迫症的小伙伴来说很别扭,请看下图:

    func GetGoodsList(form *GoodsListForm) (err *utils.ControllerError, list *[]orm.Params) {
        var maps []orm.Params
        var returnp *utils.ControllerError
        o := orm.NewOrm()
        qs := o.QueryTable(new(Goods)).RelatedSel()
        var limit, offset int
        var errd error
        if form.Limit < 1 {
            limit = 20
        }
        if form.Offset < 1 {
            offset = 0
        }
        if len(form.GoodTypId) == 0 {
            _, errd = qs.Limit(limit, offset).OrderBy("Look").Values(&maps, "name", "GoodType__TypeId", "GoodType__TypeName", "Look", "GoodsId", "Price", "Discount", "ImageUrl", "StoreInfo__Amount", "StoreInfo__Left")
        } else {
            _, errd = qs.Filter("GoodType__TypeId", form.GoodTypId).Limit(limit, offset).OrderBy("Look").Values(&maps, "name", "GoodType__TypeId", "GoodType__TypeName", "Look", "GoodsId", "Price", "Discount", "ImageUrl", "StoreInfo__Amount", "StoreInfo__Left")
        }
        if errd != nil {
            returnp = utils.ErrDatabase
        } else {
            returnp = utils.Actionsuccess
        }
        return returnp, &maps
    }
    

    传入参数返回

    {
        "code": 9000,
        "data": [
            {
                "Discount": 7,
                "GoodType__TypeId": "2018020209225223",
                "GoodType__TypeName": "保险推销",
                "GoodsId": "2018020209252238",
                "ImageUrl": "",
                "Look": 9,
                "Name": "成人意外险",
                "Price": 90,
                "StoreInfo__Amount": 350,
                "StoreInfo__Left": 350
            }
        ],
        "message": "获取信息成功"
    }
    

    返回字段都是大写,且关联表字段含有双_,对于这样的格式我只能呵呵了

    如果采用原生SQL查询,请看一下代码

    func GetGoodsList1(form *GoodsListForm) (err *utils.ControllerError, list *[]orm.Params) {
        var maps []orm.Params
        var returnp *utils.ControllerError
        var errd error
        limit, offset := form.Limit, form.Offset
        if limit < 1 {
            limit = 20
        }
        if offset < 1 {
            offset = 0
        }
        o := orm.NewOrm()
        if len(form.GoodTypId) > 0 {
            _, errd = o.Raw("SELECT T0.`name` `name`, T1.`type_id` `typeid`, T1.`type_name` `typename`, T0.`look` `look`, T0.`goods_id` `goodsid`, T0.`price` `price`, T0.`discount` `discount`, T0.`image_url` `imageurl`, T2.`amount` `amount`, T2.`left` `left` FROM `goods` T0 INNER JOIN `good_type` T1 ON T1.`id` = T0.`good_type_id` INNER JOIN `good_store` T2 ON T2.`id` = T0.`store_info_id` WHERE T1.`type_id` = ? ORDER BY T0.`look` ASC LIMIT ? OFFSET ?", form.GoodTypId, limit, offset).Values(&maps)
        } else {
            _, errd = o.Raw("SELECT T0.`name` `name`, T1.`type_id` `typeid`, T1.`type_name` `typeName`, T0.`look` `look`, T0.`goods_id` `goodsid`, T0.`price` `price`, T0.`discount` `discount`, T0.`image_url` `imageurl`, T2.`amount` `amount`, T2.`left` `left` FROM `goods` T0 INNER JOIN `good_type` T1 ON T1.`id` = T0.`good_type_id` INNER JOIN `good_store` T2 ON T2.`id` = T0.`store_info_id` ORDER BY T0.`look` ASC LIMIT ? OFFSET ?", limit, offset).Values(&maps)
        }
        if errd != nil {
            returnp = utils.ErrDatabase
        } else {
            returnp = utils.Actionsuccess
        }
        return returnp, &maps
    }
    

    传入参数返回

    {
        "code": 9000,
        "data": [
            {
                "amount": "350",
                "discount": "7",
                "goodsid": "2018020209252238",
                "imageurl": "",
                "left": "350",
                "look": "9",
                "name": "成人意外险",
                "price": "90",
                "typeid": "2018020209225223",
                "typename": "保险推销"
            }
        ],
        "message": "获取信息成功"
    }
    

    采用原生SQL可以随意修改返回参数的名称了,再也不用担心有代码强迫症的伙伴BB了

    相关文章

      网友评论

      本文标题:使用beego开发时候需要注意的地方

      本文链接:https://www.haomeiwen.com/subject/hgcmzxtx.html