@Repository
public interface UserDao extends User {
public List<User> findByFirstname(String firstname);
}
How could I use above code to retrieve all records?
I tried findByFistname(null);
, it doesn't work...
I don't want to use findByFirstname();
because it's possible to have parameter.
Hope you all understand.
findById() method is used for finding the records from mysql.
The findAll() method calls userRepository's findAll() method and retrieves all users. The UserRepository extends from the CrudRepository . It provides the type of the entity and of its primary key.
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.
You should extend your repository from JpaRepository
. Be careful with name of repository (It should follow convention). After you inject your UserRepository
bean you will have already implemeted by spring data crud methods like findOne()
, findAll()
, delete()
etc.
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
//assume your primary key is Long type
}
Also will be useful documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With