Find user by role using spring data jpa

后端 未结 2 1625
忘掉有多难
忘掉有多难 2021-01-28 10:51

My User Entity

@Entity
public class User {

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

    @Column(nullable = fals         


        
2条回答
  •  春和景丽
    2021-01-28 11:31

    In your UserRepository use in this way

    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.Collection;
    import java.util.List;
    
    public interface UserRepository extends JpaRepository {
        List findByRolesIn(Collection names, Pageable pageable);
    }
    

    In your Controller

    @GetMapping(value = "/api/usersByRole/{userRole}")
    public List getUser(@PathVariable String userRole, Pageable pageable){
        return userRepository.findByRolesIn(Arrays.asList(userRole), pageable);
    }
    

    And You will have result like this

提交回复
热议问题