mongodb java 注入_java-spring与mongodb的整合方式一 手动注入xml

但是其中的操作都比较直白 没有经过封装  而且  每次使用前都要先写 数据库名和Ip端口

这次我们把spring和mongodb整合起来

内容如下:

1.创建项目和配置xml

2.spring-mongodb的增删改查 mapreduce

1.创建项目和配置xml

我们首先创建一个叫SM的项目

ec9b5797e63dbf2ea0176168344d4b3d.png

a2a912c773201d7357c79191f73f0342.png

然后增加web框架

8072231b08f926f41315a1459fc53c73.png

增加web框架我们看到的项目如图:

67e6a96c49120d90ba3673e7429bf3d7.png

解压得到lib文件夹和mongoConfig配置文件  这它们放入项目的WEB-INF文件夹中

69e46085673918321e4d9c2a12767bad.png

放入xml后刷新我们看到的项目如图:

ea1c531462ccd65639f4e5fcc2cafb1c.png

新建User类和UserService类如下:

f3fbf13fc6d2c4667b644fe7fcbad3df.png

d43839ee8ac4d8c491bac2152c0b437d.png

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 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 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

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());

}

}

}

结果演示:

插入和查询所有

41139c0c2f103dd0b111296c470bfc75.png

查询单个name为zoe的

7237b92f5a55dca9a6527a3e565038a7.png

模糊查询name中有o的

b417b19eaf53e7c55cd9b844b5a0fea8.png

修改数据

f98d2362472a060b87c6d0e6243f6a7e.png

删除数据

f4acb08827c9702c540aee904ecfb14b.png

mapreduce统计数量

a3f8219e83f829303e527df0224aade2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值