码迷,mamicode.com
首页 > 编程语言 > 详细

Spring JPA使用Tips

时间:2020-06-08 10:44:27      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:done   read   sel   pac   streams   相关   using   问题   tips   

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

标签:done   read   sel   pac   streams   相关   using   问题   tips   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!