@Query注释使用%% 1% [英] @Query annotation using like %?1%

查看:359
本文介绍了@Query注释使用%% 1%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个这样的查询

@Query("select p from Product p where p.name = ?1 or p.desc like %?1%")

但这给了我一个例外

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: % near line 1,

我尝试用'%'替换%或将查询字符串连接成这样:"select ... like'%'" +?1" +'%'",但是没有运气,请帮助我

I tried replacing % with '%' or concatenating the query string like this: "select ... like '%'" + "?1" + "'%'" but with no luck, please help me

推荐答案

如果您使用的是Spring Data JPA 1.3.1或更高版本,则需要执行以下操作:

If you are using Spring Data JPA version 1.3.1 or later you need to do the following:

@Query("select p from Product p where p.name = :name or p.desc like %:name%")
public List<Product> searchByName(@Param("name") String name);

查看博客文章以获取更多详细信息

Check out this blog post for more details

在Spring Data JPA 1.3.1之前,您不能在@Query批注中使用%,而是需要将其添加到参数本身中

Prior to Spring Data JPA 1.3.1, you could not use % in the @Query annotation, but instead you needed to add it in the argument itself

这篇关于@Query注释使用%% 1%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆