My First Full Stack Spring Boot / JQuery Application
Part 2 - Creating a Repository / Config
Create Repository
- Data Access Objects (DAOs) provide an abstraction for interacting with datastores.
- In the Spring framework, repositories act as a specific type of DAO which can access a respective database table.
- For example,
PersonRepositorycan access a respectivePERSONdatabase table.
- For example,
- Typically DAOs include an interface that provides
- a set of finder methods for retrieving data such as
readById,readAll - and methods to persist and delete data such as
create,updateById, anddeleteById
- a set of finder methods for retrieving data such as
- It is customary to have one
Repositorypermodelobject.
public interface PersonRepository extends CrudRepository<Person, Long> {
}
Prepopulate The Entity
- Populates the
Persontable in H2 with entities before the Web Server begins serving.
@Configuration
public class PersonConfig {
@Autowired
private PersonRepository repository;
@PostConstruct
public void setup() {
Person person1 = new Person();
person1.setFirstName("Leon");
person1.setLastName("Hunter");
Person person2 = new Person();
person2.setFirstName("John");
person2.setLastName("Doe");
repository.saveAll(Arrays.asList(
person1,
person2
));
}
}
- View the newly added records by
- restarting the application
- navigating to
localhost:8080/h2-console - Executing query
SELECT * FROM PERSONin theH2-Console


