0
我正在使用Spring Boot應用程序創建在線聊天應用程序。我可以用下面的代碼來顯示所有的在線用戶從我UserDao.java類僅當isAdmin爲真時才顯示在線用戶
public List getAll() {
return getSession().createQuery("from User").list();
}
控制器,要求檢索所有
@ResponseBody
@RequestMapping(value = "/get-all-users", method = RequestMethod.GET)
public List<User> getAllUsers() {
try {
return _userDao.getAll();
} catch (Exception e) {
logger.error("Exception in fetching users: ", e.getStackTrace());
}
return null;
}
的UserDao.java方法
代碼段
有沒有一種方法可以顯示所有的用戶只有當管理員設置爲true。
Users.java bean類
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "userId")
private Long id;
@Column(nullable = false)
private String name;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private long timestamp;
@Column(nullable = true)
private boolean isAdmin;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.id == null || obj == null || !(this.getClass().equals(obj.getClass()))) {
return false;
}
User that = (User) obj;
return this.id.equals(that.getId());
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return id == null ? 0 : id.hashCode();
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
請我怎麼只能檢索時isAdmin設置爲true,而不是僅僅顯示所有用戶的所有用戶。