1. Introduction
Before in a lot of places have seen this spring Data magical things, do not know what east, finally, today to simple to find out, the bottom is I from the online finishing ...
First, explain why you should use spring to integrate MongoDB: Because MongoDB's Java drivers are cumbersome to use, especially when dealing with multiple, deep-level relationships. You need to write a lot of code to construct DBObject. The first thing you must think of is the "Tiger Balm" spring. Spring data supports many NoSQL products, such as Redis, HBase, MongoDB, neo4j, and so on. If the project already uses spring, it should be taken for granted that spring data is introduced.
Introduction to Spring Data:
Spring Data, as one of the parent projects of SpringSource, is designed to unify and simplify persistent storage of all types , regardless of whether relational or NoSQL data is stored. Regardless of which persisted storage, a data access object (or called a DAO, data access Objects) typically provides CRUD (create, read, update, delete) operations on a single domain object, query methods, sorting and paging methods, and so on. Spring data provides a unified interface (Crudrepository,pagingandsortingrepository) based on these layers and an implementation of persistent storage. such as jdbctemplate,mongotemplate==;the Spring Data project is designed to simplify building data access counts based on spring framework applications, including non-relational databases, map-reduce frameworks, cloud data services, and more In addition, it also includes access support for the relational database.
There is a comparison of wood with the use of spring data, the scenario: A query that makes a fuzzy match to MongoDB:
- Pure Java, without spring Data:
Basicdbobject cond=new basicdbobject ();
Pattern pattern=pattern.compile ("^.*" +keywords+ ". *$");
Cond.put ("name", pattern);
list<user> users = Mongotemplate.find ( new Query ( "Name" ). Regex (". *?" ) + "Zhang" + ".*" ) . Limit (9), User. class );
It is obvious that code that uses spring data is easier to understand and neatly
Now there are a lot of people Spring the tired of seeing also began to annoy. Yes,Spring is so ambitious that he almost wants to monopolize everything in Java. There's no way I'm going to use Spring without him, so that other frameworks don't have to be learned. I learned a lot about spring , such as:spring security/spring integration/spring Batch , etc... He's not inventing the wheels. He has provided many scenarios in programming, and I have used those scenes to solve many of the problems in my work and make my work very efficient. So I have time to learn more about it. The Spring Data Mongo encapsulates the mongodb java driver, which provides and springjdbc/ Template Consistent programming style of mongotemplate.
See:http://static.springsource.org/spring-data/data-mongodb/docs/current/api/org/springframework/data /mongodb/core/mongotemplate.html
2. The crud of Spring Data MongoDB
The source of the project is from http://blog.mkfree.com/posts/511396a3975a15ea1757d849, under the source link of the project, download the import can use, using MAVEN to create the project, thanks to the Bo Lord's selfless dedication, The article about MongoDB is still good, I can see it is Bo Master's own experience, I also got some knowledge, but, say, that site, whether it is the interface or function, do not dare to compliment Ah, the article list is a mess, even the most basic categories are not, slightly spit trough ...
The next item about the code, should be original, please look forward to ...