JPA Query Methods

First, the use where conditions
on the use of JPA we had a data source access, the default JPA has achieved several interfaces can be called. However, in actual business, the query will inevitably need to use where, order by other statements.

We do with product data example, add a price field price, by price range queries, take a look at how to achieve.

One way: Name achieved by a method
public interface GoodsRepository the extends JpaRepository <Goods, Long> {
List <Goods> findByPriceBetween (Double startPrice, Double endPrice);
}
the Spring of the Data supporting the JPA keyword query method (refer to: https: // docs.spring.io/spring-data/jpa/docs/2.2.x/reference/html/#repositories.query-methods)

Table 2.2. Supported keywords inside method names

 

 

 

 

Second way: custom SQL is achieved by
in reality, may contain very complex SQL statements, or to optimize performance, we need to customize sql. JPA annotations and XML configuration that provides 2 ways.

the extends JpaRepository interface GoodsRepository public <Goods, Long> {
@Query (value = "SELECT * WHERE g.price BETWEEN G from Goods: startPrice and: endPrice", to true nativeQuery =)
List <Goods> findByPriceBetween (Double startPrice, Double endPrice) ;
}
two, order by queries
use may be prepared in order by the method name to be changed to the above example

public interface GoodsRepository extends JpaRepository<Goods, Long> {
List<Goods> findByPriceBetweenOrderByPriceAsc(Double startPrice, Double endPrice);

----------------
Disclaimer: This article is the original article CSDN bloggers "loophome", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/loophome/article/details/87186778

Guess you like

Origin www.cnblogs.com/fengli9998/p/11904265.html
Recommended