应用版本:SpringBoot 2.0 版本 , Tomcat 1.8 , Intellij Idea ,springDataJpa
//查询多个表,在某个站点下的指定tag的统计值
@Query(value = " where name IN (?1)) and source_time between ?2 and ?3",nativeQuery = true)
List selectTable(List<String> tag ,Timestamp time1,Timestamp time2);
多表查询,语句就不写了,in(?1) ,?1 包含多个值,前端传过来参数,本人接收,然后查询数据库进行返回。【前后端分离,且本人只做查询】
其实这里还有个问题,就是为什么用json字符串转java对象数组?
答:前端传给后端参数的时候,由于 tag 是多个值,数组的形式,但是后台接收的时候会莫名其妙出现个{ } ,导致接收到的是这样的-- tag:{"[ ]"} 的东西,SpringBoot 这里不识别这个{ } ,所以需要解析一下参数,就有了用json字符串转java对象数组。
@GetMapping("/selectTables")
public List selectTable(String tag ,Timestamp time1 ,Timestamp time2){
//json 字符串 转java 对象数组
JSONArray json = JSONArray.fromObject(tag);
List<String> tags = (List<String>)JSONArray.toCollection(json, String.class);
return metricsRepository.selectTable(tags,time1 ,time2);
}
参考: json字符串转java对象数组