Home > Java > body text

Spring and editable 'WHERE' @Query

王林
Release: 2024-02-08 21:30:12
forward
1238 people have browsed it

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template