gorm返回结果集映射

1.注意结构体的属性名首字母一定要大写

package main

import (
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
)

func main() {
    var roles [] role
    db, err := gorm.Open("mysql", "root:123456@/shop?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        fmt.Print("链接异常")
    }
    defer db.Close()
    db.AutoMigrate(&role{})
    db.Debug().Table("role").Find(&roles)
    fmt.Print(roles[0].RoleName)
}
type role struct {
    Id string `gorm:"column:id"`
    RoleName string `gorm:"column:role_name"`
    Description string `gorm:"column:description"`
    CreateTime string `gorm:"column:create_time"`
    UpdateTime string `gorm:"column:update_time"`
    Status string `gorm:"column:status"`
}

 

posted @ 2021-02-15 22:54  动力起点  阅读(236)  评论(0)    收藏  举报