我怎样才能获得在 spring 引导中具有角色的所有用户的列表?

How can i get the liste of all users with roles in spring boot?

我有 3 个表 (User(id_user...) - Role(id_role) - users_roles(id_role,id_user) (多对多))。我想获得所有 UserRoles 的列表。

// 服务

public List<User> AllUsers() {
    return loginRepo.findAll();
}

// 控制器

@GetMapping("/gestion_utilisateurs")
public String GetAllUsers(Model model) {
    List<User> all_users = loginServ.AllUsers();
    model.addAttribute("all_users", all_users);
    return "administration/identite_patient/gestion_utilisateurs";
}

// 查看

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}" ></td>
      <td></td>
  </tr>
<th:block>

// 用户

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Integer id;
    private String nom;
    private String prenom;
    private String username;
    private String password;
    private boolean enabled;
    
    
    @ManyToMany(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
    @JoinTable(
            name = "users_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id")
            )
    
    
    //private Collection<Role> roles = new HashSet<>();
    private List<Role> roles=new ArrayList<Role>(); 


    public User(String nom, String prenom, String username, String password,Boolean enabled, List<Role> roles) {
        super();
        this.nom = nom;
        this.prenom = prenom;
        this.username = username;
        this.password = password;
        this.enabled = true;
        this.roles = roles;
    }


    

        
    
    
    
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "roles")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Long id;
    private String role_name;
    
    public Role(String role_name) {
        this.role_name = role_name;
    }
    
    
    
    
}

我想显示 role_name 但我不知道如何获取它(all_users.roles.role_name 不起作用)。 如果有人有想法,请。

由于您可以在 user 中有多个 roles,因此您需要像处理 all_users 一样遍历它们以列出所有用户。所以沿着以下几行:

<th:block th:each="all_users : ${all_users}">
  <tr>
      <td th:style = "${all_users.enabled == true } ? 'color: green' : 'color: red' " > &diams; </td>
      <td th:text="${all_users.nom}"></td>
      <td th:text="${all_users.prenom}"></td>
      <td th:text="${all_users.username}"></td>
      <td th:text="${all_users.roles.role_name}"></td>
      <td th:each="role : ${all_users.roles}">
          <span th:text="${role.role_name}">
      </td>
      <td></td>
  </tr>
<th:block>