Repository in Spring Framework For Beginners


Repository in spring is classes use to interact with database, that is simplify as CRUD (create, read, update and delete). To make a class repository, you add a @Repository annotation to the interfaces, but you also need to extends one of spring repository class that is CrudRepository, JpaRepository or PagingAndSortingRepository. And you need to mention your DAO, for example.

@Repository
public interface UserRepository extends JpaRepository<UserDao, Long> {
}


@Entity
class UserDao{
private Long id;
private String name;
}


Actually that's enough, now you can use your repository anywhere you want, just inject that component, for example to find a user you can use it.


@Controller
@RequestMapping("/user")
public Class UserController{
@Autowired 

private UserRepository userRepository;

@GetMapping("findAll")
public List<UserDao> allUser(){
return userRepository.findAll();
}
}


The above class is an example on how to get all user expose by endpoint "user/findAll".

Popular posts from this blog

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

How To Connect SSH Using PEM Certificate On Windows

Flutter Button With Left Align Text and Icon