Spring Data => accès base de données Ouvrir le fichier de configuration build.gradle Ajouter la librairie au dependencies :
implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
https://github.com/charroux/WebServiceExample/blob/master/build.gradle
Valider les changements => import changes (Intellij)
Sous Eclipse : clic droit projet -> gradle -> update
Ajouter org.springframework.data.annotation.Id au champs plateNumber
Ajouter org.springframework.data.mongodb.core.mapping.Document à la classe Car
Créer une interface :
public interface CarRepository extends CrudRepository<Car,String> {
}
Spring va implanter automatiquement cette interface. Vous bénéficierez alors des méthodes issues de CrudRepository :
Ajouter l'accès au repository dans le web service et modifier le constructeur du web service pour qu'il récupère les données dans la base :
@Autowired
CarRepository carRepository;
https://docs.mongodb.com/manual/installation/
cd C:\Program Files\MongoDB\Server\4.2\bin
mongod --dbpath C:\data\db
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtime("com.h2database:h2")
https://github.com/charroux/WebServiceExample/blob/master/build.gradle
Ajouter @Transient pour l'association dates afin d'éviter la création d'une table Dates
Ajouter spring.h2.console.enabled=true au fichier application.properties
https://github.com/charroux/WebServiceExample/blob/master/src/main/resources/application.properties
Open the H2 web console: http://localhost:8080/h2-console
In the login page make sure that you use jdbc:h2:mem:testdb as JDBC URL and org.h2.Driver as the Driver class.