Micro personnel fifth day: Spring Data Jpa keyword defines query methods

Although there are well-defined method Jpa, but it is not enough that we use, I need to define some of the methods in the interface itself.
1.BookDao
here define a query based on id Book

package org.javaboy.jpa.dao;

import org.javaboy.jpa.bean.Book;
import org.springframework.data.jpa.repository.JpaRepository;

//操作实体类Book
//继承接口,第一个泛型是你要操控的实体类,第二个反应是id的属性
public interface BookDao extends JpaRepository<Book,Integer> {

    Book findBookById(Integer id);

}

2. Test class
query id = 3 books

@Test
    public void find4() {
        Book book = bookDao.findBookById(3);
        System.out.println(book);
    }

Control statements and print sql query results

Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.name as name3_0_ from book book0_ where book0_.id=?
Book{id=3, name='西游记', author='吴承恩'}

dao layer, we can hold the rules to define a method to meet the requirements.
Supported query key as shown below:
Here Insert Picture Description
The following table let us define some more query method:
are larger than the query id id data and query data or less than name of fuzzy query.

 List<Book> findByIdGreaterThan(Integer id);

 List<Book> findBookByIdLessThanOrNameContaining(Integer id,String name);

Test categories:

 @Test
    public void find5() {
        List<Book> list = bookDao.findByIdGreaterThan(3);
        System.out.println(list);
        List<Book> list1 = bookDao.findBookByIdLessThanOrNameContaining(3, "楼");
        System.out.println(list1);


    }

You can see the query was successful, sql query statements and query results are printed out.

[Book{id=4, name='红楼梦', author='曹雪芹'}]
Hibernate: select book0_.id as id1_0_, book0_.author as author2_0_, book0_.name as name3_0_ from book book0_ where book0_.id<? or book0_.name like ? escape ?
[Book{id=1, name='三国演义', author='罗贯中'}, Book{id=2, name='水浒传', author='施耐庵'}, Book{id=4, name='红楼梦', author='曹雪芹'}]

Query method flow analysis:
Why write the name of the method, JPA will know that you like doing it? If create the following query: findByUserDepUuid (), the frame when parsing the method, firstly findBy removed, and then analyzing the remaining attributes, the querying entity is assumed Doc:

1. First determination userDepUuid (POJO according to specifications, the first letter lowercase) to query whether the attributes of an entity, and if so, then according to the attribute query; Without this attribute, the second step continues;
2. right to left interception first begin with a capital letter string (here Uuid), then check the rest of the query string is a property of the entity, if it is, then the query based on the attribute; if not the attribute, the second step is repeated to continue taken from right to left; final hypothesis is query a user entity attribute;
3 then processes the remaining portion (depUuid), corresponding to the first user determines whether there depUuid type attributes, if any, indicates the method according to ultimately query "Doc.user.depUuid" values; otherwise, continue to step 2 according to the rules taken from right to left, represents the final query based on the value "Doc.user.dep.uuid" a.
4. There may be a special case, such a user attribute Doc comprising, userDep also has a property, then there will be confusion. Properties between plus clear "_" explicit expression of intent, such as "findByUser_DepUuid ()" or "findByUserDep_uuid ()"
5. The also some special parameters: for example, sort tab or parameters:

Published 287 original articles · won praise 24 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41998938/article/details/104062026