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

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

ASUS Battery Health Charging Software To Make Your Battery Life Longer

Is Nox Player a Malware? Nox Android Emulator

How To Uninstall Angular CLI, The Angular Framework

JavaFX Combo Box with Search Autocomplete

How To Create Spring Boot Project Using Netbeans

Push Notification Cordova, FCM And PHP

JavaFX Dialog Material Design

Inno Setup Installer Creator For Windows Programs

How to Add MySQL JDBC Driver To Wildfly