日期:2014-05-16  浏览次数:20669 次

spring-data-mongodb的MongoTemplate 使用小例子

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<Account> accounts = new ArrayList<Account>();

	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<Account> getAccounts() {
		return accounts;
	}

	public void addAccount(Account account) {
		this.accounts.add(account);
	}

	public void setAccounts(List<Account> 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 b