Springboot from getting started to getting started (seven orm-jpa-3 simple queries)

wwz_ henu 2022-02-13 05:19:49 阅读数:509

springboot getting started getting started

origin: https://www.cnblogs.com/rulian/p/6434631.html

JPA Can be parsed find The first method generates a query , Reduce SQL Statement writing

1. The rules :find+ Global decoration +By+ The attribute name of the entity + key word + Conjunctions + …( Other entity properties )+OrderBy+ Sort properties + Sort direction .

// Equate to select * from user where name = ?1
List<UserEntity> findByName(String name);
Global decoration Distinct, Top, First  (Top First Number of traceable entries , The default is 1  example Top10)
qualifier

IsNull, IsNotNull, Like, NotLike, Containing, In, NotIn,

IgnoreCase, Between, Equals, LessThan, GreaterThan, After, Before…

Conjunctions And, Or
Sort direction Asc, Desc

2. Sort

With findAll Methods as an example , This method can receive Sort Instance to sort

 /*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
List<T> findAll(Sort sort);

Sort Provides Sort.by And other methods to get Sort example

 

// List<UserEntity> list = userRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
// List<UserEntity> list = userRepository.findAll(Sort.by("id"));
// Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
// List<UserEntity> list = userRepository.findAll(Sort.by(order));
// Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
// Sort.Order[] orders = new Sort.Order[1];
// orders[0] = order;
// List<UserEntity> list = userRepository.findAll(Sort.by(orders));
// Sort.Order order = new Sort.Order(Sort.Direction.DESC,"id");
// List<Sort.Order> orderList = new ArrayList<>();
// orderList.add(order);
// List<UserEntity> list = userRepository.findAll(Sort.by(orderList));

 3. Pagination

With findAll Methods as an example , This method can receive Pageable Instance for paging

Page<T> findAll(Pageable pageable);
PageRequest Provides of And other methods to get PageRequest example 

 int page = 1;
int pageSize = 1;
Sort.Direction direction = Sort.Direction.DESC;
// List<UserEntity> list = userRepository.findAll(PageRequest.of(page,pageSize)).getContent();
// Can be sorted at the same time
// Sort sort = Sort.by("id");
// List<UserEntity> list = userRepository.findAll(PageRequest.of(page,pageSize,sort)).getContent();
// Can be sorted at the same time
// List<UserEntity> list = userRepository.findAll(PageRequest.of(page,pageSize,direction,"id")).getContent();
// Equivalent to PageRequest.of(0, pageSize);
// List<UserEntity> list = userRepository.findAll(PageRequest.ofSize(pageSize)).getContent();

copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130519473308.html