java-spring与mongodb的整合方式一 自动注入xml(3)
- UID
- 1066743
|
java-spring与mongodb的整合方式一 自动注入xml(3)
实现类:UserReposityImpl
package com.mongo.reposity.Impl;
import java.util.List;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import com.mongo.entity.User;
import com.mongo.reposity.UserReposity;
import com.mongodb.Mongo;
public class UserReposityImpl extends GenericReposityImpl<User> implements
UserReposity {
public UserReposityImpl(Mongo mongo, String databaseName) {
super(mongo, databaseName);
// TODO Auto-generated constructor stub
}
public List<User> getUsers(int begin, int end) {
return find(new Query().limit(end - begin).skip(begin), User.class);
}
public List<User> getUsers(String Name) {
return find(
new Query(new Criteria("name")
.regex(".*?" + Name + ".*")).limit(5),
User.class);
}
public List<User> getUsers(int begin, int end, List<Condition> conditions) {
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).is(
condition.getValue()));
}
}
return find(query.limit(end - begin).skip(begin),User.class);
}
public long getCount(String message) {
Query query = new Query();
query.addCriteria(Criteria.where("message").is(message));
return count(query, User.class);
}
public List<User> getUsersBynames(int begin, int end,
List<String> names) {
Query query = new Query();
query.addCriteria(Criteria.where("name").in(names));
return find(query.limit(end - begin).skip(begin), User.class);
}
public WriteResult updateUserUpdateName(User user,String name) {
String id=user.getId();
return updateFirst(new Query(Criteria.where("id").is(id)),Update.update("name", name),User.class);
}
public void mapReduse(){
DBCollection coll = getCollection("user");
String map = "function() { emit(this.name, {count:1});}";
String reduce = "function(key, values) {var total = 0;for(var i=0;i<values.length;i++){total += values[i].count;}return {count:total};}";
String result = "resultCollection";
MapReduceOutput mapReduceOutput = coll.mapReduce(map,
reduce.toString(), result, null);
DBCollection resultColl = mapReduceOutput.getOutputCollection();
DBCursor cursor = resultColl.find();
while (cursor.hasNext()) {
Map object = cursor.next().toMap();
System.out.println(object.get("value"));
// System.out.println(cursor.next().get("value"));
// User user = (User)cursor.next();
}
}
} |
|
|
|
|
|