What is the @Query annotation in Spring Boot, its principle, and how to use it

What is the @Query annotation in Spring Boot, its principle, and how to use it

In Spring Boot, the @Query annotation is a very commonly used annotation for defining custom query statements. This article will introduce the function, principle and usage of @Query annotation.

insert image description here

1. The role of @Query annotation

In Spring Boot, the @Query annotation is used to define custom query statements. Through the @Query annotation, we can define any query statement that conforms to the SQL standard and map it to a method, so as to facilitate persistence, query, update and other operations.

The advantage of using the @Query annotation is that it can help us perform database operations more flexibly, especially for some complex query scenarios. At the same time, the @Query annotation can also be used in combination with other annotations, such as the @Param annotation, to specify parameters in the query statement.

2. The principle of @Query annotation

In Spring Boot, the @Query annotation is an annotation provided by Spring Data JPA, which is defined in the org.springframework.data.jpa.repository.Query package. The function of @Query annotation is to map a custom query statement to a method.

When using the @Query annotation, we can specify a SQL-compliant query statement in the annotation, and use placeholders (?1,?2, etc.) or named parameters (:name1, :name2, etc.) to represent the query statement parameter. At the same time, we can also use the @Param annotation to specify the mapping relationship between method parameters and parameters in the query statement.

In addition to the @Query annotation, Spring Data JPA also provides many other annotations, such as @NamedQuery, @NamedQueries, @QueryHints, etc., to control the behavior and configuration of query statements and query results.

3. How to use @Query annotation

In Spring Boot, we can use the @Query annotation to define custom query statements. The following is an example of defining a custom query statement using the @Query annotation:

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

    @Query("select u from User u where u.name = ?1 and u.age = ?2")
    List<User> findUsersByNameAndAge(String name, Integer age);

    @Query("select u from User u where u.name like %:name%")
    List<User> findUsersByNameLike(@Param("name") String name);

}

In this example, we define a UserRepository interface, and use the @Query annotation to define two custom query methods. In the first method, we use placeholders to represent the parameters in the query statement, and use ?1 and ?2 to match the method parameters with the parameters in the query statement. In the second method, we use named parameters to represent the parameters in the query statement, and use the @Param annotation to match the method parameters with the parameters in the query statement.

When using the @Query annotation, we can also use the @Modifying annotation to identify update operations, such as insert, update, delete, etc.

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

    @Modifying
    @Query("update User u set u.name = ?1 where u.id = ?2")
    void updateUserNameById(String name, Long id);

}

In this example, we define an update operation method, use the @Modifying annotation to identify the method as an update operation, and use the @Query annotation to define the update statement.

4. Summary

In Spring Boot, the @Query annotation is a very commonly used annotation for defining custom query statements. Using the @Query annotation can help us perform database operations more flexibly, especially for some complex query scenarios. At the same time, the @Query annotation can also be used in combination with other annotations, such as @Param, @Modifying, etc., to control the behavior and configuration of query statements and query results.

I hope this article is helpful to you, welcome to leave a message for exchange.

Guess you like

Origin blog.csdn.net/yujun2023/article/details/131485127