0
0
Spring Bootframework~10 mins

Custom query methods by naming convention in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom query methods by naming convention
Define method name in repository
Spring Data parses method name
Generate query based on keywords
Execute query when method called
Return results to caller
Spring Data reads the method name, builds a query from it, runs the query, and returns the results.
Execution Sample
Spring Boot
interface UserRepository extends JpaRepository<User, Long> {
  List<User> findByLastName(String lastName);
}
Defines a method to find users by their last name using naming convention.
Execution Table
StepActionMethod Name ParsedQuery GeneratedResult
1Method declared: findByLastNamefindByLastNameSELECT * FROM user WHERE last_name = ?Ready to execute
2Method called with lastName='Smith'findByLastNameSELECT * FROM user WHERE last_name = 'Smith'Query executed
3Database returns matching usersN/AN/AList of users with lastName 'Smith' returned
4Method returns results to callerN/AN/ACaller receives List<User>
💡 Method execution completes after returning matching users from database
Variable Tracker
VariableStartAfter Step 2After Step 3Final
methodNamenullfindByLastNamefindByLastNamefindByLastName
querynullSELECT * FROM user WHERE last_name = 'Smith'Executed queryExecuted query
resultnullnullList<User> with lastName 'Smith'List<User> with lastName 'Smith'
Key Moments - 3 Insights
How does Spring know what query to run from the method name?
Spring parses the method name (see Step 1 in execution_table) and converts keywords like 'findByLastName' into SQL conditions automatically.
What happens if the method name does not follow naming rules?
Spring cannot parse the method name into a query, so it will throw an error at startup or runtime (not shown in this trace).
Can the method return types vary?
Yes, return types like List<User>, Optional<User>, or User are supported depending on the query and method signature.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what query is generated when calling findByLastName with 'Smith'?
ASELECT * FROM user WHERE last_name = 'Smith'
BSELECT * FROM user WHERE first_name = 'Smith'
CSELECT * FROM user WHERE name = 'Smith'
DSELECT * FROM user
💡 Hint
Check Step 2 in execution_table for the generated query.
At which step does the database return the matching users?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Result' column in execution_table rows.
If the method was named findByAge, what would change in the execution_table?
AThe method would not be parsed
BThe query would filter by age instead of last_name
CThe query would remain the same
DThe result would be empty
💡 Hint
Naming convention changes the query generated as shown in Step 1 and 2.
Concept Snapshot
Custom query methods use method names to build queries.
Method names start with findBy, readBy, getBy, etc.
Keywords after 'By' map to entity fields.
Spring Data parses method name and creates SQL automatically.
Call method with parameters to run query and get results.
Full Transcript
In Spring Boot, you can create custom queries by simply naming methods in your repository interface. For example, a method named findByLastName will be parsed by Spring Data to generate a query that selects users where the last_name matches the given parameter. When you call this method with a value like 'Smith', Spring runs the query and returns the matching users. This process involves Spring reading the method name, converting it into a SQL query, executing it, and returning the results. If the method name does not follow the naming rules, Spring will not be able to generate the query and will throw an error. This feature lets you write queries without writing SQL, just by naming methods clearly.