Home  >  Article  >  Spring and editable "WHERE" @Query

Spring and editable "WHERE" @Query

王林
王林forward
2024-02-08 21:30:121184browse

php editor Xigua will introduce to you a powerful feature in the Spring framework-editable "WHERE" @Query. The Spring framework is one of the most popular frameworks in Java development, and the @Query annotation is a way for defining custom queries provided by Spring Data JPA. In actual development, we often need to dynamically construct SQL query statements based on different conditions, and the editable "WHERE" @Query is born for this. By using this annotation, we can dynamically generate WHERE clauses in query methods as needed to achieve more flexible and efficient query operations. Next, we'll detail how to use editable "WHERE" @Query to optimize our query functionality.

Question content

Is it even possible to create something like this?

@Query(value = """
            SELECT Name
            FROM Users
            WHERE :column = :value
                   """, nativeQuery = true)
    List<Users> getValue(@Param("column") String column, @Param("value") String name);

and change "Columns" to the columns I want. userRepository.getVale("Name","Mike");

This way 1 query can be used for more options

Workaround

Using mybatis You can do this

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Select("select * from table where ${column} = #{value}")
List<Users> selectUsersByColumnValue(@Param("column")String column, @Param("value")String value);

Attention${column} SQL injection risk

The above is the detailed content of Spring and editable "WHERE" @Query. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete