本文主要是介绍mongodb和spring集成中MongoTemplate的总结是使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文档下载地址: http://download.csdn.net/detail/ruishenh/6591721
- 基础实体类
@Document(collection="person") class Person{
String id;
String name;
int age;
public String getId() { returnid; } public void setId(String id) { this.id = id; } public String getName() { returnname; } public void setName(String name) { this.name = name; } public int getAge() { returnage; } public void setAge(int age) { this.age = age; } } |
/**
* The collection name used for the specifiedclass by this template.
*
* @param entityClass must not be {@literalnull}.
* @return
*/
StringgetCollectionName(Class<?> entityClass);
String personName=mongoTemplate.getCollectionName(Person.class); |
/**
* Execute the a MongoDB command expressed as aJSON string. This will call the method JSON.parse that is part of the
* MongoDB driver to convert the JSON string toa DBObject. Any errors that result from executing this command will be
* converted into Spring's DAO exceptionhierarchy.
*
* @param jsonCommand a MongoDB commandexpressed as a JSON string.
*/
CommandResultexecuteCommand(String jsonCommand);
String jsonSql="{distinct:'person', key:'name'}"; CommandResult commandResult=mongoTemplate.executeCommand(jsonSql); System.out.println(); BasicDBList list = (BasicDBList)commandResult.get("values"); for (int i = 0; i < list.size(); i ++) { System.out.println(list.get(i)); } System.out.println(); |
/**
* Execute a MongoDB command. Any errors thatresult from executing this command will be converted into Spring's DAO
* exception hierarchy.
*
* @param command a MongoDB command
*/
CommandResultexecuteCommand(DBObject command);
String jsonSql="{distinct:'person', key:'name'}"; CommandResult commandResult=mongoTemplate.executeCommand((DBObject) JSON.parse(jsonSql)); System.out.println(); BasicDBList list = (BasicDBList)commandResult.get("values"); for (int i = 0; i < list.size(); i ++) { System.out.println(list.get(i)); } System.out.println(); |
/**
* Execute a MongoDB command. Any errors thatresult from executing this command will be converted into Spring's DAO
* exception hierarchy.
*
* @param command a MongoDB command
* @param options query options to use
*/
CommandResultexecuteCommand(DBObject command, int options);
String jsonSql="{distinct:'person', key:'name'}"; CommandResult commandResult=mongoTemplate.executeCommand((DBObject) JSON.parse(jsonSql),Bytes.QUERYOPTION_NOTIMEOUT); System.out.println(); BasicDBList list = (BasicDBList)commandResult.get("values"); for (int i = 0; i < list.size(); i ++) { System.out.println(list.get(i)); } System.out.println(); |
/**
* Execute a MongoDB query and iterate over thequery results on a per-document basis with a DocumentCallbackHandler.
*
* @param query the query class that specifiesthe criteria used to find a record and also an optional fields
* specification
* @param collectionName name of the collectionto retrieve the objects from
* @param dch the handler that will extractresults, one document at a time
*/
void executeQuery(Queryquery, String collectionName, DocumentCallbackHandler dch);
final Query query =new Query(); Criteria criteria =new Criteria(); criteria.and("name").is("zhangsan"); mongoTemplate.executeQuery(query,"person",new DocumentCallbackHandler() { //处理自己的逻辑,这种为了有特殊需要功能的留的开放接口命令模式 public void processDocument(DBObject dbObject) throws MongoException, DataAccessException { mongoTemplate.updateFirst(query, Update.update("name","houchangren"),"person"); } }); |
/**
* Executes a {@link DbCallback} translatingany exceptions as necessary.
* <p/>
* Allows for returning a result object, thatis a domain object or a collection of domain objects.
*
* @param <T> return type
* @param action callback object that specifiesthe MongoDB actions to perform on the passed in DB instance.
* @return a result object returned by theaction or <tt>null</tt>
*/
<T> Texecute(DbCallback<T> action);
Person person=mongoTemplate.execute(new DbCallback<Person>() { public Person doInDB(DB db)throws MongoException, DataAccessException { Person p=new Person(); //自己写逻辑和查询处理 // p.setAge(age); return p; } }); |
/**
* Executes the given {@linkCollectionCallback} on the entity collection of the specified class.
* <p/>
* Allows for returning a result object, thatis a domain object or a collection of domain objects.
*
* @param entityClass class that determines thecollection to use
* @param <T> return type
* @param action callback object that specifiesthe MongoDB action
* @return a result object returned by theaction or <tt>null</tt>
*/
<T> Texecute(Class<?> entityClass, CollectionCallback<T> action);
Person person=mongoTemplate.execute(Person.class,new CollectionCallback<Person>() {
public Person doInCollection(DBCollection collection) throws MongoException, DataAccessException { Person p=new Person(); //自己取值然后处理返回对应的处理 collection.find(); return p; } }); |
/**
* Executes the given {@linkCollectionCallback} on the collection of the given name.
* <p/>
* Allows for returning a result object, thatis a domain object or a collection of domain objects.
*
* @param <T> return type
* @param collectionName the name of thecollection that specifies which DBCollection instance will be passed into
* @param action callback object that specifiesthe MongoDB action the callback action.
* @return a result object returned by theaction or <tt>null</tt>
*/
<T> Texecute(String collectionName, CollectionCallback<T> action);
Person person=mongoTemplate.execute(“person”,new CollectionCallback<Person>() {
public Person doInCollection(DBCollection collection) throws MongoException, DataAccessException { Person p=new Person(); //自己取值然后处理返回对应的处理 collection.find(); return p; } }); |
/**
* Executes the given {@link DbCallback} withinthe same connection to the database so as to ensure consistency in a
* write heavy environment where you may readthe data that you wrote. See the comments on {@see <a
*href=http://www.mongodb.org/displ
这篇关于mongodb和spring集成中MongoTemplate的总结是使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!