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) } //注意xorm Count、Find方法执行会自动closeSession,直接使用上边的session,第二条sql的where条件会丢失, //所以通过 session.Conds()提取where条件,放到各自的session进行执行 cnt,_:=engine.Where(session.Conds()).Count(&persons) err := engine.Where(session.Conds()).OrderBy("create_time desc").Limit(10, 0).Find(&persons)