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".

Comments

Popular posts from this blog

Flixify Video Streaming Platform

ASUS Battery Health Charging Software To Make Your Battery Life Longer

Push Notification Cordova, FCM And PHP