背景
数据库连接
`
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
`
模型
type Application struct {
AppIntIdint32 `json:"app_int_id" `
AppIdstring `json:"app_id" `
AppNamestring `json:"app_name"`
AppIconstring `json:"app_icon" `
AppDescstring `json:"app_desc" `
AppTagsstring `json:"app_tags" `
AppLinkstring `json:"app_link" `
AppImagesstring `json:"app_images" `
AppFunctionstring `json:"app_function" `
CreatedTimetime.Time `json:"create_time" `
UpdateTimetime.Time `json:"update_time" `
}
没有使用orm ,用原生sql 查询,查询如下,
func GetAppListDao(req *models.GetAppListReq) (*[]dto.Application, error) {
list := &[]dto.Application{}
sql :="select * from t_application"
err :=mysql.DB.Select(list, sql)
if err !=nil {
return nil, err
}
fmt.Println("app list:", list)
return list, nil
}
报错提示:
`{"error": "missing destination name app_int_id in *[]dto.Application"}`
原因:
是因为 结构体没有定义映射数据库表的结构标签, 增加结构体 标签之后 就ok 了。 如果某个字段的标签和数据库表的字段名不一致,也会提示同样的错误,确保结构体标签都正确即可。
` db:"app_int_id"`
网友评论