0
0
Spring Bootframework~5 mins

Custom query methods by naming convention in Spring Boot

Choose your learning style9 modes available
Introduction

Custom query methods let you find data easily without writing SQL. You just name the method to tell Spring Boot what to search for.

You want to get users by their email address.
You need to find products cheaper than a certain price.
You want to list orders placed after a specific date.
You want to check if a record exists with a certain name.
You want to count how many items belong to a category.
Syntax
Spring Boot
List<Entity> findByPropertyName(Type value);
Optional<Entity> findByPropertyName(Type value);
boolean existsByPropertyName(Type value);
long countByPropertyName(Type value);
List<Entity> findByPropertyNameAndOtherProperty(Type value1, Type value2);

Method names start with findBy, existsBy, or countBy.

Use And, Or to combine conditions in method names.

Examples
Finds all users with the given last name.
Spring Boot
List<User> findByLastName(String lastName);
Finds a user by email, returns empty if none found.
Spring Boot
Optional<User> findByEmail(String email);
Checks if a user exists with the given username.
Spring Boot
boolean existsByUsername(String username);
Finds products cheaper than the given price.
Spring Boot
List<Product> findByPriceLessThan(Double price);
Sample Program

This example shows a Spring Data JPA repository with custom query methods named by convention. The service class calls these methods and prints results.

Spring Boot
package com.example.demo.repository;

import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByLastName(String lastName);
    Optional<User> findByEmail(String email);
    boolean existsByUsername(String username);
}

// Sample usage in a service class
package com.example.demo.service;

import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void demoQueries() {
        List<User> smiths = userRepository.findByLastName("Smith");
        System.out.println("Users with last name Smith: " + smiths.size());

        Optional<User> userOpt = userRepository.findByEmail("alice@example.com");
        System.out.println("User with email alice@example.com found: " + userOpt.isPresent());

        boolean exists = userRepository.existsByUsername("alice123");
        System.out.println("User with username alice123 exists: " + exists);
    }
}
OutputSuccess
Important Notes

Spring Boot creates the query automatically based on method name.

Make sure property names in method match entity field names exactly.

Use Optional return type when you expect zero or one result.

Summary

Custom query methods let you search data by naming methods clearly.

Use prefixes like findBy, existsBy, countBy.

Combine conditions with And or Or in method names.