java-spring与mongodb的整合方式一 自动注入xml(7)
- UID
- 1066743
|
java-spring与mongodb的整合方式一 自动注入xml(7)
特殊查询用法积累:
添加在UserReposityImpl中
1.增加排序
public List<User> getByLinkUrlId(int begin, int end, String linkUrlid,String orderField,Sort.Direction direction ) {
Query query = new Query();
query.addCriteria(Criteria.where("pageUrl.linkUrl.id").is(linkUrlid));
return find(query.limit(end - begin).skip(begin).with(new Sort(new Sort.Order(direction, orderField))),User.class);
}
2.按时间段查询
public List<Log> getLogs(List<Condition> conditions,String beginDate,String endDate)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).regex(
".*?\\" +condition.getValue()+ ".*"));
}
}
query.addCriteria(Criteria.where("crawlTime").lt(new Date(endDate)).gt(new Date(beginDate)));
return find(query,Log.class);
}
3.或者查询
Criteria criteria = new Criteria();
if (!StringUtils.isEmpty(data.get("name"))) {
criteria.orOperator(
Criteria.where("name").regex(
".*" + data.get("name") + ".*", "i"),
Criteria.where("chromosome").regex(
".*" + data.get("name") + ".*", "i"));
}
Query query = Query.query(criteria); |
|
|
|
|
|