如何在 Spring Boot 中将 return 管理员电子邮件作为列表
How to return admin emails in SpringBoot as a List
用户可以有多个角色。但是我如何 return 所有具有列表的管理员用户?我尝试了类似的解决方案,但他的解决方案与我的不同
使用request_manager; SELECT users
.username
FROM request_manager
.users
JOIN users_roles ON users.id=users_roles.user_id WHERE role_id=2;
此查询将 return 我在 SQL 中需要的内容,但我不知道如何在 springboot 中return 实现它。我
如果有其他更好的方法,我很乐意知道。
User.java(实体)
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
private String password;
private String name;
private String surname;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "user")
@ToString.Exclude
private List<RequestEntity> requestEntities;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
User user = (User) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return 0;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
this.roles.add(role);
}}
Role.java(实体)
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, length = 45)
private String name;
public Role() { }
public Role(String name) {
this.name = name;
}
public Role(Integer id, String name) {
this.id = id;
this.name = name;
}
public Role(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}}
UserRepository.java(回购)
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.username = ?1")
User findByUsername(String username);
}
我建议试试这个:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findAllByRole_Id(Integer roleId);
}
用户可以有多个角色。但是我如何 return 所有具有列表的管理员用户?我尝试了类似的解决方案,但他的解决方案与我的不同
使用request_manager; SELECT users
.username
FROM request_manager
.users
JOIN users_roles ON users.id=users_roles.user_id WHERE role_id=2;
此查询将 return 我在 SQL 中需要的内容,但我不知道如何在 springboot 中return 实现它。我 如果有其他更好的方法,我很乐意知道。
User.java(实体)
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
private String password;
private String name;
private String surname;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<Role> roles = new HashSet<>();
@OneToMany(mappedBy = "user")
@ToString.Exclude
private List<RequestEntity> requestEntities;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
User user = (User) o;
return Objects.equals(id, user.id);
}
@Override
public int hashCode() {
return 0;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
this.roles.add(role);
}}
Role.java(实体)
@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false, length = 45)
private String name;
public Role() { }
public Role(String name) {
this.name = name;
}
public Role(Integer id, String name) {
this.id = id;
this.name = name;
}
public Role(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}}
UserRepository.java(回购)
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.username = ?1")
User findByUsername(String username);
}
我建议试试这个:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findAllByRole_Id(Integer roleId);
}