J'ai créé cette application simple pour me connecter à la base de données MySQL et j'obtiens cette erreur:
org.springframework.jdbc.core.JdbcTemplate
Dans ma configuration (com.kubamadry.dao.MySqlStudentDao)
:
*****************************
APPLICATION FAILED TO START
*****************************
Description:
Field jdbcTemplate in com.kubamadry.dao.MySqlStudentDao required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:49838', transport: 'socket'
Process finished with exit code 1
La structure du projet:
Classes
Principal
package com.kubamadry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String args[]) {
SpringApplication.run(Main.class, args);
}
}
Étudiant
package com.kubamadry.entity;
public class Student {
private int id;
private String name;
private String course;
}
//Constructor, getters and setters
StudentController
package com.kubamadry.controller;
import com.kubamadry.entity.Student;
import com.kubamadry.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import Java.util.Collection;
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(method = RequestMethod.GET)
public Collection<Student> getAllStudents() {
return studentService.getAllStudents();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Student getStudentById(@PathVariable int id) {
return studentService.getStudentById(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void deleteStudentById(@PathVariable int id) {
studentService.deleteStudentById(id);
}
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateStudent(@RequestBody Student student) {
studentService.updateStudent(student);
}
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void addStudent(@RequestBody Student student) {
studentService.addStudent(student);
}
}
MySqlStudentDao
package com.kubamadry.dao;
import com.kubamadry.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.core.JdbcTemplate;
import Java.sql.ResultSet;
import Java.sql.SQLException;
import Java.util.Collection;
import Java.util.List;
@Repository("mysql")
public class MySqlStudentDao implements StudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Collection<Student> getAllStudents() {
final String sql = "SELECT * FROM students";
List<Student> students = jdbcTemplate.query(sql, new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setCourse(resultSet.getString("course"));
return student;
}
});
return students;
}
@Override
public Student getStudentById(int id) {
return null;
}
@Override
public void deleteStudentById(int id) {
//
}
@Override
public void updateStudent(Student student) {
//
}
@Override
public void addStudent(Student student) {
//
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost/students
spring.datasource.username=root
spring.datasource.password=password123
pom.xml
<?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.kubamadry</groupId>
<artifactId>workingProject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-Java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>6.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
<properties>
<Java.version>1.8</Java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Veuillez m'aider avec ce problème.
Vous devez déclarer JdbcTemplate
bean dans n'importe quel @Configuration
classe, par exemple:
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Pensez également à utiliser spring-boot-starter-jdbc
au lieu de spring-jdbc
dépendance. Le module de démarrage contient la plupart des bibliothèques dont vous aurez besoin à l'avenir.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
J'ai eu le même problème. peut-être, ce n'est pas votre travail mon ami. après une journée de recherche dans mes codes, j'ai compris que j'avais un problème avec ma dépendance dans le fichier pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
</dependency>
mais quand j'ai supprimé "test" dans 2 dépendances, j'ai réalisé que mon projet fonctionnait. Je pense que si vous passez en revue votre dépendance dans pom.xml (si utilisé maven), vous pourriez peut-être résoudre votre problème :)
pom.xml (pool de connexions Tomcat JDBC)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.Apache.Tomcat</groupId>
<artifactId>Tomcat-jdbc</artifactId>
</dependency>
pom.xml (HikariCP)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
YAML
spring:
application:
name: app
datasource:
url: url
username: user
password: password
driver-class-name: driverClass
C'est ça. Vous pouvez maintenant commencer à utiliser JdbcTemplate dans votre application
@Autowired
private JdbcTemplate jdbcTemplate;