The use of MongoDB Learning (v) Spring integrated MongoDB

Source: Internet
Author: User

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

    • Using Spring Data

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.