下面这段代码是我最近写项目用到的,功能是查找用户列表
限制条件是:指定用户周边、条数限制、随机获取、指定范围
public List<User> listUserByLocation(String username, double longitude,
double latitude, int distance, int size) {
Location location = new Location(longitude, latitude, distance);
String hql="from User u where u.username <> '"+username+"' and u.longitude between "+location.getMinLongitude()+" and "+location.getMaxLongitude()+" and u.latitude between "+location.getMinLatitude()+" and "+location.getMaxLatitude()+" order by rand()";
List<User> users=hibernateTemplate.getSessionFactory().openSession().createQuery(hql).setMaxResults(size).list();
return users;
}
转载于:https://blog.51cto.com/jormin/1314319