MongoDB Learning One (using mongoresposity)

Source: Internet
Author: User

Recently the company to do a project to use MongoDB, below to introduce the Mongorepository interface.

You can analogy Hibernate's jpa,mongorepository is a basic interface that a springdata provides for operations such as add-and-revise checks and paging.

When we use interfaces, we only need to define an interface for the DAO layer, for example:

Interface Userresposity extends Mongorepository<user, string>{}, User is an entity class.

Paste the user entity class below

1 @Data2@Document (collection = "User")3  Public classUserImplementsSerializable {4 5 @Id6     PrivateString ID;7 8     /**9 * TelephoneTen      */ One@Indexed (unique =true) A     PrivateString Telephone; -  -  the     /** - * Nickname -      */ - @Indexed +     PrivateString nickname; -  +     /** A * Avatar Address at      */ -     PrivateString Avatar; -  -  -     /** - * Date of birth in      */ -     PrivateLong birthday; to  +     /** - * Gender the      * *      */ $     PrivateInteger sex;Panax Notoginseng  -   the     /** + * Focus on users ' collections A      */ the     PrivateList<string>followinglist; +  -  $  $}

The Userresposity interface code is as follows:

1 @Repository 2  Public Interface extends Mongorepository<user, string> {34    User findbytelephone (String telephone) ; 5 }

We just need to write the interface, not to write the implementation of the interface. For example, the Findbytelephone method, telephone is a property of the user class

The basic naming method for interface methods is find + by + entity class property name (first capitalization)+ query criteria (capitalized)

Query condition is like, used SQL everyone knows, is the meaning of fuzzy query.

There are also GreaterThan (greater than), LessThan (less than), between (in ... ), Isnotnull, notnull (whether non-empty), near (query geographically similar) and so on. View official website document https://docs.spring.io/spring-data/mongodb/docs/1.10.13.RELEASE/reference/html/#repositories. Definition

If you need to query the method has more than one field can be connected with and.

If you want to use this interface, you need to inject the interface. The code is as follows:

1 @Autowired 2 private userrepository userrepository;

MongoDB Learning One (using mongoresposity)

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.