Spring Data MongoDB Combat (top)

Source: Internet
Author: User
Tags mongodb driver

Spring Data MongoDB Combat (ON) Chszs, all rights reserved, without consent, not reproduced. Blogger Home: Http://blog.csdn.net/chszs

This article will detail how spring data MongoDB accesses the MongoDB database. MongoDB is an open-source, document-type NoSQL database, and spring data MongoDB is one of the spring data's modules dedicated to accessing the MongoDB database. The Spring Data MongoDB module provides both query methods based on method names and annotation-based query methods.

1. Configure and manage MongoDB with spring data

To install the MongoDB database, you can download it here: https://www.mongodb.org/downloads
The installation process is omitted. After you have completed the installation and operation of MongoDB, you can begin application development.
Start by creating a simple Maven project in Eclipse and configure Pom.xml to manage the dependencies of the spring Data MongoDB project. The contents are as follows:

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>SpringDataMongoDBDemo</groupId>    <artifactId>SpringDataMongoDBDemo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-mongodb</artifactId>            <version>1.7.2.RELEASE</version>        </dependency>    </dependencies></project>

Eclipse downloads the required jar packages and configures the dependencies to the project's classpath. Now that the project's dependencies have been imported, you can start writing the actual code.
First create an entity class that needs to be persisted to the MongoDB database.

Person.java

Package Com.ch.jpa.entity;import Java.util.arraylist;import Java.util.list;import Org.springframework.data.annotation.id;import Org.springframework.data.annotation.persistenceconstructor;import Org.springframework.data.mongodb.core.mapping.dbref;import org.springframework.data.mongodb.core.mapping.Document; @Document (collection = "Person") public class Person {@Id pri    Vate Long personId;    private String name;    private int age;    @DBRef (db = "address") private list<address> addresses = new arraylist<> ();        Public person () {} @PersistenceConstructor the public person (Long PersonId, String name, Int. age) {super ();        This.personid = personId;        THIS.name = name;    This.age = age;    } public Long Getpersonid () {return personId;    } public void Setpersonid (Long personId) {This.personid = PersonId;    } public String GetName () {return name; } public void SetName (String name) {this.name = NamE    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public list<address> getaddresses () {return addresses;    } public void Setaddresses (list<address> addresses) {this.addresses = addresses; } @Override Public String toString () {return ' person [personid= ' + PersonId + ", name=" + name + ", age=" +    Age + ", addresses=" + Addresses + "]"; }}

Note @document indicates that the data to be persisted is a collection. If the collection does not specify a name, the class name of the entity class is used by default.
Note @id indicates that the annotated field is mapped to the _id column in the collection. If this comment is not used in an entity class, the default domain named ID is mapped to the _id column in the collection. And the value of this field is automatically generated by the MongoDB driver package, and its value is not available in Pojo.
Note @dbref is used to refer to an existing entity class in the current entity class. However, unlike the case of a relational database, if we save the current entity, it does not save the referenced related entities. The persistence of the related entities referenced is separate.
Note @persistenceconstructor is used to mark the construction method for creating entities when retrieving data from the MongoDB database server.

The following is the associated address entity class:

Address.java

Package Com.ch.jpa.entity;import Org.springframework.data.annotation.id;import Org.springframework.data.annotation.persistenceconstructor;import org.springframework.data.mongodb.core.mapping.Document; @Document (collection = "Address") public class address {@Id P    Rivate long Addressid;    Private String address;    Private String City;    Private String State;    Private long zipcode;    Public Address () {System.out.println ("calling Default Cons");        @PersistenceConstructor public Address (Long Addressid, String Address, String city, String state, long ZipCode) {        Super ();        This.addressid = Addressid;        this.address = address;        this.city = City;        This.state = State;    This.zipcode = ZipCode;    } public String getaddress () {return address;    The public void setaddress (String address) {this.address = address;    } public String getcity () {return city; } public void Setcity (String city) {this.city = City;    } public String GetState () {return state;    public void SetState (String state) {this.state = state;    } public Long Getzipcode () {return zipcode;    } public void Setzipcode (long zipcode) {this.zipcode = ZipCode;  } @Override Public String toString () {return ' Address [address= + address + ', city= "+ City +", state= "+    State + ", zipcode=" + ZipCode + "]"; }}

Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.

Spring Data MongoDB Combat (top)

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.