In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to create a method name for spring data jpa for simple query", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to create a method name for spring data jpa to make a simple query" can help you solve your doubts.
Spring data jpa can query by creating a method in the interface according to the specified syntax. In the basic spring data jpa interface, such as findOne,save,delete in CrudRepository, how can we create a method to query as needed?
The most common practice is
Declare that an interface inherits from CrudRepository or PagingAndSortingRepository,JpaRepository,Repository
Public interface TaskDao extends JpaRepository {}
Or use annotations to inherit table names from JpaRepository, for example, the following two are equivalent
RepositoryDefinition (domainClass = Task.class, idClass = Long.class) public interface TaskDao {} public interface TaskDao extends JpaRepository {}
Inheriting CrudRepository or PagingAndSortingRepository,JpaRepository will extract some commonly used methods. If your spring data jpa helps you customize so many methods, you can inherit from JpaRepository, and then copy some methods to your interface. You can optionally ask for some methods.
@ NoRepositoryBeaninterface MyBaseRepository extends Repository {T findOne (ID id); T save (T entity);} interface TaskDao extends MyBaseRepository {} create query methods according to the specification
Generally, according to the java hump writing specification, some specific keywords are added. For example, we want to get the list of task entity classes through the task name.
Use attributes to get task list
Interface TaskDao extends MyBaseRepository {List findByName (String name);}
Use and and or to get the task list
Interface TaskDao extends JpaRepository {List findByNameAndProjectId (String name,Long projectId); List findByNameOrProjectId (String name,Long projectId);}
Using Pageable and Sort,Slice to get paged task list and sorting
Interface TaskDao extends JpaRepository {Page findByName (String name,Pageable pageable); Slice findByName (String name,Pageable pageable); List findByName (String name, Sort sort);}
Weight removal by Distinct
Interface TaskDao extends JpaRepository {List findDistinctTaskByNameOrProjectId (String name, Long projectId);}
Using OrderBy to sort
Interface TaskDao extends JpaRepository {List findByNameOrderByProjectIdDesc (String name, Long projectId);}
Using Top and First to obtain restricted data
Interface TaskDao extends JpaRepository {User findFirstByOrderByLastnameAsc (); Task findTopByOrderByNameDesc (String name); Page queryFirst10ByName (String name, Pageable pageable); Slice findTop3ByName (String name, Pageable pageable); List findFirst10ByName (String name, Sort sort); List findTop10ByName (String name, Pageable pageable);}
So how does spring data jpa assemble query statements through these specifications?
When parsing the method name, the Spring Data JPA framework will first truncate the redundant prefixes of the method name, such as find, findBy, read, readBy, get, getBy, and then parse the rest.
If you create the following query: findByTaskProjectName (), when the framework parses the method, it first removes findBy, and then parses the remaining attributes, assuming that the query entity is Doc
First determine whether taskProjectName (according to the POJO specification, the initials are lowercase) is an attribute of the query entity, if so, it means to query according to the attribute; if not, proceed to the second step
Truncate the string starting with the first uppercase letter from right to left, here Name), and then check whether the remaining string is an attribute of the query entity, if so, it indicates that the query is based on the attribute; if not, repeat the second step and continue to intercept from right to left; finally, assume that task is an attribute of the query entity Person
Then deal with the rest (ProjectName), first determine whether the type corresponding to task has a projectName attribute, if so, it means that the method is finally queried according to the value of "Person.task.projectName"; otherwise, continue to intercept from right to left according to the rules of step 2, and finally indicate that the query is based on the value of "Person.task.project.name".
There may be a special case, such as Person contains an attribute of task, there is also a property of projectName, there will be confusion. You can explicitly add "_" between attributes to explicitly express your intention, such as "findByTask_ProjectName ()"
Supported canonical expressions
Here the entity is User, and there are firstName and lastName,age
Expression example hql query statement AndfindByLastnameAndFirstname … Where x.lastname =? 1 and x.firstname =? 2OrfindByLastnameOrFirstname... Where x.lastname =? 1 or x.firstname =? 2IsMagnedByFirstNameFindByFirstnameIsFindByFirstname Isequal... Where x.firstname = 1?BetweenfindByStartDateBetween... Where x.startDate between 1? And? 2LessThanfindByAgeLessThan... Where x.age
< ?1LessThanEqualfindByAgeLessThanEqual… where x.age ⇐ ?1GreaterThanfindByAgeGreaterThan… where x.age >? 1GreaterThanEqualfindByAgeGreaterThanEqual... Where x.age > =? 1AfterfindByStartDateAfter... Where x.startDate >? 1BeforefindByStartDateBefore... Where x.startDate 1IsNullfindByAgeIsNull... Where x.age is nullIsNotNull,NotNullfindByAge (Is) NotNull... Where x.age not nullLikefindByFirstnameLike... Where x.firstname like? 1NotLikefindByFirstnameNotLike... Where x.firstname not like? 1StartingWithfindByFirstnameStartingWith... Where x.firstname like? 1 (parameter bound with appended%) EndingWithfindByFirstnameEndingWith … Where x.firstname like? 1 (parameter bound with prepended%) ContainingfindByFirstnameContaining … Where x.firstname like? 1 (parameter bound wrapped in%) OrderByfindByAgeOrderByLastnameDesc … Where x.age =? 1 order by x.lastname descNotfindByLastnameNot... Where x.lastname? 1InfindByAgeIn (Collection ages)... Where x.age in? 1NotInfindByAgeNotIn (Collection age)... Where x.age not in? 1TruefindByActiveTrue ()... Where x.active = trueFalsefindByActiveFalse ()... Where x.active = falseIgnoreCasefindByFirstnameIgnoreCase... Where UPPER (x.firstame) = UPPER (? 1) read here, this article "how to create a method name for simple query" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.