5.3.5. 使用Sort

5.3.5. 使用Sort

5.3.5. Using Sort 提供PageRequest或者直接使用Sort可以启用排序。在Sort的实例Order中使用的属性需要匹配你的域模型,意味着需要匹配一个属性或者在查询中使用别名。JPQL将此定义为一个状态字段路径表达式。 Sorting can be done be either providing a PageRequest or by using Sort directly. The properties actually used within the Order instances of Sort need to match your domain model, which means they need to resolve to either a property or an alias used within the query. The JPQL defines this as a state field path expression. 但同时使用@Query和Sort会使Order实例的ORDER BY语句中包含函数。原因是Order添加了指定的查询字符串。默认Spring Data JPA拒绝所有包含函数调用的Order实例,但你可以使用JpaSort.unsafe来添加潜在的非安全排序。 However, using Sort together with @Query lets you sneak in non-path-checked Order ins…

阅读全文 Read More

5.3.4. 使用@Query

5.3.4. 使用@Query

5.3.4. Using @Query 使用命名查询来为实体声明查询,对于小数据量的查询来说个不错的选择。查询本身会绑定被执行的Java方法,你可以使用Spring Data JPA @Query注解,而不是注解在域类上,来绑定对应方法。这种做法将域类从特定持久化信息中释放出来,并协同定位查询所属的库接口。 Using named queries to declare queries for entities is a valid approach and works fine for a small number of queries. As the queries themselves are tied to the Java method that executes them, you can actually bind them directly by using the Spring Data JPA @Query annotation rather than annotating them to the domain class. This frees the domain class from persistence specific information and co-locates the query to the repository interface. 注解在方法上的查询优先级高于使用@NamedQuery定义的查询和在orm.xml声明的命名查询。 Queries ann…

阅读全文 Read More

5.3.3. 使用JPA命名查询

5.3.3. 使用JPA命名查询

5.3.3. Using JPA Named Queries XML命名查询定义 XML Named Query Definition 可以在classpath下的META-INF文件夹中的orm.xmlJPA配置文件中,添加<named-query />元素来使用XML配置。通过使用名义的命名转换,来启用命名查询的自动调用。下面是详细例子。 To use XML configuration, add the necessary <named-query /> element to the orm.xml JPA configuration file located in the META-INF folder of your classpath. Automatic invocation of named queries is enabled by using some defined naming convention. For more details, see below. 例53. XML命名查询配置 Example 53. XML named query configuration <named-query name=”User.findByLastname”> <query>select u from User u where u.lastname = ?1</query> </named-query> 此查询有一…

阅读全文 Read More

5.3.2. 查询创建

5.3.2. 查询创建

5.3.2. Query Creation JPA的查询创建的机制通常来说都像“查询方法”中描述的一样。下面的例子展示了一个JPA查询方法的转换: Generally, the query creation mechanism for JPA works as described in “Query methods”. The following example shows what a JPA query method translates into: 例52. 从方法名创建查询 Example 52. Query creation from method names public interface UserRepository extends Repository<User, Long> { List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); } 我们使用JPA标准API来创建一个查询,但本质上是被翻译为了:select u from User u where u.emailAddress = ?1 and u.lastname = ?2。Spring Data JPA做了属性检查与属性转换,正如“属性表达式”中描述的一样。 We create a query using the JPA criteria API from this, but, essential…

阅读全文 Read More

5.3.1. 查询策略

5.3.1. 查询策略

5.3.1. Query Lookup Strategies JPA模块支持手动定义一个查询为字符串,或者通过方法命名推导。 The JPA module supports defining a query manually as a String or having it being derived from the method name. 声明查询 Declared Queries 虽然从方法名中推导得到查询的方式比较便捷,但也会面临一个问题,方法名解析器不支持要用到的关键词,或者通过方法名得到的不是预期的结果。所以你可以通过命名规范(请查阅使用JPA命名查询来获取更多信息)来使用JPA命名查询,或者使用@Query(请查询使用@Query获取更新信息)来注解你的查询方法。 Although getting a query derived from the method name is quite convenient, one might face the situation in which either the method name parser does not support the keyword one wants to use or the method name would get unnecessarily ugly. So you can either use JPA named queries through a naming convention (see Usin…

阅读全文 Read More

3.4 定义查询方法

3.4 定义查询方法

3.4. Defining query methods 库代理有两种方法从方法名中提取指定存储的查询。一是可以直接从方法名中提取查询,二是使用手动定义的查询。使用哪一个取决于实际的存储。但必须有一种策略来决定实际创建的查询。接下来看看这两种方法。 The repository proxy has two ways to derive a store-specific query from the method name. It can derive the query from the method name directly, or by using a manually defined query. Available options depend on the actual store. However, there’s got to be a strategy that decides what actual query is created. Let’s have a look at the available options. 3.4.1. 查询策略 3.4.1. Query lookup strategies 库基础设施有下列策略来解决查询问题。你可以通过XML配置中的query-lookup-strategy属性,或者在Java config中的Enable${store}Repositories注解通过queryLookupStrategy属性,在命名空间下配置策略。其中某些策略可…

阅读全文 Read More

3.3 定义库接口

3.3 定义库接口

3.3. Defining repository interfaces 首先你需要定义一个指定域类的库接口。接口必须继承Repository并指定域类和ID的类型。如果你希望暴露CRUD方法,将继承Repository更换为CrudRepository即可。 As a first step you define a domain class-specific repository interface. The interface must extend Repository and be typed to the domain class and an ID type. If you want to expose CRUD methods for that domain type, extend CrudRepository instead of Repository. 3.3.1 微调库定义 3.3.1. Fine-tuning repository definition 一般你的库接口会继承Repository, CrudRepository或者PagingAndSortingRepository。如果你不想继承Spring Data的接口,还有一种做法是将你的库接口注解为@RepositoryDefinition。继承CrudRepository将暴露一系列的操作实体的方法。如果你希望选择性的暴露这些方法,可以将想要暴露的方法从CrudRepository复制到你的域库中。 Typically…

阅读全文 Read More

3.2 查询方法

3.2 查询方法

3.2. Query methods 标准CRUD功能库通常都有各种查询。在Spring Data中,声明这些查询需要四个步骤: Standard CRUD functionality repositories usually have queries on the underlying datastore. With Spring Data, declaring those queries becomes a four-step process: 继承Repository或者它的一个子接口来声明一个接口,并定义其需要处理的域类的类型与ID类型。 Declare an interface extending Repository or one of its subinterfaces and type it to the domain class and ID type that it will handle. interface PersonRepository extends Repository<Person, Long> { … } 在此接口中声明查询方法。 Declare query methods on the interface. interface PersonRepository extends Repository<Person, Long> { List<Person> findByLastname(String lastname); } 设…

阅读全文 Read More

3.1 核心概念

3.1 核心概念

3.1. Core concepts 在Spring Data仓库抽象中的中心接口是Repository。此接口将其id作为参数并使用域类来管理。此接口主要作为一个标记接口来捕获起效的类型并帮助你发现继承此接口的类型。CrudRepository为管理的实体类提供了复杂的CRUD功能。 The central interface in Spring Data repository abstraction is Repository (probably not that much of a surprise). It takes the domain class to manage as well as the id type of the domain class as type arguments. This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one. The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed. 例3:CrudRepository接口 Example 3. CrudRepository interf…

阅读全文 Read More

二分搜索 Binary search

二分搜索 Binary search

二分搜索也是比较常见的,在生活中的应用尤其广泛。比如在酒桌上玩猜数字,一个人坐庄先准备一个在一定范围内的数字,其余每个人轮流猜一次,庄说出是大了还是小了,说中的人喝。这个游戏玩过的人应该不少,其中肯定有许多人都是从中间开始猜,认为这样是比较快的排除方法。是的,这就是二分搜索。 二分搜索对于查找对象有要求,即有序。原理就是取其中间进行判断,如果未找到,则根据顺序关系再分别对两边其中的一个子区进行再次查找,直到找到或者未发现,搜索结束。 public static void main(String[] args) { int[] array = {1, 12, 13, 27, 34, 38, 49, 50, 64, 65, 76, 78, 97}; int target = 50; System.out.println(“result index:” + binarySearch(array, target)); } public static int binarySearch(int[] arr, int target) { int startPosition = 0; // 设置搜索的上下界 int endPosition = arr.length – 1; while (startPosition <= endPosition) { // 在搜索过程中,会逐渐缩小搜索范围 int midPosition = (startPosition + endPosition) / 2; // 确定中间…

阅读全文 Read More