AI Hints
Home›Spring Boot Tutorial›Derived Query Methods, More Query Keywords and Sorting
Spring Boot · Lesson 56 of 120

More Query Keywords

And, Between, Containing.

⏱ 2 min read▶ Video at 1:15:43🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Method names can do a lot more. Combine conditions, search inside text, and even ignore upper and lower case.

  • Combine conditions
  • Search inside text
  • Ignore case

Useful keywords

And, and Or, combine conditions. Between needs two values. Containing searches inside text, and Starting With checks the beginning. Ignore Case ignores capital letters. In matches any value in a list. And First, or Top, limits the results.

KeywordExample
And / OrfindByAuthorAndPriceLessThan
BetweenfindByPriceBetween(min, max)
Containing / StartingWithfindByTitleContaining
IgnoreCase...ContainingIgnoreCase
InfindByAuthorIn(list)
First / TopfindFirstByOrderByPriceDesc

Richer queries

Here are six richer methods. Author and price combines two conditions. Price between takes a range. Containing ignore case searches the title, in any case. Starting with checks the start. Author in takes a list. And find first, ordered by price descending, gives the most expensive book.

BookRepository.javajava
public interface BookRepository extends JpaRepository<Book, Long> {

    List<Book> findByAuthorAndPriceLessThan(String author, double max);

    List<Book> findByPriceBetween(double min, double max);

    List<Book> findByTitleContainingIgnoreCase(String text);

    List<Book> findByTitleStartingWith(String prefix);

    List<Book> findByAuthorIn(List<String> authors);

    Optional<Book> findFirstByOrderByPriceDesc();
}

Trying them out

We call five of them. Run it. Herbert has one book under nine. Two books cost between nine and ten. Dune matches two titles, in any case. Two titles start with the. And the priciest book is The Fellowship of the Ring.

QueryDemo.javajava
@Component
public class QueryDemo implements CommandLineRunner {

    private final BookRepository books;

    public QueryDemo(BookRepository books) { this.books = books; }

    @Override
    public void run(String... args) {
        System.out.println("Herbert under 9: "
            + books.findByAuthorAndPriceLessThan("Frank Herbert", 9));
        System.out.println("9 to 10: " + books.findByPriceBetween(9, 10));
        System.out.println("'dune': " + books.findByTitleContainingIgnoreCase("dune"));
        System.out.println("Starts with 'The': " + books.findByTitleStartingWith("The"));
        System.out.println("Priciest: " + books.findFirstByOrderByPriceDesc());
    }
}
Output
Herbert under 9: [Book 4: Children of Dune (8.49)]
9 to 10: [Book 1: Dune (9.99), Book 5: Song of Achilles (9.49)]
'dune': [Book 1: Dune (9.99), Book 4: Children of Dune (8.49)]
Starts with 'The': [Book 2: The Hobbit (8.99), Book 6: The Fellowship of the Ring
(11.99)]
Priciest: Optional[Book 6: The Fellowship of the Ring (11.99)]
Also uses Book.java
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private double price;

    protected Book() {}

    public Book(String title, String author, double price) {
        this.title = title; this.author = author; this.price = price;
    }

    public Long getId() { return id; }
    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }

    @Override
    public String toString() { return "Book " + id + ": " + title + " (" + price + ")"; }
}
Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {

    List<Book> findByAuthorAndPriceLessThan(String author, double max);

    List<Book> findByPriceBetween(double min, double max);

    List<Book> findByTitleContainingIgnoreCase(String text);

    List<Book> findByTitleStartingWith(String prefix);

    List<Book> findByAuthorIn(List<String> authors);

    Optional<Book> findFirstByOrderByPriceDesc();
}
Also uses data.sql
INSERT INTO book (title, author, price) VALUES
  ('Dune', 'Frank Herbert', 9.99),
  ('The Hobbit', 'J.R.R. Tolkien', 8.99),
  ('Circe', 'Madeline Miller', 10.99),
  ('Children of Dune', 'Frank Herbert', 8.49),
  ('Song of Achilles', 'Madeline Miller', 9.49),
  ('The Fellowship of the Ring', 'J.R.R. Tolkien', 11.99),
  ('Emma', 'Jane Austen', 6.99),
  ('Persuasion', 'Jane Austen', 7.49);
Also uses application.properties
spring.jpa.defer-datasource-initialization=true

Summary

Keywords like And, Between, Containing, Ignore Case, and First, build rich queries from method names. Next, sorting.

Key takeaways
  • And / Or
  • Between, Containing
  • IgnoreCase, First
Next up: Sorting — put results in order