春季 jpa 查询未获取到

huangapple go评论87阅读模式
英文:

Spring jpa query not fetching

问题

我需要使用Spring Data JPA执行此查询。

select substring_index(date_time, ' ', 1) as date, number, msg, type 
from sms 
where substring_index(date_time, ' ', 1) 
between ("parameter1") and ("parameter2") and type=("parameter3");

我定义的Java方法如下:上述查询应该使用以下三个参数来获取所需数据。

public List<Sms> findByDateContainingAndNumberAndMsgAndType(
                                        String startDate, String endDate, String type);

如何使用Spring Data JPA实现这一点呢?

英文:

I need to execute this query using spring data jpa

select substring_index(date_time,&#39; &#39;,1) as date, number,msg,type 
   from sms  where substring_index(date_time,&#39; &#39;,1) 
   between (&quot;parameter1&quot;) and (&quot;parameter2&quot;) and type=(&quot;parameter3&quot;)&quot;;

The java method i have defined is below: the above query should take the three paramters below to fetch the required data

public List&lt;Sms&gt; findByDateContainingAndNumberAndMsgAndType( 
                                         String startDate, String endDate,String type);

How can i do this using spring data jpa?

答案1

得分: 1

以下是您要翻译的内容:

在此情况下,根据查询需要返回多于2个字段,因此您需要使用对象来收集所有值。

可能的情况是,查询可以返回多行,因此为了收集所有这些行,我们需要使用 List

因此,请在您的控制器中添加类似以下内容:

@PostMapping("/endpoint")
public String search(Model model) {
    List<填写短信实体类名称> list = sms_service.retrieve(parameter1, 2, 3);
    model.addAttribute("list", list);
    return "index";
}

现在在您的存储库中包含以下代码:

@Query(value = "select substring_index(date_time, ' ', 1) as date, number, msg, type from sms where substring_index(date_time, ' ', 1) between ?1 and ?2 and type = ?3", nativeQuery = true)
List<类名> retrieve(parameter1, 2, 3);
英文:

Here you require more than 2 fields to be returned as per the query so you need to use the object to collect all values.

Chances are like the query can return multiple rows, so to collect them all we need to use List.

So include something like this in your controller

    @PostMapping(&quot;/endpoint&quot;)
    public String search(Model model) {
        List&lt;fill_sms_entity_class_name_here&gt; list = sms_service.retrieve(parameter1,2,3);
        model.addAttribute(&quot;list&quot;, list);
        return &quot;index&quot;;
    }

Now inside your repository include the following code

@Query(value=&quot;select substring_index(date_time,&#39; &#39;,1) as date, number,msg,type from sms where substring_index(date_time,&#39; &#39;,1) between ?1 and ?2 and type=?3&quot;, nativeQuery = true)
List&lt;classname&gt; retrieve(parameter1,2,3);

huangapple
  • 本文由 发表于 2020年10月15日 15:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64367011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定