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)

 

你可能感兴趣的:(go语言)