标题:
java-spring与mongodb的整合方式一 手动注入xml
[打印本页]
作者:
look_w
时间:
2019-4-18 15:38
标题:
java-spring与mongodb的整合方式一 手动注入xml
User.java
package mongo;
public class User {
private String name;
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Userservice.java
package mongo;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MapReduceOutput;
public class Userservice {
private static String USERS_COLLECTION = "users";
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"WEB-INF/mongoConfig.xml");
MongoTemplate mongoTemplate = (MongoTemplate) ctx
.getBean("mongoTemplate");
// 插入数据
// User user=new User();
// user.setName("zoe");
// user.setMessage("try insert");
// mongoTemplate.insert(user,USERS_COLLECTION);
// System.out.println("insert finish");
// 查询
// 查询全部
// List<User> users=mongoTemplate.find(new Query(),
// User.class,USERS_COLLECTION);
// for(User a:users)
// {
// System.out.println(a.getName()+" "+a.getMessage());
// }
// 查询一个
// User a = mongoTemplate.findOne(
// new Query(Criteria.where("name").is("zoe")), User.class,
// USERS_COLLECTION);
// System.out.println(a.getName() + " " + a.getMessage());
// 模糊查询
// List<User> users = mongoTemplate.find(
// new Query(new Criteria("name").regex(".*?" + "o" + ".*"))
// .limit(9), User.class, USERS_COLLECTION);
// for (User a : users) {
// System.out.println(a.getName() + " " + a.getMessage());
// }
// 修改
// User a = mongoTemplate.findOne(
// new Query(Criteria.where("name").is("zoe")), User.class,
// USERS_COLLECTION);
// System.out.println(a.getName() + " " + a.getMessage());
// mongoTemplate
// .updateFirst(new Query(Criteria.where("name").is("zoe")),
// new Update().set("message", "update success"),
// USERS_COLLECTION);
// User b = mongoTemplate.findOne(
// new Query(Criteria.where("name").is("zoe")), User.class,
// USERS_COLLECTION);
// System.out.println(b.getName() + " " + b.getMessage());
// 删除
// User a = mongoTemplate.findOne(
// new Query(Criteria.where("name").is("zoe")), User.class,
// USERS_COLLECTION);
// System.out.println(a);
// mongoTemplate.remove(new Query(Criteria.where("name").is("zoe")),
// USERS_COLLECTION);
// User b = mongoTemplate.findOne(
// new Query(Criteria.where("name").is("zoe")), User.class,
// USERS_COLLECTION);
// System.out.println(b);
//mapreduce 统计同名数量
DBCollection coll = mongoTemplate.getCollection("users");
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()) {
System.out.println(cursor.next());
}
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0