Learning Spring-Data-Jpa (eight) --- defined method to query

1, the query strategy 
spring-data There are three ways to query strategy:
QueryLookupStrategy.Key.CREATE, try the method name in accordance created. General procedure is to remove the prefix from a particular set of method names, and the rest of the analytical method. If the method name does not conform to the rules, an exception is thrown.
QueryLookupStrategy.Key.USE_DECLARED_QUERY, try to find a query has been declared, if no exception is thrown. The query can be declared by the definition of notation somewhere other ways.
QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND, (the default), and combinations CREATE USE_DECLARED_QUERY. It first looks for a query statement, if the query statement can not be found, it will create a custom method name based on the query.

If we want to modify the query policy can queryLookupStrategy property @EnableJpaRepositories settings.
@EnableJpaRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
public class StudySpringDataJpaApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudySpringDataJpaApplication.class, args);
    }

}

2, query creates
2.1, spring-data built in accordance with the method name to create a query builder to build a query constraints on the physical repository useful. The method of this mechanism have the prefix find ... By, read ... By, query ... By, count ... By, and get ... By, these methods can be resolved from the rest of it. Introducing clauses may contain other expressions, such as Distinct, to set a different logo on the query to be created. However, the first delimiter By acting as an instruction to start the de facto standard. At the most basic
on the level, you can customize entity attribute condition, and use AND and OR carried out in series.

2.2, the actual results of the analytical method depends on persistent storage and create a query. Note, however, some general considerations:
2.2.1, an expression usually when the operator can connect property traversal. Attribute expressions may be used a combination of AND and OR, arithmetic may be used keyword Between, LessThan, GreaterThan, and properties as Like expression. Supported operators may be different for each database, you need to refer to the official documentation.
2.2.2 parser support method for a single attribute Ignore case (IgnoreCase), or completely ignore case (AllIgnoreCase), support ignoring case different for each database, you need to refer to the database support.
2.2.3, reference can be attached by the attribute OrderBy clause, providing the sort direction (or the Asc Desc) query method.
2.2.4 When an entity is an entity in the property, we generally use an underscore _ to manually define traverse point (so named our property to regulate the use hump).

2.3, keyword list  

     

3、特殊参数处理
框架可以识别Pageable和Sort,动态的将分页和排序应用到查询中。可以返回Page<T> 、Slice<T> 、List<T>。
3.1、Sort和Pageable不能传null值,如果不想应用排序或分页,使用Sort.unsorted()和Pageable.unpaged()。
3.2、返回Page<T>,可以知道一些附加信息,如页面总数。他会额外执行一条count语句去查询总数,如果数据量很大,会很消耗资源,可以使用下面的来替代。
3.3、返回Slice<T>,Slice的作用是,只知道是否有下一个Slice可用,不会执行count语句,所以当查询较大的数据量时,也没必要关心页数。
3.4、返回List<T>,分页也可以返回List,这种情况下,也不会额外执行count语句,仅仅是将范围内的结果装入List。

4、限制查询结果
可以通过使用first或者top关键字来限制查询方法的返回结果,可以互换使用。可以将可选的数值追加到它们后面,指定返回结果大小。如果省略数字,则假定结果大小为1。限制表达式也支持Distinct关键字。对于结果集限制为1个的示例查询,支持将结果包装到Optional中。如果将分页或切片
应用于限制查询分页(以及对可用页面数的计算),则会在限制结果内应用分页或切片(不建议分页和限制同使使用)。

5、常用的返回结果的不同形式
5.1、void,对于我们不关乎结果的操作,可以选择不返回结果,一般用作更新。
5.2、Primitives,Java的基本类型,一般用作统计返回,(如long,boolean)。
5.3、Wrapper types,Java的包装类型。
5.4、T,返回一个实体,没有查询结果,返回null,如果超过一个返回结果,抛出IncorrectResultSizeDataAccessException异常。
5.5、Iterator<T>、Collection<T>、List<T>,Set<T>,返回多个结果时,可以使用迭代器,集合,List及其子类,Set及其子类。
5.6、Optional<T>,Java8或者Guava的Optional类,查询方法最多返回一个结果,如果不存在返回Optional.empty()或Optional.absent(),如果超出一个结果,抛出IncorrectResultSizeDataAccessException异常。
5.7、Stream<T>,Java8的Stream。必须在使用后关闭流。可以使用try-with-resources,也可以使用Stream的close()方法。
5.7、Streamable<T>,Spring-Data提供的Streamable,可以作为Iterator或任何集合类型的替代。
5.8、自定义Streamable的类型,实现Streamable并提供一个将Streamable作为参数的公开的构造方法或方法名为of(…) or valueOf(…)的静态工厂方法。
5.9、Slice<T>、Page<T>,分页相关,需要提供一个Pageable入参。Page继承自Slice,Slice继承自Streamable。
5.10、Future<T>,查询方法上需要添加@Async注解,并开启Spring异步执行方法功能。
5.11、CompletableFuture<T>,Java8的CompletableFuture,需要在方法上添加@Async注解,并开启Spring异步执行方法功能。
5.12、ListenableFuture,返回org.springframework.util.concurrent.ListenableFuture,需要在方法上添加@Async注解,并开启Spring异步执行方法功能。
5.13、关联查询时,可返回Object[] 和 Map

源码地址:https://github.com/caofanqi/study-spring-data-jpa

Guess you like

Origin www.cnblogs.com/caofanqi/p/11854542.html