英文:
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<DbUser, Integer> {
@Query("SELECT * from users WHERE platform = *****parameter here***** ")
public List<DbUser> 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 = "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;
}
}
答案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("SELECT * from users WHERE platform = :pltfrm")
public List<DbUser> findAllByPlatform(@Param("pltfrm") String platform);
答案2
得分: 0
你可以像这样做:
@Query("SELECT * from users WHERE platform = %?1")
英文:
You can do something like that
@Query("SELECT * from users WHERE platform = %?1")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论