spring data jpa 使用原生sql查询数据库 原生sql中有in关键字 该如何传参?直接传List集合就能找到数据,解析List集合交给spring data jpa 框架去做
遇到问题?
第一次写的时候in 关键字后面传的是将List 集合转化为一个这样的字符串,
"'123','23','23'"
@Query(nativeQuery = true, value = "SELECT COUNT(beew.id) AS total FROM `bhpp2.0_evenmngtservice`.`evenmngt_workitem` beew "
+"WHERE beew.thingnum IN(:thingnumListStr) "
+"ORDER BY beew.id DESC"
)
public Object findWorkIteamByThingNumListTotal(@Param("thingnumListStr") String thingnumListStr);
然后发现不管什么情况下都查不到数据,鼓捣了半天,找到原因我们传的 字符串参数中单引号被spring data jpa 框架给转义了,将单引号转换为普通的字符串。
解决方法其实是想多了,直接传List集合就能找到数据,解析List集合交给spring data jpa 框架去做。
@Query(nativeQuery = true, value = "SELECT COUNT(beew.id) AS total FROM `bhpp2.0_evenmngtservice`.`evenmngt_workitem` beew "
+"WHERE beew.thingnum IN(:thingnumList) "
+"ORDER BY beew.id DESC"
)
public Object findWorkIteamByThingNumListTotal(@Param("thingnumList") List<String> thingnumList);
原文链接:https://blog.csdn.net/huxiaochao_6053/article/details/84749658 标签:jpa,spring,sql,List,beew,data From: https://www.cnblogs.com/sunny3158/p/17782528.html