字段 jdbcTemplate 需要类型为 'org.springframework.jdbc.core.JdbcTemplate' 的 bean,但找不到
Field jdbcTemplate required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found
我做了这个简单的应用程序来连接到 MySQL 数据库,但我收到了这个错误:
org.springframework.jdbc.core.JdbcTemplate
在我的配置中(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
项目结构:
Image
类
主要
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);
}
}
学生
package com.kubamadry.entity;
public class Student {
private int id;
private String name;
private String course;
}
//Constructor, getters and setters
学生控制器
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>
请帮我解决这个问题。
您需要在任何@Configuration
class中声明JdbcTemplate
bean,例如:
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
此外,考虑使用 spring-boot-starter-jdbc
而不是 spring-jdbc
依赖项。入门模块包含您将来需要的大部分库。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
pom.xml(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
就是这样。现在您可以开始在您的应用程序中使用 JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
我遇到了同样的问题。也许,这不是你的工作,我的朋友。在我的代码中搜索了一天之后,我了解到我在 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>
但是当我在 2 个依赖项中删除 "test" 时,我意识到我的项目有效。
我想如果你在 pom.xml(如果使用 maven)中检查你的依赖关系,也许你可以解决你的问题:)
我做了这个简单的应用程序来连接到 MySQL 数据库,但我收到了这个错误:
org.springframework.jdbc.core.JdbcTemplate
在我的配置中(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
项目结构:
Image
类
主要
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);
}
}
学生
package com.kubamadry.entity;
public class Student {
private int id;
private String name;
private String course;
}
//Constructor, getters and setters
学生控制器
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>
请帮我解决这个问题。
您需要在任何@Configuration
class中声明JdbcTemplate
bean,例如:
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
此外,考虑使用 spring-boot-starter-jdbc
而不是 spring-jdbc
依赖项。入门模块包含您将来需要的大部分库。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
pom.xml(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
就是这样。现在您可以开始在您的应用程序中使用 JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
我遇到了同样的问题。也许,这不是你的工作,我的朋友。在我的代码中搜索了一天之后,我了解到我在 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>
但是当我在 2 个依赖项中删除 "test" 时,我意识到我的项目有效。 我想如果你在 pom.xml(如果使用 maven)中检查你的依赖关系,也许你可以解决你的问题:)