本文记录了Spring data JPA 的一些细碎的规则。
比如:
User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
List<User> findFirst10ByLastname(String lastname, Sort sort);
List<User> findTop10ByLastname(String lastname, Pageable pageable);
And:等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd);
Or:等价于 SQL 中的 or 关键字,比如 findByUsernameOrAddress(String user, String addr);
Between:等价于 SQL 中的 between 关键字,比如 findBySalaryBetween(int max, int min);
public interface UserRepository extends JpaRepository<User, Long> {
@Query("select u from User u where u.firstname like %?1")
List<User> findByFirstnameEndsWith(String firstname);
}
当nativeQuery = true时,填入的是原生的SQL
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
User findByEmailAddress(String emailAddress);
}
rewriteBatchedStatements=true
generate_statistics: true
<Logger name="org.hibernate" level="info" includeLocation="true">
</Logger>
见参考链接