CrudRepository带参数的查询

huangapple go评论85阅读模式
英文:

CrudRepository query with parameter

问题

我想知道是否有任何方法可以在传递给仓库的函数中使用参数,然后在 @Query 中使用该参数。

我想要按游戏平台对用户进行排序,所以我在我的 UserRepository 中添加了以下函数:

@Repository
public interface UserRepository extends CrudRepository<DbUser, Integer> {

    @Query("SELECT * from users WHERE platform = *****在这里放入参数***** ")
    public List<DbUser> findAllByPlatform(String platform);
}

有人知道这是否可能?如果可能,应该怎么做?如果不行,是否有干净的解决方法?提前感谢。

编辑:我的 DbUser 类:

@Entity
@Table(name = "users")
public class DbUser {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="user_id")
    private int UserId;

    @Column(name="user_name")
    private String UserName;

    @Column(name="email_address")
    private String EmailAddress;

    @Column(name="password_hash")
    private int PasswordHash;

    @Column(name="platform")
    private String Platform;

    @Column(name="platformid")
    private String PlatformID;

    @Convert(converter = StringListConvertor.class)
    private ArrayList<String> Wishlist;

    public DbUser(String userName, String emailAddress, int passwordHash, String platform, String platformID, String newWishlistItem){
        UserName = userName;
        EmailAddress = emailAddress;
        PasswordHash = passwordHash;
        Platform = platform;
        PlatformID = platformID;
        Wishlist.add(newWishlistItem);
    }

    public DbUser() {

    }

    public int getUserId() {
        return UserId;
    }

    public void setUserId(int userId) {
        UserId = userId;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getEmailAddress() {
        return EmailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        EmailAddress = emailAddress;
    }

    public int getPasswordHash() {
        return PasswordHash;
    }

    public void setPasswordHash(int passwordHash) {
        PasswordHash = passwordHash;
    }

    public String getPlatform() {
        return Platform;
    }

    public void setPlatform(String platform) {
        Platform = platform;
    }

    public String getPlatformID() {
        return PlatformID;
    }

    public void setPlatformID(String platformID) {
        PlatformID = platformID;
    }

    public ArrayList<String> getWishlist() {
        return Wishlist;
    }

    public void setWishlist(ArrayList<String> wishlist) {
        Wishlist = wishlist;
    }
}
英文:

I would like to know if there is any way to use a parameter from a function I pass to a repository can be used in the @Query.

I would like to sort users by gaming platform so I added the following function to my UserRepository:

@Repository
public interface UserRepository extends CrudRepository&lt;DbUser, Integer&gt; {
@Query(&quot;SELECT * from users WHERE platform = *****parameter here***** &quot;)
public List&lt;DbUser&gt; findAllByPlatform(String platform);
}

Does anybody know if this is possible? If so, how? If not, is there a clean workaround? Thanks in advance.

EDIT: My DbUser class:

@Entity
@Table(name = &quot;users&quot;)
public class DbUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name=&quot;user_id&quot;)
private int UserId;
@Column(name=&quot;user_name&quot;)
private String UserName;
@Column(name=&quot;email_address&quot;)
private String EmailAddress;
@Column(name=&quot;password_hash&quot;)
private int PasswordHash;
@Column(name=&quot;platform&quot;)
private String Platform;
@Column(name=&quot;platformid&quot;)
private String PlatformID;
@Convert(converter = StringListConvertor.class)
private ArrayList&lt;String&gt; Wishlist;
public DbUser(String userName, String emailAddress, int passwordHash, String platform, String platformID, String newWishlistItem){
UserName = userName;
EmailAddress = emailAddress;
PasswordHash = passwordHash;
Platform = platform;
PlatformID = platformID;
Wishlist.add(newWishlistItem);
}
public DbUser() {
}
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getEmailAddress() {
return EmailAddress;
}
public void setEmailAddress(String emailAddress) {
EmailAddress = emailAddress;
}
public int getPasswordHash() {
return PasswordHash;
}
public void setPasswordHash(int passwordHash) {
PasswordHash = passwordHash;
}
public String getPlatform() {
return Platform;
}
public void setPlatform(String platform) {
Platform = platform;
}
public String getPlatformID() {
return PlatformID;
}
public void setPlatformID(String platformID) {
PlatformID = platformID;
}
public ArrayList&lt;String&gt; getWishlist() {
return Wishlist;
}
public void setWishlist(ArrayList&lt;String&gt; wishlist) {
Wishlist = wishlist;
}
}

答案1

得分: 1

如果您正在使用Spring Data,请使用@Param注解参数,并提供要在查询中使用的变量名称:

@Query("SELECT * FROM users WHERE platform = :pltfrm")
public List<DbUser> findAllByPlatform(@Param("pltfrm") String platform);
英文:

If you're using Spring data, annotate parameter with @Param and supply variable name to be used in query:

@Query(&quot;SELECT * from users WHERE platform = :pltfrm&quot;)
public List&lt;DbUser&gt; findAllByPlatform(@Param(&quot;pltfrm&quot;) String platform);

答案2

得分: 0

你可以像这样做:

@Query("SELECT * from users WHERE platform = %?1")

spring data jpa 文档

英文:

You can do something like that

@Query(&quot;SELECT * from users WHERE platform = %?1&quot;)

spring data jpa documentation

huangapple
  • 本文由 发表于 2020年9月21日 20:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63992600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定