使用LIKE Spring JPA @Query [英] Spring JPA @Query with LIKE

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

问题描述

我正在尝试在CrudRepository中创建一个方法,该方法能够为我提供用户列表,其用户名是LIKE输入参数(不仅以开头,还包含它)。我试图使用方法findUserByUsernameLike(@Param(username)String username)但是在Spring文档中告诉它,这个方法等于
其中user.username喜欢?1 。这对我来说并不好,因为我已经告诉我,我正试图让所有用户名都包含的用户...

I'm trying to make a method in CrudRepository that will be able to give me list of users, whose usernames are LIKE the input parameter(not only begin with, but also contains it). I tried to use method "findUserByUsernameLike(@Param("username") String username)" but as it is told in Spring documentation, this method is equal to "where user.username like ?1". It is not good for me, as I already told that I'm trying to get all users whose username contains ...

我写了一个查询方法,但它甚至不部署。

I wrote a queryto the method but it even doesn't deploy.

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

@Query("select u from user u where u.username like '%username%'")
List<User> findUserByUsernameLike(@Param("username") String username);
}

任何人都可以帮我这个吗?

Can anybody help me with this?

推荐答案

尝试使用以下方法(它适用于我):

Try to use the following approach (it works for me):

@Query("SELECT u.username FROM User u WHERE u.username LIKE CONCAT('%',:username,'%')")
List<String> findUsersWithPartOfName(@Param("username") String username);

注意:JPQL中的表名必须以大写字母开头。

Notice: The table name in JPQL must start with a capital letter.

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

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