Spring Data JPA Simple Query

Source: Internet
Author: User

First, the common rules of fast-track


1 and and
2 or OR
3 is,equals equals
4 between between the two
5 LessThan less than
6 lessthanequal less than equals
7 GreaterThan greater than
8 greaterthanequal greater than or equal
9 After (time) >
Before before (time) <
One IsNull equals null
Isnotnull,notnull not equal to NULL
Like fuzzy query. You need to add% in your enquiry
The notlike is not within the blur range. You need to add% in your enquiry
Startingwith at a certain beginning
Endingwith with a certain end
Containing contains a
ORDER BY
Not equal to
In a range
Notin a range outside
True
False false
IgnoreCase ignoring case

Spring Data Parsing method name--rule description

1. Rule description

According to the rules defined by spring data, the query method starts with Find|read|get (for example, find,findBy, read, Readby, get, Getby), and when the conditional query is involved, the properties of the condition are connected with the Conditional keyword , note that the first letter of the conditional attribute should be capitalized. When parsing a method name, the framework first intercepts the extra prefix of the method name and then parses the rest.

If the last parameter of the method is a sort or pageable type, the relevant information is also extracted for sorting by rules or for paging queries.

2. Examples and explanations

such as FindByuseraddresszip(). When parsing the method, the framework first rejects the findBy, then parses the rest of the attributes, with detailed rules as follows (this assumes that the method is for the domain object AccountInfo type):

    1. First Judge Useraddresszip (according to the POJO specification, the first letter to lowercase, the same as the same) is a property of AccountInfo, if so, the query based on the property, if not, continue the second step;
    2. from right to left , the string that begins with the first capital letter ( Ziphere), and then checks whether the remaining string is a property of AccountInfo, or, if it is, a query based on that property, or, if there is no property, repeats the second step, Continue to intercept from right to left, and finally assume that user is a property of AccountInfo;
    3. Then processing the remainder ( addresszip ), first determine whether the user's corresponding type has a addresszip attribute, if any, it is ultimately based on the "ACCOUNTINFO.USER.ADDRESSZIP" value to query Otherwise, continue to intercept from right to left according to the rules of Step 2, which ultimately indicates a query based on the value of "AccountInfo.user.address.zip".

There may be a special case, such as AccountInfo contains a user attribute and a useraddress attribute, there is confusion. Readers can explicitly add "_" between attributes to explicitly express intentions, such as "Findbyuser_addresszip ()" or "Findbyuseraddress_zip ()". (It is strongly recommended that you add "_" between different class levels to increase code readability, whether or not there is confusion)

Iii. some cases

1. When the query condition is null.

Examples are as follows:

    • entity definition: for a customer entity Cus, contains name and sex, all of which are string types.
    • Query method definition: List<cus> findbynameandsex (String name,string sex);
    • when used:dao.findbynameandsex (null, "male");
    • background generated SQL fragment:where (Cus0_.name is null) and cus0_.sex=?
    • Conclusion: when the value of a query is null, only records in the database that are null for the field are eligible, not to mention ignoring this condition. In other words, this kind of query method is only suitable for the business that the query condition must be transmitted, for the dynamic query (the condition is dynamic, such as the General query list, when the end user decides to enter those query conditions), this simple query cannot satisfy the requirement.

2. Sorting

list<cus> findbysexorderbyname (String sex); list<cus> findbysexorderbynameasc (String Sex) When the name is positive (in the positive order, this method is recommended); Name Positive order (effect Ibid.) list<cus> Findbysexorderbynamedesc (String sex); Reverse Name

Spring Data JPA Simple Query

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.