beego

基础

获取配置的两种方式

     mode:= beego.AppConfig.String( "runmode")
     mode, _:= beego.GetConfig("String", "runmode", "dev")

orm

模型

type AwardDetailInfo struct {
    Id             uint64    `orm:"pk;column(id)" json:"id"`                                             //主键
    OrderId        uint64    `orm:"column(order_id)" json:"orderId"`                                     //订单主ID
    Mobile         string    `orm:"column(mobile)" json:"mobile"`                                        //手机号
    AwardMoney     int       `orm:"column(award_money)" json:"awardMoney" `                              //返费金额
    CreateTime     time.Time `orm:"column(create_time)" json:"createTime" default:"1971-01-01 00:00:00"` //数据库记录生成时间
    UpdateTime     time.Time `orm:"column(update_time)" json:"updateTime"`                               //数据库记录更新时间
}
type AwardDetailEntity struct {
    Id             uint64    `json:"id"`                                       //主键
    OrderId        uint64    `json:"orderId"`                                  //订单主ID
    Mobile         string    `json:"mobile"`                                   //手机号
    AwardMoney     int       `json:"awardMoney" `                              //返费金额
    CreateTime     time.Time `json:"createTime" default:"1971-01-01 00:00:00"` //数据库记录生成时间
    UpdateTime     time.Time `json:"updateTime"`                               //数据库记录更新时间
}

使用queryTable

func main() {
    o := orm.NewOrm()
    o.Using(common.DB_NAME)

    var daoLists []*dao.AwardDetailInfo
    qs := o.QueryTable("award_detail_info")
    num, _ := qs.Filter("orderId", 10001).All(&daoLists)
    fmt.Println(num)
    entityList := make([]common.AwardDetailEntity, 0)
        // 输出json格式
    for _, row := range daoLists {
        entity := common.AwardDetailEntity{}
        copier.Copy(&entity, row)
        entityList = append(entityList, entity)
    }
    result, _ := json.Marshal(entityList)
    fmt.Println(string(result))
}

使用Raw

func main() {
    o := orm.NewOrm()
    o.Using(common.DB_NAME)
    daoLists := make([]dao.AwardDetailInfo, 0)
    num,_ := o.Raw("SELECT * FROM award_detail_info WHERE order_id = ?",100012381125).QueryRows(&daoLists)
    entityList := make([]common.AwardDetailEntity, 0)

    for _, row := range daoLists {
        entity := common.AwardDetailEntity{}
        copier.Copy(&entity, row)
        entityList = append(entityList, entity)
    }
    result, _ := json.Marshal(entityList)
    fmt.Println(string(result))

    fmt.Println(num)
}

Read

    // Read 默认通过查询主键赋值,可以使用指定的字段进行查询
    o := orm.NewOrm()
    o.Using(common.DB_NAME)

    detail :=  dao.AwardDetailInfo{Id: 564}
    o.Read(&detail)
    fmt.Print(detail)

Read 默认通过查询主键赋值,可以使用指定的字段进行查询

    o := orm.NewOrm()
    o.Using(common.DB_NAME)
    u :=  dao.AwardDetailInfo{OrderId: 10001}
    o.Read(&u, "OrderId")
    fmt.Print(u)

QueryBuilder

适合复杂查询

    o := orm.NewOrm()
    o.Using(common.DB_NAME)

    qb, _ := orm.NewQueryBuilder("mysql")

    // 构建查询对象
    qb.Select("award_money",).
        From("award_detail_info").
        Where("order_id = ?")
    // 导出 SQL 语句
    sql := qb.String()

    // 执行 SQL 语句
    var detailDao []dao.AwardDetailInfo
    o.Raw(sql, 1001).QueryRows(&detailDao)

    fmt.Println(detailDao)

你可能感兴趣的:(beego)