xorm
2020-08-27 本文已影响0人
宋song一
xormplus
Go数据库操作:1、xorm包-基础配置与映射
https://www.kancloud.cn/xormplus/xorm/167077
一、xorm
文档
xorm操作指南
xorm操作指南
xorm数据简单查询
a. xorm一对多查询
type result struct {
Count int `xorm:"count"`
Hash string `xorm:"hash"`
}
item := &result{}
//query:= []result{}
rows, err := mysql.GetEngine().SQL("select `hash`,count(*) as count from tx_record group by `hash` having count>1;").Rows(item)
if err != nil {
logrus.Error("err:", err.Error())
//types.HTTPReturnWrite(w, &types.JSONRet{Code: types.ParamError, Msg: "etherscan后台错误"})
return
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(item)
if err != nil {
logrus.Error("err:", err.Error())
//types.HTTPReturnWrite(w, &types.JSONRet{Code: types.ParamError, Msg: "etherscan后台错误"})
return
}
fmt.Println(item.Hash, item.Count)
}
二、go xorm 动态条件查询
var persons []domain.Person
session := engine.Where("1=1") //重要代码
if param.ActivityId != nil {
session = session.And("activity_id = ?", param.ActivityId)
}
if param.Sex != nil {
session = session.And("sex = ?", param.Sex)
}
if param.Num != nil {
session = session.And("num = ?", param.Num)
}
if param.Name != "" {
name := "%" + param.Name + "%"
session = session.And("name like ?", name)
}
err := session.OrderBy("create_time desc").Limit(10, 0).Find(&persons)