首页 > 编程语言 > 详细

Spring JPA使用Tips

时间:2020-06-08 10:47:18      阅读:33      评论:0      收藏:0      [点我收藏+]

命名查询的前缀定义可以在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

Spring JPA使用Tips

原文:https://www.cnblogs.com/echo1937/p/13064018.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!