作者:红枫1983_1 | 来源:互联网 | 2014-05-28 16:53
1、首先使用Eclipse的git插件把spring提供的示例下载下来https://github.com/SpringSource/spring-data-document-examples.git下载之后导入maven工程2、首先看我们的domain定义,也就是entity。org.springframework.
1、首先使用Eclipse的git插件把spring提供的示例下载下来
https://github.com/SpringSource/spring-data-document-examples.git
下载之后导入maven工程
2、首先看我们的domain定义,也就是entity。
org.springframework.data.mongodb.examples.hello.domain.Person
package org.springframework.data.mongodb.examples.hello.domain;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.document.mongodb.mapping.Document;
@Document
public class Person {
@Id
private String id;
private String name;
private int age;
private List
accounts = new ArrayList();
public Person() {
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getAccounts() {
return accounts;
}
public void addAccount(Account account) {
this.accounts.add(account);
}
public void setAccounts(List accounts) {
this.accounts = accounts;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", accounts=" + accounts + "]";
}
}
dao层的类org.springframework.data.mongodb.examples.hello.HelloMongo
package org.springframework.data.mongodb.examples.hello;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
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 org.springframework.data.mongodb.examples.hello.domain.Account;
import org.springframework.data.mongodb.examples.hello.domain.Person;
import org.springframework.stereotype.Repository;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
*
* @author xiaofancn
*
* 参考 http://static.springsource.org/spring-data/data-document/docs/1.0.0.M2/reference/html/
* 参考 http://nosql.mypopescu.com/post/816470307/tutorial-mongodb-in-java
*
*/
@Repository
public class HelloMongo {
@Autowired
MongoOperations mongoOperations;
public void gridFSInput(String inputFilepath) {
DB db = mongoOperations.getCollection(
mongoOperations.getCollectionName(Person.class)).getDB();
db.requestStart();
File inputFile = new File(inputFilepath);
GridFSInputFile gfsInput;
try {
gfsInput = new GridFS(db, "fs")
.createFile(inputFile);
gfsInput.setFilename("qq123456789logo");// 保存到数据库的文件名为qq123456789logo
gfsInput.save();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.requestDone();
}
public void gridFSOutput(String outputFilepath) {
DB db = mongoOperations.getCollection(
mongoOperations.getCollectionName(Person.class)).getDB();
GridFSDBFile gfsFile = new GridFS(db, "fs").findOne("qq123456789logo");// 查找文件名qq123456789logo输出保存
try {
gfsFile.writeTo(outputFilepath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(int count) {
if (mongoOperations.collectionExists(Person.class)) {
mongoOperations.dropCollection(Person.class);
}
mongoOperations.createCollection(Person.class);
for (int i = 0; i < count; i++) {
Person p = new Person("小樊", i);
Account a = new Account("1234-59873-893-1", Account.Type.SAVINGS,
123.45D);
p.addAccount(a);
mongoOperations.insert(p);
}
}
public void delete(int age) {
// 删除查询到的年龄
mongoOperations.remove(new Query(new Criteria("age").is(age)),
Person.class);
}
public void alert(int age) {
// 根据age查询,更新查询到的age字段为随机数
mongoOperations.updateMulti(new Query(new Criteria("age").is(age)),
new Update().inc("age", new Random().nextInt()), Person.class);
}
public void alert(String name) {
// 根据name查询,更新查询到的name字段为xiaofancn
mongoOperations.updateMulti(new Query(new Criteria("name").in(name)),
new Update().set("name", "xiaofancn"), Person.class);
}
public void query(int age) {
// 根据age查询
List persons = mongoOperations.find(new Query(new Criteria(
"age").is(age)), Person.class);
for (Person p : persons) {
System.out.println(p.toString());
}
// ===========================jdbc原生的类
DBCollection personColl = mongoOperations.getCollection(mongoOperations
.getCollectionName(Person.class));
BasicDBObject parameter = new BasicDBObject();
parameter.put("age", age);
DBCursor item = personColl.find(parameter);
while (item.hasNext()) {
System.out.println(item.next());
}
}
/**
* 统计这个年龄的人数
* @param age
* @return
*/
public Long count(int age) {
DBCollection personColl = mongoOperations.getCollection(mongoOperations
.getCollectionName(Person.class));
BasicDBObject parameter = new BasicDBObject();
parameter.put("age", age);
return personColl.count(parameter);
}
public void showAllOrederByAge(int startPage, DBObject sort,
int countPerPage) {
if (sort == null) {// 默认按照年龄的升序
sort = BasicDBObjectBuilder.start().add("age", 1).get();
}
DBCollection personColl = mongoOperations.getCollection(mongoOperations
.getCollectionName(Person.class));
DBCursor item = personColl.find().sort(sort)
.skip((startPage-1) * countPerPage).limit(countPerPage);
while (item.hasNext()) {
// Person p = item.next();错误
DBObject parameter = item.next();
System.out.println(parameter);
}
}
public void showAll() {
List list = mongoOperations.findAll(Person.class);
for (Person p : list) {
System.out.println(p);
}
// =================大批量的查询尽量使用原生的函数,也要对应原生的文档结构。
DBObject sort = BasicDBObjectBuilder.start().add("age", -1).get();
DBCollection personColl = mongoOperations.getCollection(mongoOperations
.getCollectionName(Person.class));
DBCursor item = personColl.find();// 查询前100条记录
while (item.hasNext()) {
// Person p = item.next();错误
DBObject parameter = item.next();
System.out.println(parameter);
}
}
}
// 参考 http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations
// 对数组对象有效,数组对象后添加数据
mongoOperations.updateMulti(new Query(new Criteria("age").is(0)),
new Update().push("name", "小樊"), Person.class);
//找到数据,加法属性 age = age +-10
mongoOperations
.updateMulti(new Query(new Criteria("age").is(0)), new Update().inc("age", -10), Person.class);
//unset 重置属性值,数字为0,字符其他为null
mongoOperations.updateMulti(new Query(new Criteria("age").is(0)),
new Update().set("name", "小樊").set("age", -1), Person.class);
客户端org.springframework.data.mongodb.examples.hello.App
package org.springframework.data.mongodb.examples.hello;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello Mongo!
*/
public class App {
public static void main(String[] args) {
System.out.println("Bootstrapping HelloMongo");
ConfigurableApplicationContext context = null;
// use @Configuration using Java:
context = new ClassPathXmlApplicationContext(
"META-INF/spring/bootstrap.xml");
// use XML application context:
// context = new
// ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
HelloMongo hello = context.getBean(HelloMongo.class);
hello.delete(39);
hello.query(39);
//hello.showAll();
System.out.println("DONE!");
}
}
参考
http://static.springsource.org/spring-data/data-document/docs/1.0.0.M2/reference/html/