Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a method name for simple query by spring data jpa

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

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report