简体   繁体   English

如何根据条件在 spring 启动时使用动态参数作为占位符构建本机查询?

[英]How Can I construct native query in spring boot with dynamic param as placeholder based on condition?

I am using MSSQL server as DB and the method in the repository layer of my spring boot project:我正在使用 MSSQL 服务器作为 DB 和我的 spring 启动项目的存储库层中的方法:

List<Object[]> SummaryData(@Param("startDate") LocalDateTime startDate,
                                           @Param("endDate") LocalDateTime endDate, List<Integer> cNoList);

here cNoList may contain only 0 or some values.这里cNoList可能只包含 0 或一些值。 i want when cNoList contains values such as (45,30,20 etc.) then the below query will execute:我想当 cNoList 包含诸如(45,30,20 等)之类的值时,将执行以下查询:

    SELECT 'CUSTOMER' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = 'CUSTOMER' and lg.is_verified = 1 and 
lg.sys_time BETWEEN ?1 AND ?2 AND cId in (?3)
                

when cNoList contains only 0 then the last part of the above query --> "AND cId in (?3)" will not execute just like below:当 cNoList 仅包含 0 时,上述查询的最后部分 --> "AND cId in (?3)" 将不会像下面这样执行:

SELECT 'CUSTOMER' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = 'CUSTOMER' and lg.is_verified = 1 and lg.sys_time BETWEEN ?1 AND ?2

i have made an attempt like below:我做了如下尝试:

"DECLARE @part VARCHAR(100) = ''; \n" +
                    "DECLARE @region_no INT = 420;\n" +
                    "DECLARE @start Datetime = '2022-07-01 00:00:00';\n" +
                    "DECLARE @end Datetime = '2023-01-17 23:59:59';\n" +
                    "DECLARE @tp VARCHAR(100) = 'CUSTOMER';\n" +
                    "SET @part = (CASE WHEN @region_no != 0 THEN 'and region_no = ' + CAST(@region_no AS VARCHAR(100)) ELSE '' END);\n" +
                    "EXEC ('SELECT ''' + @tp + ''' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut on lg.u_id = ut.tid_no where lg.user_type = ''' + @tp + ''' and lg.is_verified = 1 and \n" +
                    "lg.sys_time BETWEEN\n" +
                    "CONVERT(DATETIME, ''' + @start + ''', 120) \n" +
                    "AND\n" +
                    "CONVERT(DATETIME, ''' + @end + ''', 120) ' + @part)", nativeQuery = true)
                    
List<Object[]> SummaryData(@Param("startDate") LocalDateTime startDate,
                                           @Param("endDate") LocalDateTime endDate, List<Integer> cNoList);

this query executes well with the static value @region_no, @start, @end but these values will be assigned dynamically.此查询使用 static 值 @region_no、@start、@end 时执行良好,但这些值将动态分配。

issues I am facing:我面临的问题:

  • i tried putting a placeholder "?3" but not working.我试着放一个占位符“?3”但没有用。 how can I put a placeholder like "?3" instead of 420 in this line "DECLARE @region_no INT = 420"?我怎样才能在“DECLARE @region_no INT = 420”这一行中放置一个像“?3”这样的占位符而不是 420? (the value of @region_no needs to be something of type array of int but i guess it is of type only int here) (@region_no 的值需要是 int 类型的数组,但我猜它在这里只是 int 类型)

  • how can i use the placeholder for "DECLARE @start Datetime = '2022-07-01 00:00:00';\n", i want to use something like我如何使用“DECLARE @start Datetime = '2022-07-01 00:00:00';\n”的占位符,我想使用类似

    "DECLARE @start Datetime =?1;" “声明@start Datetime =?1;”

You need to use :param instead of ?1 .您需要使用:param而不是?1

For example:例如:

"DECLARE @part VARCHAR(100) = ''; \n" +
        "DECLARE @region_no INT = :cNoList;\n" +
        "DECLARE @start Datetime = :startDate;\n" +
        "DECLARE @end Datetime = :endDate;\n" +
        "DECLARE @tp VARCHAR(100) = 'CUSTOMER';\n" +
        "SET @part = (CASE WHEN @region_no != 0 THEN 'and region_no = ' + CAST(@region_no AS VARCHAR(100)) ELSE '' END);\n" +
        "EXEC ('SELECT ''' + @tp + ''' as title_key, COUNT(*) as total_count FROM LOGIN_TABLE lg left join USER_TABLE ut " +
        "on lg.u_id = ut.tid_no where lg.user_type = ''' + @tp + ''' and lg.is_verified = 1 and \n" +
        "lg.sys_time BETWEEN\n" +
        "CONVERT(DATETIME, ''' + @start + ''', 120) \n" +
        "AND\n" +
        "CONVERT(DATETIME, ''' + @end + ''', 120) ' + @part)", nativeQuery = true)

    List<Object[]> SummaryData(@Param("startDate") LocalDateTime startDate,
                               @Param("endDate") LocalDateTime endDate, 
                               @Param("cNoList") List<Integer> cNoList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jpa根据postgres的jsonb列的条件(不使用本机查询)在Spring Boot中选择数据? - How to select data in spring boot using jpa based on where condition of jsonb column of postgres(without native query)? 我如何在 spring 引导中编写本机查询,并在 PostgreSQL 中使用连接子句 - how can i write a native query in spring boot whith join clause in PostgreSQL spring 查询参数中的启动 offsetDateTime - spring boot offsetDateTime in query param 如何在spring boot中获取查询参数数组的值? - How to get value of query param array in spring boot? 如何在 spring 启动 rest Controller 中使用多个参数? - How can I use more then one param in a spring boot rest Controller? Spring 引导 JPA 原生查询 - 不是 null 检查基于参数 - Spring Boot JPA Native Query - is not null check based on parameter 在Spring Boot中映射为Query Param的值 - Map as a value for Query Param in Spring Boot I want to create a search filter in Spring Boot controller, that takes 6 paramas and param value may be null then how to write a Data JPA Query? - I want to create a search filter in Spring Boot controller, that takes 6 paramas and param value may be null then how to write a Data JPA Query? 在Spring Boot应用程序中无法解析占位符 - Placeholder can't be resolved in Spring boot application 如何在 Spring Data jpa 中应用本机连接查询? - How can i apply native join query in spring data jpa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM