0
0
Spring Bootframework~30 mins

Custom query methods by naming convention in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom Query Methods by Naming Convention in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage a list of books in a library. You want to find books by their title and by their author using Spring Data JPA's custom query methods by naming convention.
🎯 Goal: Create a Spring Data JPA repository interface with custom query methods that find books by their exact title and by their author's name.
📋 What You'll Learn
Create a Book entity class with fields id, title, and author.
Create a repository interface called BookRepository that extends JpaRepository.
Add a custom query method findByTitle to find a book by its exact title.
Add a custom query method findByAuthor to find all books by a given author.
💡 Why This Matters
🌍 Real World
Finding data by specific fields is common in applications like libraries, stores, or user management systems. Spring Data JPA lets you write simple method names to get this functionality without writing SQL.
💼 Career
Knowing how to create custom query methods by naming convention is a key skill for Java backend developers working with Spring Boot and databases.
Progress0 / 4 steps
1
Create the Book entity
Create a class called Book with private fields Long id, String title, and String author. Annotate it with @Entity and mark id as the primary key with @Id and @GeneratedValue.
Spring Boot
Need a hint?

Use @Entity on the class, @Id and @GeneratedValue on the id field.

2
Create the BookRepository interface
Create an interface called BookRepository that extends JpaRepository<Book, Long>.
Spring Boot
Need a hint?

Extend JpaRepository<Book, Long> in the interface declaration.

3
Add custom query method to find by title
Inside the BookRepository interface, add a method declaration Book findByTitle(String title); to find a book by its exact title.
Spring Boot
Need a hint?

Use the method name findByTitle with a String title parameter.

4
Add custom query method to find by author
Inside the BookRepository interface, add a method declaration List<Book> findByAuthor(String author); to find all books by the given author. Import java.util.List.
Spring Boot
Need a hint?

Use List<Book> as the return type and import java.util.List.