英文:
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,' ',1) as date, number,msg,type
from sms where substring_index(date_time,' ',1)
between ("parameter1") and ("parameter2") and type=("parameter3")";
The java method i have defined is below: the above query should take the three paramters below to fetch the required data
public List<Sms> 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("/endpoint")
public String search(Model model) {
List<fill_sms_entity_class_name_here> list = sms_service.retrieve(parameter1,2,3);
model.addAttribute("list", list);
return "index";
}
Now inside your repository include the following code
@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<classname> retrieve(parameter1,2,3);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论