如何解决考虑在您的配置中定义一个“com.jd.springdata.product.KlientJDBCTemplate”类型的 bean
我正在尝试运行连接到本地 MySQL 服务器的 spring 应用程序,但我遇到了无法解决的错误。
Description:
com.jd.springdata.product.StartApplication 中的字段 klientJDBCTemplate 需要一个无法找到的类型为“com.jd.springdata.product.KlientJDBCTemplate”的 bean。
注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)
操作:
考虑在您的配置中定义一个“com.jd.springdata.product.KlientJDBCTemplate”类型的 bean。
客户端JDBC类代码
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
public class KlientJDBCTemplate implements KlientDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
public void insert(String name,Integer age) {
String SQL = "insert into Klient (name,age) values (?,?)";
jdbcTemplateObject.update( SQL,name,age);
System.out.println("Created Record Name = " + name + " Age = " + age);
return;
}
public Klient findByid(Integer id) {
String SQL = "select * from Klient where id = ?";
Klient klient = jdbcTemplateObject.queryForObject(SQL,new KlientRowMapper(),id);
return klient;
}
public List<Klient> listaKlientow() {
String SQL = "select * from Klient";
List <Klient> klienci = jdbcTemplateObject.query(SQL,new KlientRowMapper());
return klienci;
}
public void delete(Integer id) {
String SQL = "delete from Klient where id = ?";
jdbcTemplateObject.update(SQL,id);
System.out.println("Deleted Record with ID = " + id );
return;
}
public void update(Integer id,Integer age) {
String SQL = "update Klient set age = ? where id = ?";
jdbcTemplateObject.update(SQL,age,id);
System.out.println("Updated Record with ID = " + id );
return;
}
}
主类
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
public class StartApplication implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(StartApplication.class);
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
KlientJDBCTemplate klientJDBCTemplate ;
public static void main(String[] args) {
SpringApplication.run(StartApplication.class,args);
}
@Override
public void run(String... args) {
System.out.println("StartApplication...");
log.info("StartApplication...");
startCustomerApp();
}
void startCustomerApp() {
System.out.println("------Wstawianie rekordów z danymi--------" );
log.info("------Wstawianie rekordów z danymi--------");
klientJDBCTemplate.insert("Adam",11);
klientJDBCTemplate.insert("Jan",2);
klientJDBCTemplate.insert("Robert",15);
System.out.println("------Lista wielu rekordow--------" );
log.info("------Lista wielu rekordow--------");
List<Klient> klienci = klientJDBCTemplate.listaKlientow();
for (Klient record : klienci) {
System.out.print("ID : " + record.getId() );
System.out.print(",Name : " + record.getName() );
System.out.println(",Age : " + record.getAge() );
}
System.out.println("---- Modyfikacja wiersza z ID = 2 -----" );
log.info("------ Modyfikacja wiersza z ID = 2 --------");
klientJDBCTemplate.update(2,20);
System.out.println("----Wylistowanie wiersza z ID = 2 -----" );
log.info("------ Wylistowanie wiersza z ID = 2 --------");
Klient klient = klientJDBCTemplate.findByid(2); System.out.print("ID : " + klient.getId() );
System.out.print(",Name : " + klient.getName() ); System.out.println(",Age : " + klient.getAge());
jdbcTemplate.execute("DROP TABLE klient IF EXISTS");
jdbcTemplate.execute("CREATE TABLE klient(" + "id INTEGER,name VARCHAR(100),age INTEGER)");
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/Kontakty
spring.datasource.username=springuser
spring.datasource.password=ThePassword
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jd.springdata.product</groupId>
<artifactId>lekcja8</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lekcja8</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
有人可以帮助我吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。