仅当isAdmin为true时才显示在线用户(display online users only when isAdmin is true)

编程入门 行业动态 更新时间:2024-10-24 09:17:08
仅当isAdmin为true时才显示在线用户(display online users only when isAdmin is true)

我正在使用Spring Boot应用程序创建一个在线聊天应用程序。 我可以使用以下代码显示所有在线用户

我的UserDao.java类的代码片段

public List getAll() { return getSession().createQuery("from User").list(); }

控制器调用UserDao.java方法来检索所有

@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; }

有没有办法只有当admin设置为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时检索所有用户,而不是仅显示所有用户。

I am creating an online chat application with Spring Boot application. I am able to display all the online users with the following code

Code snippets from my UserDao.java class

public List getAll() { return getSession().createQuery("from User").list(); }

Controller calling the UserDao.java method for retrieving all

@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; }

is there a way I can display all the users only when is admin is set to true.

Users.java bean class

@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; }

Please how can I retrieve all the Users only when isAdmin is set to true instead of just displaying all the users.

最满意答案

应该可以

public List getAll() { return getSession().createQuery("SELECT u FROM User u WHERE u.isAdmin = true").list(); }

免责声明:没有检查代码。

Should be possible with

public List getAll() { return getSession().createQuery("SELECT u FROM User u WHERE u.isAdmin = true").list(); }

Disclaimer: Didn't check the code.

更多推荐

本文发布于:2023-08-02 14:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在线   时才   用户   true   isAdmin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!