First, method name resolution
1. Introduction
Review the DAO interface in the HelloWorld project
Public Interface extends Jparepository<girl, integer>{ // New Custom Query method list<girl> Findbyage (Integer age);}
As you can see, in this interface we just declare the method without writing the implementation, but it is available, and this is thanks to the JPA method name parsing
2. Simple condition Query
According to the specifications of Spring Data, the query method begins with a Find | read | get
When a conditional query is involved, the properties of the condition are concatenated with the conditional keyword, note that the conditional attribute is capitalized in the first letter
The common keywords are as follows:
and---equivalent to the AND keyword in SQL, such as Findbyusernameandpassword (String user, Striang pwd); ---equivalent to or keyword in SQL, such as findbyusernameoraddress (string user, String addr);between The---is equivalent to the between keyword in SQL, such as Findbysalarybetween (intMaxintmin);LessThan ---is equivalent to "<" in SQL, such as Findbysalarylessthan (intmax);GreaterThan ---is equivalent to ">" in SQL, such as Findbysalarygreaterthan (intmin);IsNull ---is equivalent to "is null" in SQL, such as Findbyusernameisnull ();isnotnull ---equivalent to "is not NULL" in SQL, such as Findbyusernameisnotnull ();notnull ---equivalent to Isnotnull; ---is equivalent to "like" in SQL, such as Findbyusernamelike (String user);
Startingwith notlike ---equivalent to "not-like" in SQL, such as Findbyusernamenotlike (String user) ;---is equivalent to "order by" in SQL, such as FINDBYUSERNAMEORDERBYSALARYASC (String user) ;---is equivalent to "!" in SQL! =", such as Findbyusernamenot (String user) ;The---is equivalent to "in" in SQL, such as Findbyusernamein (collection<string>userlist), the parameter of the method can be Collection type, or it can be an array or an indefinite length parameter;notin ---is equivalent to "not in" SQL, such as Findbyusernamenotin (Collection<string> userlist), the parameters of the method can be either a Collection type or an array or an indefinite length parameter;
Full keyword support, view official documents: Click to view
Idea has a very friendly support for this area! Write method parsing automatically have smart tips!
Example:
List<girl> Findbynamestartingwithandidlessthan (String name, Integer ID);
/*** 1. Repository is an empty interface. That is a labeled interface * 2. If the interface we define inherits Repository, the interface is recognized by the IOC container as a Repository Bean. * Included in the IOC container. In this interface, a method that satisfies a certain specification can be defined. * * 3. In fact, you can also override the inherited Repository interface by @RepositoryDefinition annotations*//*** Declare the method in the Repository sub-interface * 1. It is not a casual statement. and need to conform to certain specifications * 2. Query method with Find | read | get start * 3. When a conditional query is involved, the properties of the condition are connected with the Conditional keyword * 4. Note Yes: The conditional attribute is capitalized in the first letter. * 5. cascading queries that support attributes. If the current class has properties that match the criteria, it takes precedence instead of cascading properties. * If you need to use cascading properties, use _ to connect between attributes. *///@RepositoryDefinition (Domainclass=person.class,idclass=integer.class) Public InterfacePersonrepsotoryextendsjparepository<person, integer>, Jpaspecificationexecutor<Person>, persondao{//according to LastName to get the corresponding personPerson getbylastname (String lastName); //WHERE lastName like?% and ID <?List<person>Getbylastnamestartingwithandidlessthan (String lastName, Integer ID); //WHERE lastName like%? and ID <?List<person>Getbylastnameendingwithandidlessthan (String lastName, Integer ID); //WHERE Email in (?,?,?) OR Birth <?List<person> Getbyemailinandbirthlessthan (list<string>emails, Date birth); //WHERE a.id >?List<person> Getbyaddress_idgreaterthan (Integer ID);
more examples
Other keywords are similar, you can see the printed SQL in the console (configured with show SQL)
Of course, the shortcomings are very obvious, the method name is particularly long! This later will be improved through JPQL!
Second, custom query
For example, the tape query can not be implemented, here you need to customize SQL to query!
Reference: https://www.cnblogs.com/zj0208/p/6008627.html
SPRING-DATA-JPA Quick Start (ii)--Simple query