Development

스프링 JPA @Query 예 : 스프링 부팅의 사용자 정의 쿼리.

sonpro 2023. 3. 9. 08:10
반응형

Spring JPASummary: In this blog post, we will discuss how to use Spring JPA @Query to create custom queries in Spring Boot. We will cover the basics of @Query annotation, how to write custom queries using JPQL, and how to use named parameters and native queries.

Spring JPA @Query example : Custom query in spring boot.

Spring JPA is a powerful tool that allows developers to easily interact with databases using Java objects. One of the most useful features of Spring JPA is the ability to create custom queries using the @Query annotation. In this blog post, we will explore how to use @Query to create custom queries in Spring Boot.

The @Query annotation is used to define custom queries in Spring JPA. It allows developers to write JPQL (Java Persistence Query Language) or native SQL queries directly in their Java code. Here is an example of how to use @Query to create a custom query:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    @Query("SELECT u FROM User u WHERE u.email = ?1")
    User findByEmail(String email);
}

In this example, we are defining a custom query to find a user by their email address. The query is written in JPQL and uses a named parameter (?1) to represent the email parameter passed in.

Named parameters can also be used in @Query annotations. This allows developers to write more readable and maintainable queries. Here is an example of how to use named parameters in @Query:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    @Query("SELECT u FROM User u WHERE u.firstName = :firstName AND u.lastName = :lastName")
    List<User> findByFirstNameAndLastName(@Param("firstName") String firstName, @Param("lastName") String lastName);
}

In this example, we are defining a custom query to find users by their first and last names. The query uses named parameters (:firstName and :lastName) to represent the parameters passed in.

In addition to JPQL, @Query annotations can also be used to define native SQL queries. Native queries are SQL queries that are executed directly on the database. Here is an example of how to use a native query in @Query:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    @Query(value = "SELECT * FROM users WHERE email = ?1", nativeQuery = true)
    User findByEmail(String email);
}

In this example, we are defining a custom query to find a user by their email address using a native SQL query. The nativeQuery attribute of the @Query annotation is set to true to indicate that the query is a native SQL query.

In conclusion, Spring JPA @Query is a powerful tool that allows developers to create custom queries in Spring Boot. By using @Query, developers can write JPQL or native SQL queries directly in their Java code, making it easier to interact with databases. We hope this blog post has been helpful in understanding how to use @Query to create custom queries in Spring Boot.

반응형