根据 Spring Boot 中的角色获取用户列表

发布于 2025-01-12 10:57:15 字数 4005 浏览 2 评论 0原文

我正在使用 spring boot、hibernate、mysql 数据库,我想按角色获取用户列表我有两个实体和一个枚举: ERole.java

package com.gestionAbscences.entity;

public enum ERole {
  ADMIN,
  CHEF_DEPARTEMENT,
  ENSIEGNANT,
  ETUDIANT,
}

Role.java:User.java

package com.gestionAbscences.entity;

import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "roles")
public class Role {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  @Enumerated(EnumType.STRING)
  @Column(length = 20)
  private ERole name;

  public Role(){

  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public ERole getName() {
    return name;
  }

  public void setName(ERole name) {
    this.name = name;
  }
}

package com.gestionAbscences.entity;


import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "users")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotBlank
  private String firstName;

  @NotBlank
  private String lastName;


  @NotBlank
  @Email
  private String email;

  @NotBlank
  private String password;


  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "user_roles",joinColumns = @JoinColumn(name = "user_id"),
  inverseJoinColumns = @JoinColumn(name = "role_id"))
  private Set<Role> roles = new HashSet<>();

  public User(){

  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public Set<Role> getRoles() {
    return roles;
  }

  public void setRoles(Set<Role> roles) {
    this.roles = roles;
  }
}

我的管理控制器是一个REST控制器:

AdminController.java:

package com.gestionAbscences.controller;


import com.gestionAbscences.entity.Role;
import com.gestionAbscences.entity.User;
import com.gestionAbscences.services.RoleService;
import com.gestionAbscences.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {

  @Autowired
  private UserService userService;

  @Autowired
  private RoleService roleService;


  @GetMapping("/users?={role}")
  public List<User> getUsers(@RequestParam int role){
     return userService.getUsersByRole(role);
  }


}

对于userRepository.java,我不知道如何编写sql语句来获取具有指定角色的用户列表:

userRepository .java:

package com.gestionAbscences.repository;

import com.gestionAbscences.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserRepository extends JpaRepository<User,Long> {

  //for this Query I don't know how to write it
  @Query("")
  List<User> findUserByRole(int role);
}

对于数据库的架构,它是这样的: 输入图片这里的描述

我使用的java版本是openjdk11,我使用的是spring 2.6.4

I'm using spring boot, hibernate, mysql database and I want to get list of users by their role I have two entities and one Enum:
ERole.java

package com.gestionAbscences.entity;

public enum ERole {
  ADMIN,
  CHEF_DEPARTEMENT,
  ENSIEGNANT,
  ETUDIANT,
}

Role.java:

package com.gestionAbscences.entity;

import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "roles")
public class Role {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  @Enumerated(EnumType.STRING)
  @Column(length = 20)
  private ERole name;

  public Role(){

  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public ERole getName() {
    return name;
  }

  public void setName(ERole name) {
    this.name = name;
  }
}

User.java:

package com.gestionAbscences.entity;


import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "users")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotBlank
  private String firstName;

  @NotBlank
  private String lastName;


  @NotBlank
  @Email
  private String email;

  @NotBlank
  private String password;


  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(name = "user_roles",joinColumns = @JoinColumn(name = "user_id"),
  inverseJoinColumns = @JoinColumn(name = "role_id"))
  private Set<Role> roles = new HashSet<>();

  public User(){

  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public Set<Role> getRoles() {
    return roles;
  }

  public void setRoles(Set<Role> roles) {
    this.roles = roles;
  }
}

My Admin controller is a REST Controller:

AdminController.java:

package com.gestionAbscences.controller;


import com.gestionAbscences.entity.Role;
import com.gestionAbscences.entity.User;
import com.gestionAbscences.services.RoleService;
import com.gestionAbscences.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {

  @Autowired
  private UserService userService;

  @Autowired
  private RoleService roleService;


  @GetMapping("/users?={role}")
  public List<User> getUsers(@RequestParam int role){
     return userService.getUsersByRole(role);
  }


}

And for the userRepository.java I don't know how to write the sql statement to get list of users with specified role:

userRepository.java:

package com.gestionAbscences.repository;

import com.gestionAbscences.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface UserRepository extends JpaRepository<User,Long> {

  //for this Query I don't know how to write it
  @Query("")
  List<User> findUserByRole(int role);
}

And for the schema of the DataBase it's like this:
enter image description here

the java version that I'm using is openjdk11, and I'm using spring 2.6.4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2025-01-19 10:57:15

据我了解,这应该有效!

  @Query("SELECT user FROM User user LEFT JOIN user.roles role WHERE role.id = ?1")
    List<User> findUserByRole(int role);

页面非常有用。

From what I understand,this should work!

  @Query("SELECT user FROM User user LEFT JOIN user.roles role WHERE role.id = ?1")
    List<User> findUserByRole(int role);

This page is quite useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文