命名查询的前缀定义可以在PartTree类找到,
package org.springframework.data.repository.query.parser; public class PartTree implements Streamable<OrPart> { // ... private static final String QUERY_PATTERN = "find|read|get|query|stream"; private static final String COUNT_PATTERN = "count"; private static final String EXISTS_PATTERN = "exists"; private static final String DELETE_PATTERN = "delete|remove"; // ... }
1、count和exists前缀让我们可以使用命名查询获取查询结果;
@Query(" select count(t) from FollowerInfo t where investUserId = :invUserId") Integer findFollowerNumberByInvUserId(@Param("invUserId") Long invUserId); // 效果等同于 Long countByInvestUserId(Long investUserId);
2、stream和Pageable可以优化程序的内存占用(类似Python的生成器)
public interface UserRepository extends JpaRepository<User, Integer> { // ... Page<User> findAll(Pageable pageable); // ... } public interface UserRepository extends JpaRepository<User, Integer> { // ... Stream<User> findAllByName(String name); // ... }
3、Optional可以有效解决Null值问题
public interface UserRepository extends JpaRepository<User, Integer> { Optional<User> findOneByName(String name); }
4、xxxxxxxxx
相关链接:
https://www.baeldung.com/spring-data-java-8
http://knes1.github.io/blog/2015/2015-10-19-streaming-mysql-results-using-java8-streams-and-spring-data.html
原文:https://www.cnblogs.com/echo1937/p/13064018.html