gpt4 book ai didi

java - 字段 jdbcTemplate 需要类型为 'org.springframework.jdbc.core.JdbcTemplate' 的 bean,但无法找到

转载 作者:行者123 更新时间:2023-12-04 15:29:52 29 4
gpt4 key购买 nike

我做了这个简单的应用程序来连接到 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

类(class)

主页
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

学生 Controller
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) {
//
}
}

应用程序属性
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>

请帮我解决这个问题。

最佳答案

您需要申报JdbcTemplate bean 中任意@Configuration类,例如:

@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>

关于java - 字段 jdbcTemplate 需要类型为 'org.springframework.jdbc.core.JdbcTemplate' 的 bean,但无法找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51342685/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com