ORM组件提供了一些常用的条件查询方法,并且条件方法支持多种数据类型输入。

  1. func (m *Model) Where(where interface{}, args...interface{}) *Model
  2. func (m *Model) WherePri(where interface{}, args ...interface{}) *Model
  3. func (m *Model) WhereBetween(column string, min, max interface{}) *Model
  4. func (m *Model) WhereLike(column string, like interface{}) *Model
  5. func (m *Model) WhereIn(column string, in interface{}) *Model
  6. func (m *Model) WhereNull(columns ...string) *Model
  7. func (m *Model) WhereNotBetween(column string, min, max interface{}) *Model
  8. func (m *Model) WhereNotLike(column string, like interface{}) *Model
  9. func (m *Model) WhereNotIn(column string, in interface{}) *Model
  10. func (m *Model) WhereNotNull(columns ...string) *Model
  11. func (m *Model) WhereOr(where interface{}, args ...interface{}) *Model
  12. func (m *Model) WhereOrBetween(column string, min, max interface{}) *Model
  13. func (m *Model) WhereOrLike(column string, like interface{}) *Model
  14. func (m *Model) WhereOrIn(column string, in interface{}) *Model
  15. func (m *Model) WhereOrNull(columns ...string) *Model
  16. func (m *Model) WhereOrNotBetween(column string, min, max interface{}) *Model
  17. func (m *Model) WhereOrNotLike(column string, like interface{}) *Model
  18. func (m *Model) WhereOrNotIn(column string, in interface{}) *Model
  19. func (m *Model) WhereOrNotNull(columns ...string) *Model

下面我们对其中的几个常用方法做简单介绍,其他条件查询方法用法类似。

Where/WhereOr查询条件

基本介绍

这两个方法用于传递查询条件参数,支持的参数为任意的string/map/slice/struct/*struct类型。

Where条件参数推荐使用字符串的参数传递方式(并使用?占位符预处理),因为map/struct类型作为查询参数无法保证顺序性,且在部分情况下(数据库有时会帮助你自动进行查询索引优化),数据库的索引和你传递的查询条件顺序有一定关系。

当使用多个Where方法连接查询条件时,多个条件之间使用And进行连接。 此外,当存在多个查询条件时,gdb会默认将多个条件分别使用()符号进行包含,这种设计可以非常友好地支持查询条件分组。

使用示例:

  1. // WHERE `uid`=1
  2. Where("uid=1")
  3. Where("uid", 1)
  4. Where("uid=?", 1)
  5. Where(g.Map{"uid" : 1})
  6. // WHERE `uid` <= 1000 AND `age` >= 18
  7. Where(g.Map{
  8. "uid <=" : 1000,
  9. "age >=" : 18,
  10. })
  11. // WHERE (`uid` <= 1000) AND (`age` >= 18)
  12. Where("uid <=?", 1000).Where("age >=?", 18)
  13. // WHERE `level`=1 OR `money`>=1000000
  14. Where("level=? OR money >=?", 1, 1000000)
  15. // WHERE (`level`=1) OR (`money`>=1000000)
  16. Where("level", 1).WhereOr("money >=", 1000000)
  17. // WHERE `uid` IN(1,2,3)
  18. Where("uid IN(?)", g.Slice{1,2,3})

使用struct参数的示例,其中ormtag用于指定struct属性与表字段的映射关系:

  1. type Condition struct{
  2. Sex int `orm:"sex"`
  3. Age int `orm:"age"`
  4. }
  5. Where(Condition{1, 18})
  6. // WHERE `sex`=1 AND `age`=18

使用示例

Where + string,条件参数使用字符串和预处理。

  1. // 查询多条记录并使用Limit分页
  2. // SELECT * FROM user WHERE uid>1 LIMIT 0,10
  3. db.Model("user").Where("uid > ?", 1).Limit(0, 10).All()
  4. // 使用Fields方法查询指定字段
  5. // 未使用Fields方法指定查询字段时,默认查询为*
  6. // SELECT uid,name FROM user WHERE uid>1 LIMIT 0,10
  7. db.Model("user").Fileds("uid,name").Where("uid > ?", 1).Limit(0, 10).All()
  8. // 支持多种Where条件参数类型
  9. // SELECT * FROM user WHERE uid=1 LIMIT 1
  10. db.Model("user").Where("u.uid=1",).One()
  11. db.Model("user").Where("u.uid", 1).One()
  12. db.Model("user").Where("u.uid=?", 1).One()
  13. // SELECT * FROM user WHERE (uid=1) AND (name='john') LIMIT 1
  14. db.Model("user").Where("uid", 1).Where("name", "john").One()
  15. db.Model("user").Where("uid=?", 1).And("name=?", "john").One()
  16. // SELECT * FROM user WHERE (uid=1) OR (name='john') LIMIT 1
  17. db.Model("user").Where("uid=?", 1).Or("name=?", "john").One()

Where + slice,预处理参数可直接通过slice参数给定。

  1. // SELECT * FROM user WHERE age>18 AND name like '%john%'
  2. db.Model("user").Where("age>? AND name like ?", g.Slice{18, "%john%"}).All()
  3. // SELECT * FROM user WHERE status=1
  4. db.Model("user").Where("status=?", g.Slice{1}).All()

Where + map,条件参数使用任意map类型传递。

  1. // SELECT * FROM user WHERE uid=1 AND name='john' LIMIT 1
  2. db.Model("user").Where(g.Map{"uid" : 1, "name" : "john"}).One()
  3. // SELECT * FROM user WHERE uid=1 AND age>18 LIMIT 1
  4. db.Model("user").Where(g.Map{"uid" : 1, "age>" : 18}).One()

Where + struct/*structstruct标签支持 orm/json,映射属性到字段名称关系。

  1. type User struct {
  2. Id int `json:"uid"`
  3. UserName string `orm:"name"`
  4. }
  5. // SELECT * FROM user WHERE uid =1 AND name='john' LIMIT 1
  6. db.Model("user").Where(User{ Id : 1, UserName : "john"}).One()
  7. // SELECT * FROM user WHERE uid =1 LIMIT 1
  8. db.Model("user").Where(&User{ Id : 1}).One()

以上的查询条件相对比较简单,我们来看一个比较复杂的查询示例。

  1. condition := g.Map{
  2. "title like ?" : "%九寨%",
  3. "online" : 1,
  4. "hits between ? and ?" : g.Slice{1, 10},
  5. "exp > 0" : nil,
  6. "category" : g.Slice{100, 200},
  7. }
  8. // SELECT * FROM article WHERE title like '%九寨%' AND online=1 AND hits between 1 and 10 AND exp > 0 AND category IN(100,200)
  9. db.Model("article").Where(condition).All()

WherePri支持主键的查询条件

WherePri方法的功能同Where,但提供了对表主键的智能识别,常用于根据主键的便捷数据查询。假如user表的主键为uid,我们来看一下WhereWherePri的区别:

  1. // WHERE `uid`=1
  2. Where("uid", 1)
  3. WherePri(1)
  4. // WHERE `uid` IN(1,2,3)
  5. Where("uid", g.Slice{1,2,3})
  6. WherePri(g.Slice{1,2,3})

可以看到,当使用WherePri方法且给定参数为单一的参数基本类型或者slice类型时,将会被识别为主键的查询条件值。

Content Menu