Salut mes amis
Je suis entrain de réalisé une application avec Spring pour la quelle j'ai fait l'entité Etudiant suivante :
Ensuite L'interface IEtudiantRepository
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 package org.sid.entities; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @SuppressWarnings("serial") public class Etudiant implements Serializable { @Id @GeneratedValue private Long idEtudiant; @Column(name = "NOM", length = 80) private String nom; @Column(name = "PRENOM", length = 80) private String prenom; @Temporal(TemporalType.DATE) private Date dateNaissance; public Long getIdEtudiant() { return idEtudiant; } public void setIdEtudiant(Long idEtudiant) { this.idEtudiant = idEtudiant; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public Date getDateNaissance() { return dateNaissance; } public void setDateNaissance(Date dateNaissance) { this.dateNaissance = dateNaissance; } public Etudiant() { super(); } public Etudiant(String nom, String prenom, Date dateNaissance) { super(); this.nom = nom; this.prenom = prenom; this.dateNaissance = dateNaissance; } }
et la partie pom.xml est la suivante
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 package org.sid.dao; import org.sid.entities.Etudiant; import org.springframework.data.jpa.repository.JpaRepository; public interface IEtudiantRepository extends JpaRepository<Etudiant, Long> { }
Et la partie Controlleur est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>Banque</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Banque</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.0.0.RC1</version> </dependency> --> <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <!--<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.37.Final</version> </dependency> --> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
et la partie application.properties
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.sid.service; import java.util.List; import org.sid.dao.IEtudiantRepository; import org.sid.entities.Etudiant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class EtudiantRestService { @Autowired private IEtudiantRepository etudiantRepository; @RequestMapping(value = "/saveEtudiant", method = RequestMethod.GET) public Etudiant saveEtudiant(Etudiant et) { return etudiantRepository.save(et); } @RequestMapping(value = "/etudiants") public Page<Etudiant> listEtudiant(int page, int size) { return etudiantRepository.findAll(new PageRequest(page, size)); } @RequestMapping(value = "/etudiantsList") public List<Etudiant> listEtudiant() { return etudiantRepository.findAll(); } }
Jusqu’à maintenant tout va bien j’exécute l'application et tout va bien
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 spring.datasource.url = jdbc:mysql://localhost:3306/springbd spring.datasource.username = root spring.datasource.password = spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Mais lorsque j'ajoute des données dans la base de données pas d'ajout et il y a un message d'erreur qui s'affiche dans le console est la suivante:
Et la partie principale de mon projet est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.sid.dao.IEtudiantRepository' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at com.example.demo.BanqueApplication.main(BanqueApplication.java:22) ... 5 more
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package com.example.demo; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.sid.dao.IEtudiantRepository; import org.sid.entities.Etudiant; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; @SpringBootApplication @Configuration public class BanqueApplication { public static void main(String[] args) { ApplicationContext atx = SpringApplication.run(BanqueApplication.class, args); IEtudiantRepository etudiantRepository = atx.getBean(IEtudiantRepository.class); DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); try { etudiantRepository.save(new Etudiant("AAAA", "BBBB", df.parse("10-01-1983"))); etudiantRepository.save(new Etudiant("Yuiii", "KALAMA", df.parse("15-10-1963"))); etudiantRepository.save(new Etudiant("lokp", "DDDD", new Date())); etudiantRepository.save(new Etudiant("Vavava", "LLLL", df.parse("18-08-1980"))); } catch (ParseException e) { e.printStackTrace(); } List<Etudiant> etudiants = etudiantRepository.findAll(); etudiants.forEach(e -> System.out.println("le nom est : " + e.getNom())); } }
Partager