How to create a Database Seeder in Spring Boot

Photo by Maksym Kaharlytskyi on Unsplash

Introduction

Implementation

@EventListener
public void seed(ContextRefreshedEvent event) {
// Here goes the seeders
}
@Autowired
UserRepository userRepository;
private void seedUsersTable() {
User user = new User();
user.setName("User's name");
user.setEmail("User's email");
userRepository.save(user);
}
@EventListener
public void seed(ContextRefreshedEvent event) {
seedUsersTable();
}

Conclusion

Software engineer @AE